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 015import static org.apache.juneau.collections.JsonMap.*; 016 017import java.io.*; 018import java.lang.reflect.*; 019import java.nio.charset.*; 020import java.util.*; 021import java.util.function.*; 022 023import org.apache.juneau.*; 024import org.apache.juneau.collections.*; 025import org.apache.juneau.httppart.*; 026import org.apache.juneau.internal.*; 027 028/** 029 * Subclass of parser session objects for character-based parsers. 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 * <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a> 037 * </ul> 038 */ 039public class ReaderParserSession extends ParserSession { 040 041 //------------------------------------------------------------------------------------------------------------------- 042 // Static 043 //------------------------------------------------------------------------------------------------------------------- 044 045 /** 046 * Creates a new builder for this object. 047 * 048 * @param ctx The context creating this session. 049 * @return A new builder. 050 */ 051 public static Builder create(ReaderParser ctx) { 052 return new Builder(ctx); 053 } 054 055 //------------------------------------------------------------------------------------------------------------------- 056 // Builder 057 //------------------------------------------------------------------------------------------------------------------- 058 059 /** 060 * Builder class. 061 */ 062 @FluentSetters 063 public static class Builder extends ParserSession.Builder { 064 065 ReaderParser ctx; 066 Charset fileCharset, streamCharset; 067 068 /** 069 * Constructor 070 * 071 * @param ctx The context creating this session. 072 */ 073 protected Builder(ReaderParser ctx) { 074 super(ctx); 075 this.ctx = ctx; 076 fileCharset = ctx.fileCharset; 077 streamCharset = ctx.streamCharset; 078 } 079 080 @Override 081 public ReaderParserSession build() { 082 return new ReaderParserSession(this); 083 } 084 085 /** 086 * File charset. 087 * 088 * <p> 089 * The character set to use for reading Files from the file system. 090 * 091 * <p> 092 * Used when passing in files to {@link Parser#parse(Object, Class)}. 093 * 094 * <p> 095 * If not specified, defaults to the JVM system default charset. 096 * 097 * @param value 098 * The new property value. 099 * <br>Can be <jk>null</jk>. 100 * @return This object. 101 */ 102 @FluentSetter 103 public Builder fileCharset(Charset value) { 104 fileCharset = value; 105 return this; 106 } 107 108 /** 109 * Input stream charset. 110 * 111 * <p> 112 * The character set to use for converting InputStreams and byte arrays to readers. 113 * 114 * <p> 115 * Used when passing in input streams and byte arrays to {@link Parser#parse(Object, Class)}. 116 * 117 * <p> 118 * If not specified, defaults to UTF-8. 119 * 120 * @param value 121 * The new property value. 122 * <br>Can be <jk>null</jk>. 123 * @return This object. 124 */ 125 @FluentSetter 126 public Builder streamCharset(Charset value) { 127 streamCharset = value; 128 return this; 129 } 130 131 // <FluentSetters> 132 133 @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */ 134 public <T> Builder apply(Class<T> type, Consumer<T> apply) { 135 super.apply(type, apply); 136 return this; 137 } 138 139 @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */ 140 public Builder debug(Boolean value) { 141 super.debug(value); 142 return this; 143 } 144 145 @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */ 146 public Builder properties(Map<String,Object> value) { 147 super.properties(value); 148 return this; 149 } 150 151 @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */ 152 public Builder property(String key, Object value) { 153 super.property(key, value); 154 return this; 155 } 156 157 @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */ 158 public Builder unmodifiable() { 159 super.unmodifiable(); 160 return this; 161 } 162 163 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 164 public Builder locale(Locale value) { 165 super.locale(value); 166 return this; 167 } 168 169 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 170 public Builder localeDefault(Locale value) { 171 super.localeDefault(value); 172 return this; 173 } 174 175 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 176 public Builder mediaType(MediaType value) { 177 super.mediaType(value); 178 return this; 179 } 180 181 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 182 public Builder mediaTypeDefault(MediaType value) { 183 super.mediaTypeDefault(value); 184 return this; 185 } 186 187 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 188 public Builder timeZone(TimeZone value) { 189 super.timeZone(value); 190 return this; 191 } 192 193 @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */ 194 public Builder timeZoneDefault(TimeZone value) { 195 super.timeZoneDefault(value); 196 return this; 197 } 198 199 @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */ 200 public Builder javaMethod(Method value) { 201 super.javaMethod(value); 202 return this; 203 } 204 205 @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */ 206 public Builder outer(Object value) { 207 super.outer(value); 208 return this; 209 } 210 211 @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */ 212 public Builder schema(HttpPartSchema value) { 213 super.schema(value); 214 return this; 215 } 216 217 @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */ 218 public Builder schemaDefault(HttpPartSchema value) { 219 super.schemaDefault(value); 220 return this; 221 } 222 223 // </FluentSetters> 224 } 225 226 //------------------------------------------------------------------------------------------------------------------- 227 // Instance 228 //------------------------------------------------------------------------------------------------------------------- 229 230 private final ReaderParser ctx; 231 private final Charset fileCharset, streamCharset; 232 233 /** 234 * Constructor. 235 * 236 * @param builder The builder for this object. 237 */ 238 protected ReaderParserSession(Builder builder) { 239 super(builder); 240 ctx = builder.ctx; 241 fileCharset = builder.fileCharset; 242 streamCharset = builder.streamCharset; 243 } 244 245 @Override /* ParserSession */ 246 public final boolean isReaderParser() { 247 return true; 248 } 249 250 /** 251 * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into 252 * a stream or reader. 253 * 254 * @param input 255 * The input. 256 * <br>This can be any of the following types: 257 * <ul> 258 * <li><jk>null</jk> 259 * <li>{@link Reader} 260 * <li>{@link CharSequence} 261 * <li>{@link InputStream} containing UTF-8 encoded text (or whatever the encoding specified by 262 * {@link ReaderParser.Builder#streamCharset(Charset)}). 263 * <li><code><jk>byte</jk>[]</code> containing UTF-8 encoded text (or whatever the encoding specified by 264 * {@link ReaderParser.Builder#streamCharset(Charset)}). 265 * <li>{@link File} containing system encoded text (or whatever the encoding specified by 266 * {@link ReaderParser.Builder#streamCharset(Charset)}). 267 * </ul> 268 * @return 269 * A new {@link ParserPipe} wrapper around the specified input object. 270 */ 271 @SuppressWarnings("resource") 272 @Override /* ParserSesson */ 273 public final ParserPipe createPipe(Object input) { 274 return setPipe(new ParserPipe(input, isDebug(), ctx.isStrict(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), streamCharset, fileCharset)); 275 } 276 277 //----------------------------------------------------------------------------------------------------------------- 278 // Properties 279 //----------------------------------------------------------------------------------------------------------------- 280 281 /** 282 * Returns the file charset defined on this session. 283 * 284 * @return the file charset defined on this session. 285 */ 286 public Charset getFileCharset() { 287 return fileCharset; 288 } 289 290 /** 291 * Returns the stream charset defined on this session. 292 * 293 * @return the stream charset defined on this session. 294 */ 295 public Charset getStreamCharset() { 296 return streamCharset; 297 } 298 299 //----------------------------------------------------------------------------------------------------------------- 300 // Other methods 301 //----------------------------------------------------------------------------------------------------------------- 302 303 @Override /* ContextSession */ 304 protected JsonMap properties() { 305 return filteredMap("fileCharset", fileCharset, "streamCharset", streamCharset); 306 } 307}