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 org.apache.juneau.*; 016 017/** 018 * Subclass of {@link Parser} for byte-based parsers. 019 * 020 * <h5 class='topic'>Description</h5> 021 * 022 * This class is typically the parent class of all byte-based parsers. 023 * It has 1 abstract method to implement... 024 * <ul> 025 * <li><code>parse(InputStream, ClassMeta, Parser)</code> 026 * </ul> 027 */ 028public abstract class InputStreamParser extends Parser { 029 030 //------------------------------------------------------------------------------------------------------------------- 031 // Configurable properties 032 //------------------------------------------------------------------------------------------------------------------- 033 034 private static final String PREFIX = "InputStreamParser."; 035 036 037 /** 038 * Configuration property: Binary input format. 039 * 040 * <h5 class='section'>Property:</h5> 041 * <ul> 042 * <li><b>Name:</b> <js>"InputStreamParser.binaryFormat.s"</js> 043 * <li><b>Data type:</b> {@link BinaryFormat} 044 * <li><b>Default:</b> {@link BinaryFormat#HEX} 045 * <li><b>Session property:</b> <jk>false</jk> 046 * <li><b>Methods:</b> 047 * <ul> 048 * <li class='jm'>{@link InputStreamParserBuilder#binaryFormat(BinaryFormat)} 049 * </ul> 050 * </ul> 051 * 052 * <h5 class='section'>Description:</h5> 053 * <p> 054 * When using the {@link #parse(Object,Class)} method on stream-based parsers and the input is a string, this defines the format to use 055 * when converting the string into a byte array. 056 * 057 * 058 * <h5 class='section'>Example:</h5> 059 * <p class='bcode w800'> 060 * <jc>// Create a parser that parses from BASE64.</jc> 061 * InputStreamParser p = MsgPackParser 062 * .<jsm>create</jsm>() 063 * .binaryFormat(<jsf>BASE64</jsf>) 064 * .build(); 065 * 066 * <jc>// Same, but use property.</jc> 067 * InputStreamParser p = MsgPackParser 068 * .<jsm>create</jsm>() 069 * .set(<jsf>ISPARSER_binaryFormat</jsf>, <js>"BASE64"</js>) 070 * .build(); 071 * </p> 072 */ 073 public static final String ISPARSER_binaryFormat = PREFIX + "binaryFormat.s"; 074 075 static final InputStreamParser DEFAULT = new InputStreamParser(PropertyStore.create().build(), "") { 076 @Override 077 public InputStreamParserSession createSession(ParserSessionArgs args) { 078 throw new NoSuchMethodError(); 079 } 080 }; 081 082 //------------------------------------------------------------------------------------------------------------------- 083 // Instance 084 //------------------------------------------------------------------------------------------------------------------- 085 086 private final BinaryFormat binaryFormat; 087 088 /** 089 * Constructor. 090 * 091 * @param ps The property store containing all the settings for this object. 092 * @param consumes The list of media types that this parser consumes (e.g. <js>"application/json"</js>). 093 */ 094 protected InputStreamParser(PropertyStore ps, String...consumes) { 095 super(ps, consumes); 096 binaryFormat = getProperty(ISPARSER_binaryFormat, BinaryFormat.class, BinaryFormat.HEX); 097 } 098 099 @Override /* Parser */ 100 public final boolean isReaderParser() { 101 return false; 102 } 103 104 //----------------------------------------------------------------------------------------------------------------- 105 // Properties 106 //----------------------------------------------------------------------------------------------------------------- 107 108 /** 109 * Configuration property: Binary input format. 110 * 111 * @see #ISPARSER_binaryFormat 112 * @return 113 * The format to use when converting strings to byte arrays. 114 */ 115 protected final BinaryFormat getBinaryFormat() { 116 return binaryFormat; 117 } 118 119 @Override /* Context */ 120 public ObjectMap asMap() { 121 return super.asMap() 122 .append("InputStreamParser", new ObjectMap() 123 .append("binaryFormat", binaryFormat) 124 ); 125 } 126}