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