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
015
016import java.io.*;
017
018import org.apache.juneau.*;
019
020/**
021 * Subclass of parser session objects for character-based parsers.
022 *
023 * <p>
024 * This class is NOT thread safe.  It is typically discarded after one-time use.
025 */
026public abstract class ReaderParserSession extends ParserSession {
027
028   private final ReaderParser ctx;
029
030   /**
031    * Create a new session using properties specified in the context.
032    *
033    * @param ctx
034    *    The parser creating this session object.
035    *    The parser contains all the configuration settings for this object.
036    * @param args
037    *    Runtime session arguments.
038    */
039   protected ReaderParserSession(ReaderParser ctx, ParserSessionArgs args) {
040      super(ctx, args);
041      this.ctx = ctx;
042   }
043
044   /**
045    * Constructor for sessions that don't require context.
046    *
047    * @param args
048    *    Runtime session arguments.
049    */
050   protected ReaderParserSession(ParserSessionArgs args) {
051      this(ReaderParser.DEFAULT, args);
052   }
053
054
055   @Override /* ParserSession */
056   public final boolean isReaderParser() {
057      return true;
058   }
059
060   /**
061    * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into
062    * a stream or reader.
063    *
064    * @param input
065    *    The input.
066    *    <br>This can be any of the following types:
067    *    <ul>
068    *       <li><jk>null</jk>
069    *       <li>{@link Reader}
070    *       <li>{@link CharSequence}
071    *       <li>{@link InputStream} containing UTF-8 encoded text (or whatever the encoding specified by
072    *          {@link ReaderParser#RPARSER_inputStreamCharset}).
073    *       <li><code><jk>byte</jk>[]</code> containing UTF-8 encoded text (or whatever the encoding specified by
074    *          {@link ReaderParser#RPARSER_inputStreamCharset}).
075    *       <li>{@link File} containing system encoded text (or whatever the encoding specified by
076    *          {@link ReaderParser#RPARSER_fileCharset}).
077    *    </ul>
078    * @return
079    *    A new {@link ParserPipe} wrapper around the specified input object.
080    */
081   @SuppressWarnings("resource")
082   @Override /* ParserSesson */
083   public final ParserPipe createPipe(Object input) {
084      return setPipe(new ParserPipe(input, isDebug(), ctx.isStrict(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), getFileCharset(), getInputStreamCharset()));
085   }
086
087   //-----------------------------------------------------------------------------------------------------------------
088   // Properties
089   //-----------------------------------------------------------------------------------------------------------------
090
091   /**
092    * Configuration property:  Input stream charset.
093    *
094    * @see ReaderParser#RPARSER_inputStreamCharset
095    * @return
096    *    The character set to use for converting <code>InputStreams</code> and byte arrays to readers.
097    */
098   protected final String getInputStreamCharset() {
099      return ctx.getInputStreamCharset();
100   }
101
102   /**
103    * Configuration property:  File charset.
104    *
105    * @see ReaderParser#RPARSER_fileCharset
106    * @return
107    *    The character set to use for reading <code>Files</code> from the file system.
108    */
109   protected final String getFileCharset() {
110      return ctx.getFileCharset();
111   }
112
113   @Override /* Session */
114   public ObjectMap asMap() {
115      return super.asMap()
116         .append("ReaderParserSession", new ObjectMap()
117         );
118   }
119}