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