001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.xml;
014
015import java.lang.reflect.*;
016import java.nio.charset.*;
017import java.util.*;
018import java.util.function.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.httppart.*;
022import org.apache.juneau.internal.*;
023import org.apache.juneau.serializer.*;
024import org.apache.juneau.svl.*;
025
026/**
027 * Session object that lives for the duration of a single use of {@link XmlDocSerializer}.
028 *
029 * <h5 class='section'>Notes:</h5><ul>
030 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
031 * </ul>
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.XmlDetails">XML Details</a>
035 * </ul>
036 */
037public class XmlDocSerializerSession extends XmlSerializerSession {
038
039   //-----------------------------------------------------------------------------------------------------------------
040   // Static
041   //-----------------------------------------------------------------------------------------------------------------
042
043   /**
044    * Creates a new builder for this object.
045    *
046    * @param ctx The context creating this session.
047    * @return A new builder.
048    */
049   public static Builder create(XmlDocSerializer ctx) {
050      return new Builder(ctx);
051   }
052
053   //-----------------------------------------------------------------------------------------------------------------
054   // Builder
055   //-----------------------------------------------------------------------------------------------------------------
056
057   /**
058    * Builder class.
059    */
060   @FluentSetters
061   public static class Builder extends XmlSerializerSession.Builder {
062
063      XmlDocSerializer ctx;
064
065      /**
066       * Constructor
067       *
068       * @param ctx The context creating this session.
069       */
070      protected Builder(XmlDocSerializer ctx) {
071         super(ctx);
072         this.ctx = ctx;
073      }
074
075      @Override
076      public XmlDocSerializerSession build() {
077         return new XmlDocSerializerSession(this);
078      }
079
080      // <FluentSetters>
081
082      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
083      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
084         super.apply(type, apply);
085         return this;
086      }
087
088      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
089      public Builder debug(Boolean value) {
090         super.debug(value);
091         return this;
092      }
093
094      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
095      public Builder properties(Map<String,Object> value) {
096         super.properties(value);
097         return this;
098      }
099
100      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
101      public Builder property(String key, Object value) {
102         super.property(key, value);
103         return this;
104      }
105
106      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
107      public Builder unmodifiable() {
108         super.unmodifiable();
109         return this;
110      }
111
112      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
113      public Builder locale(Locale value) {
114         super.locale(value);
115         return this;
116      }
117
118      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
119      public Builder localeDefault(Locale value) {
120         super.localeDefault(value);
121         return this;
122      }
123
124      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
125      public Builder mediaType(MediaType value) {
126         super.mediaType(value);
127         return this;
128      }
129
130      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
131      public Builder mediaTypeDefault(MediaType value) {
132         super.mediaTypeDefault(value);
133         return this;
134      }
135
136      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
137      public Builder timeZone(TimeZone value) {
138         super.timeZone(value);
139         return this;
140      }
141
142      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
143      public Builder timeZoneDefault(TimeZone value) {
144         super.timeZoneDefault(value);
145         return this;
146      }
147
148      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
149      public Builder javaMethod(Method value) {
150         super.javaMethod(value);
151         return this;
152      }
153
154      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
155      public Builder resolver(VarResolverSession value) {
156         super.resolver(value);
157         return this;
158      }
159
160      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
161      public Builder schema(HttpPartSchema value) {
162         super.schema(value);
163         return this;
164      }
165
166      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
167      public Builder schemaDefault(HttpPartSchema value) {
168         super.schemaDefault(value);
169         return this;
170      }
171
172      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
173      public Builder uriContext(UriContext value) {
174         super.uriContext(value);
175         return this;
176      }
177
178      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
179      public Builder fileCharset(Charset value) {
180         super.fileCharset(value);
181         return this;
182      }
183
184      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
185      public Builder streamCharset(Charset value) {
186         super.streamCharset(value);
187         return this;
188      }
189
190      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
191      public Builder useWhitespace(Boolean value) {
192         super.useWhitespace(value);
193         return this;
194      }
195
196      // </FluentSetters>
197   }
198
199   //-----------------------------------------------------------------------------------------------------------------
200   // Instance
201   //-----------------------------------------------------------------------------------------------------------------
202
203   /**
204    * Constructor.
205    *
206    * @param builder The builder for this object.
207    */
208   protected XmlDocSerializerSession(Builder builder) {
209      super(builder);
210   }
211
212   @Override /* SerializerSession */
213   protected void doSerialize(SerializerPipe out, Object o) throws java.io.IOException, SerializeException {
214      try (XmlWriter w = getXmlWriter(out)) {
215         w.append("<?xml")
216            .attr("version", "1.0")
217            .attr("encoding", "UTF-8")
218            .appendln("?>");
219         w.flush();
220         super.doSerialize(out, o);
221      }
222   }
223}