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.parser;
014
015import java.lang.reflect.*;
016import java.nio.charset.*;
017import java.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.http.*;
021import org.apache.juneau.httppart.*;
022
023/**
024 * Runtime arguments common to all parser sessions.
025 */
026public final class ParserSessionArgs extends BeanSessionArgs {
027
028   Method javaMethod;
029   Object outer;
030
031   /**
032    * Default parser session args.
033    */
034   public static final ParserSessionArgs DEFAULT = new ParserSessionArgs();
035
036   /**
037    * Creator.
038    *
039    * @return A new parser session arguments object.
040    */
041   public static final ParserSessionArgs create() {
042      return new ParserSessionArgs();
043   }
044
045   //-----------------------------------------------------------------------------------------------------------------
046   // Properties.
047   //-----------------------------------------------------------------------------------------------------------------
048
049   /**
050    * File charset.
051    *
052    * <p>
053    * The character set to use for reading Files from the file system.
054    *
055    * <p>
056    * Used when passing in files to {@link Parser#parse(Object, Class)}.
057    *
058    * <p>
059    * If not specified, defaults to the JVM system default charset.
060    *
061    * @param value
062    *    The new property value.
063    *    <br>Can be <jk>null</jk>.
064    * @return This object (for method chaining).
065    */
066   public ParserSessionArgs fileCharset(Charset value) {
067      property(ReaderParser.RPARSER_fileCharset, value);
068      return this;
069   }
070
071   /**
072    * The java method that called this serializer, usually the method in a REST servlet.
073    *
074    * @param value
075    *    The new property value.
076    *    <br>Can be <jk>null</jk>.
077    * @return This object (for method chaining).
078    */
079   public ParserSessionArgs javaMethod(Method value) {
080      this.javaMethod = value;
081      return this;
082   }
083
084   /**
085    * The outer object for instantiating top-level non-static inner classes.
086    *
087    * @param value
088    *    The new property value.
089    *    <br>Can be <jk>null</jk>.
090    * @return This object (for method chaining).
091    */
092   public ParserSessionArgs outer(Object value) {
093      this.outer = value;
094      return this;
095   }
096
097   /**
098    * Input stream charset.
099    *
100    * <p>
101    * The character set to use for converting InputStreams and byte arrays to readers.
102    *
103    * <p>
104    * Used when passing in input streams and byte arrays to {@link Parser#parse(Object, Class)}.
105    *
106    * <p>
107    * If not specified, defaults to UTF-8.
108    *
109    * @param value
110    *    The new property value.
111    *    <br>Can be <jk>null</jk>.
112    * @return This object (for method chaining).
113    */
114   public ParserSessionArgs streamCharset(Charset value) {
115      property(ReaderParser.RPARSER_streamCharset, value);
116      return this;
117   }
118
119   @Override /* BeanSessionArgs */
120   public ParserSessionArgs debug(Boolean value) {
121      super.debug(value);
122      return this;
123   }
124
125   @Override /* BeanSessionArgs */
126   public ParserSessionArgs locale(Locale value) {
127      super.locale(value);
128      return this;
129   }
130
131   @Override /* BeanSessionArgs */
132   public ParserSessionArgs mediaType(MediaType value) {
133      super.mediaType(value);
134      return this;
135   }
136
137   @Override /* BeanSessionArgs */
138   public ParserSessionArgs schema(HttpPartSchema value) {
139      super.schema(value);
140      return this;
141   }
142
143   @Override /* BeanSessionArgs */
144   public ParserSessionArgs timeZone(TimeZone value) {
145      super.timeZone(value);
146      return this;
147   }
148
149   @Override /* SessionArgs */
150   public ParserSessionArgs properties(ObjectMap value) {
151      super.properties(value);
152      return this;
153   }
154
155   @Override /* SessionArgs */
156   public ParserSessionArgs property(String key, Object value) {
157      super.property(key, value);
158      return this;
159   }
160
161   //-----------------------------------------------------------------------------------------------------------------
162   // Other methods
163   //-----------------------------------------------------------------------------------------------------------------
164   
165   @Override /* SessionArgs */
166   public ObjectMap toMap() {
167      return super.toMap()
168         .append("ParserSessionArgs", new DefaultFilteringObjectMap()
169            .append("javaMethod", javaMethod)
170            .append("outer", outer)
171         );
172   }
173}