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