All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.parser.ReaderParser Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.parser;

import java.nio.charset.*;

import org.apache.juneau.*;

/**
 * Subclass of {@link Parser} for characters-based parsers.
 *
 * 
Description
* * This class is typically the parent class of all character-based parsers. * It has 1 abstract method to implement... *
    *
  • parse(ParserSession, ClassMeta) *
*/ public abstract class ReaderParser extends Parser { //------------------------------------------------------------------------------------------------------------------- // Configurable properties //------------------------------------------------------------------------------------------------------------------- private static final String PREFIX = "ReaderParser."; /** * Configuration property: File charset. * *
Property:
*
    *
  • Name: "ReaderParser.fileCharset.s" *
  • Data type: String *
  • Default: "DEFAULT" *
  • Session property: false *
  • Methods: *
      *
    • {@link ReaderParserBuilder#fileCharset(String)} *
    • {@link ReaderParserBuilder#fileCharset(Charset)} *
    *
* *
Description:
*

* The character set to use for reading Files from the file system. * *

* Used when passing in files to {@link Parser#parse(Object, Class)}. * *

* "DEFAULT" can be used to indicate the JVM default file system charset. * *

Example:
*

* // Create a parser that reads UTF-8 files. * ReaderParser p = JsonParser. * .create() * .fileCharset("UTF-8") * .build(); * * // Same, but use property. * ReaderParser p = JsonParser. * .create() * .set(PARSER_fileCharset, "UTF-8") * .build(); * * // Use it to read a UTF-8 encoded file. * MyBean myBean = p.parse(new File("MyBean.txt"), MyBean.class); *

*/ public static final String RPARSER_fileCharset = PREFIX + "fileCharset.s"; /** * Configuration property: Input stream charset. * *
Property:
*
    *
  • Name: "ReaderParser.inputStreamCharset.s" *
  • Data type: String *
  • Default: "UTF-8" *
  • Session property: false *
  • Methods: *
      *
    • {@link ReaderParserBuilder#inputStreamCharset(String)} *
    • {@link ReaderParserBuilder#inputStreamCharset(Charset)} *
    *
* *
Description:
*

* The character set to use for converting InputStreams and byte arrays to readers. * *

* Used when passing in input streams and byte arrays to {@link Parser#parse(Object, Class)}. * *

Example:
*

* // Create a parser that reads UTF-8 files. * ReaderParser p = JsonParser. * .create() * .inputStreamCharset("UTF-8") * .build(); * * // Same, but use property. * ReaderParser p = JsonParser. * .create() * .set(PARSER_inputStreamCharset, "UTF-8") * .build(); * * // Use it to read a UTF-8 encoded input stream. * MyBean myBean = p.parse(new FileInputStream("MyBean.txt"), MyBean.class); *

*/ public static final String RPARSER_inputStreamCharset = PREFIX + "inputStreamCharset.s"; static final ReaderParser DEFAULT = new ReaderParser(PropertyStore.create().build(), "") { @Override public ReaderParserSession createSession(ParserSessionArgs args) { throw new NoSuchMethodError(); } }; //------------------------------------------------------------------------------------------------------------------- // Instance //------------------------------------------------------------------------------------------------------------------- private final String inputStreamCharset, fileCharset; /** * Constructor. * * @param ps The property store containing all the settings for this object. * @param consumes The list of media types that this parser consumes (e.g. "application/json", "*​/json"). */ protected ReaderParser(PropertyStore ps, String...consumes) { super(ps, consumes); inputStreamCharset = getStringProperty(RPARSER_inputStreamCharset, "UTF-8"); fileCharset = getStringProperty(RPARSER_fileCharset, "DEFAULT"); } @Override /* Parser */ public final boolean isReaderParser() { return true; } //----------------------------------------------------------------------------------------------------------------- // Properties //----------------------------------------------------------------------------------------------------------------- /** * Configuration property: Input stream charset. * * @see #RPARSER_inputStreamCharset * @return * The character set to use for converting InputStreams and byte arrays to readers. */ protected final String getInputStreamCharset() { return inputStreamCharset; } /** * Configuration property: File charset. * * @see #RPARSER_fileCharset * @return * The character set to use for reading Files from the file system. */ protected final String getFileCharset() { return fileCharset; } @Override /* Context */ public ObjectMap asMap() { return super.asMap() .append("ReaderParser", new ObjectMap() .append("inputStreamCharset", inputStreamCharset) .append("fileCharset", fileCharset) ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy