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