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