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.serializer;
014
015import static org.apache.juneau.serializer.Serializer.*;
016
017import java.lang.reflect.*;
018import java.nio.charset.*;
019import java.util.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.collections.*;
023import org.apache.juneau.http.*;
024import org.apache.juneau.httppart.*;
025import org.apache.juneau.internal.*;
026import org.apache.juneau.svl.*;
027
028/**
029 * Runtime arguments common to all serializer sessions.
030 *
031 * <p>
032 * This object specifies information such as session locale or URI context.
033 */
034public final class SerializerSessionArgs extends BeanSessionArgs {
035
036   /**
037    * Default serializer session args.
038    */
039   public static final SerializerSessionArgs DEFAULT = new SerializerSessionArgs();
040
041   Method javaMethod;
042   VarResolverSession resolver;
043
044   /**
045    * Creator.
046    *
047    * @return A new parser session arguments object.
048    */
049   public static final SerializerSessionArgs create() {
050      return new SerializerSessionArgs();
051   }
052
053   //-----------------------------------------------------------------------------------------------------------------
054   // Properties.
055   //-----------------------------------------------------------------------------------------------------------------
056
057   /**
058    * File charset.
059    *
060    * <p>
061    * The character set to use for writing Files to the file system.
062    *
063    * <p>
064    * Used when passing in files to {@link Serializer#serialize(Object, Object)}.
065    *
066    * <p>
067    * If not specified, defaults to the JVM system default charset.
068    *
069    * @param value
070    *    The new property value.
071    *    <br>Can be <jk>null</jk>.
072    * @return This object (for method chaining).
073    */
074   @FluentSetter
075   public SerializerSessionArgs fileCharset(Charset value) {
076      property(WriterSerializer.WSERIALIZER_fileCharset, value);
077      return this;
078   }
079
080   /**
081    * The java method that called this serializer, usually the method in a REST servlet.
082    *
083    * @param value
084    *    The new property value.
085    *    <br>Can be <jk>null</jk>.
086    * @return This object (for method chaining).
087    */
088   @FluentSetter
089   public SerializerSessionArgs javaMethod(Method value) {
090      this.javaMethod = value;
091      return this;
092   }
093
094   /**
095    * String variable resolver.
096    *
097    * <p>
098    * If not specified, defaults to session created by {@link VarResolver#DEFAULT}.
099    *
100    * @param value
101    *    The new property value.
102    *    <br>Can be <jk>null</jk>.
103    * @return This object (for method chaining).
104    */
105   @FluentSetter
106   public SerializerSessionArgs resolver(VarResolverSession value) {
107      this.resolver = value;
108      return this;
109   }
110
111   /**
112    * Output stream charset.
113    *
114    * <p>
115    * The character set to use when writing to OutputStreams.
116    *
117    * <p>
118    * Used when passing in output streams and byte arrays to {@link WriterSerializer#serialize(Object, Object)}.
119    *
120    * <p>
121    * If not specified, defaults to UTF-8.
122    *
123    * @param value
124    *    The new property value.
125    *    <br>Can be <jk>null</jk>.
126    * @return This object (for method chaining).
127    */
128   @FluentSetter
129   public SerializerSessionArgs streamCharset(Charset value) {
130      property(WriterSerializer.WSERIALIZER_streamCharset, value);
131      return this;
132   }
133
134   /**
135    * URI context bean.
136    *
137    * <p>
138    * Bean used for resolution of URIs to absolute or root-relative form.
139    *
140    * <p>
141    * If not specified, defaults to {@link Serializer#SERIALIZER_uriContext}.
142    *
143    * @param value
144    *    The new property value.
145    *    <br>Can be <jk>null</jk>.
146    * @return This object (for method chaining).
147    */
148   @FluentSetter
149   public SerializerSessionArgs uriContext(UriContext value) {
150      property(SERIALIZER_uriContext, value);
151      return this;
152   }
153
154   /**
155    * Use whitespace.
156    *
157    * <p>
158    * If true, whitespace is added to the output to improve readability.
159    *
160    * @param value
161    *    The new property value.
162    *    <br>Can be <jk>null</jk>.
163    * @return This object (for method chaining).
164    */
165   @FluentSetter
166   public SerializerSessionArgs useWhitespace(Boolean value) {
167      property(WriterSerializer.WSERIALIZER_useWhitespace, value);
168      return this;
169   }
170
171   // <FluentSetters>
172
173   @Override /* GENERATED - SessionArgs */
174   public SerializerSessionArgs debug(Boolean value) {
175      super.debug(value);
176      return this;
177   }
178
179   @Override /* GENERATED - SessionArgs */
180   public SerializerSessionArgs locale(Locale value) {
181      super.locale(value);
182      return this;
183   }
184
185   @Override /* GENERATED - SessionArgs */
186   public SerializerSessionArgs mediaType(MediaType value) {
187      super.mediaType(value);
188      return this;
189   }
190
191   @Override /* GENERATED - SessionArgs */
192   public SerializerSessionArgs properties(OMap value) {
193      super.properties(value);
194      return this;
195   }
196
197   @Override /* GENERATED - SessionArgs */
198   public SerializerSessionArgs property(String key, Object value) {
199      super.property(key, value);
200      return this;
201   }
202
203   @Override /* GENERATED - SessionArgs */
204   public SerializerSessionArgs timeZone(TimeZone value) {
205      super.timeZone(value);
206      return this;
207   }
208
209   @Override /* GENERATED - BeanSessionArgs */
210   public SerializerSessionArgs schema(HttpPartSchema value) {
211      super.schema(value);
212      return this;
213   }
214
215   // </FluentSetters>
216
217   //-----------------------------------------------------------------------------------------------------------------
218   // Other methods
219   //-----------------------------------------------------------------------------------------------------------------
220
221   @Override /* SessionArgs */
222   public OMap toMap() {
223      return super.toMap()
224         .a("SerializerSessionArgs", new DefaultFilteringOMap()
225            .a("javaMethod", javaMethod)
226            .a("resolver", resolver)
227         );
228   }
229}