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.plaintext;
014
015import static org.apache.juneau.common.internal.IOUtils.*;
016
017import java.io.IOException;
018import java.lang.reflect.*;
019import java.nio.charset.*;
020import java.util.*;
021import java.util.function.*;
022
023import org.apache.juneau.*;
024import org.apache.juneau.httppart.*;
025import org.apache.juneau.internal.*;
026import org.apache.juneau.parser.*;
027
028/**
029 * Session object that lives for the duration of a single use of {@link PlainTextParser}.
030 *
031 * <h5 class='section'>Notes:</h5><ul>
032 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 * </ul>
037 */
038public class PlainTextParserSession extends ReaderParserSession {
039
040   //-------------------------------------------------------------------------------------------------------------------
041   // Static
042   //-------------------------------------------------------------------------------------------------------------------
043
044   /**
045    * Creates a new builder for this object.
046    *
047    * @param ctx The context creating this session.
048    * @return A new builder.
049    */
050   public static Builder create(PlainTextParser ctx) {
051      return new Builder(ctx);
052   }
053
054   //-------------------------------------------------------------------------------------------------------------------
055   // Builder
056   //-------------------------------------------------------------------------------------------------------------------
057
058   /**
059    * Builder class.
060    */
061   @FluentSetters
062   public static class Builder extends ReaderParserSession.Builder {
063
064      PlainTextParser ctx;
065
066      /**
067       * Constructor
068       *
069       * @param ctx The context creating this session.
070       */
071      protected Builder(PlainTextParser ctx) {
072         super(ctx);
073         this.ctx = ctx;
074      }
075
076      @Override
077      public PlainTextParserSession build() {
078         return new PlainTextParserSession(this);
079      }
080
081      // <FluentSetters>
082
083      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
084      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
085         super.apply(type, apply);
086         return this;
087      }
088
089      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
090      public Builder debug(Boolean value) {
091         super.debug(value);
092         return this;
093      }
094
095      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
096      public Builder properties(Map<String,Object> value) {
097         super.properties(value);
098         return this;
099      }
100
101      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
102      public Builder property(String key, Object value) {
103         super.property(key, value);
104         return this;
105      }
106
107      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
108      public Builder unmodifiable() {
109         super.unmodifiable();
110         return this;
111      }
112
113      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
114      public Builder locale(Locale value) {
115         super.locale(value);
116         return this;
117      }
118
119      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
120      public Builder localeDefault(Locale value) {
121         super.localeDefault(value);
122         return this;
123      }
124
125      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
126      public Builder mediaType(MediaType value) {
127         super.mediaType(value);
128         return this;
129      }
130
131      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
132      public Builder mediaTypeDefault(MediaType value) {
133         super.mediaTypeDefault(value);
134         return this;
135      }
136
137      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
138      public Builder timeZone(TimeZone value) {
139         super.timeZone(value);
140         return this;
141      }
142
143      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
144      public Builder timeZoneDefault(TimeZone value) {
145         super.timeZoneDefault(value);
146         return this;
147      }
148
149      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
150      public Builder javaMethod(Method value) {
151         super.javaMethod(value);
152         return this;
153      }
154
155      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
156      public Builder outer(Object value) {
157         super.outer(value);
158         return this;
159      }
160
161      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
162      public Builder schema(HttpPartSchema value) {
163         super.schema(value);
164         return this;
165      }
166
167      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
168      public Builder schemaDefault(HttpPartSchema value) {
169         super.schemaDefault(value);
170         return this;
171      }
172
173      @Override /* GENERATED - org.apache.juneau.parser.ReaderParserSession.Builder */
174      public Builder fileCharset(Charset value) {
175         super.fileCharset(value);
176         return this;
177      }
178
179      @Override /* GENERATED - org.apache.juneau.parser.ReaderParserSession.Builder */
180      public Builder streamCharset(Charset value) {
181         super.streamCharset(value);
182         return this;
183      }
184
185      // </FluentSetters>
186   }
187
188   //-------------------------------------------------------------------------------------------------------------------
189   // Instance
190   //-------------------------------------------------------------------------------------------------------------------
191
192   /**
193    * Constructor.
194    *
195    * @param builder The builder for this object.
196    */
197   protected PlainTextParserSession(Builder builder) {
198      super(builder);
199   }
200
201   @Override /* ParserSession */
202   protected <T> T doParse(ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException {
203      return convertToType(read(pipe.getReader()), type);
204   }
205}