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