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 static org.apache.juneau.collections.JsonMap.*;
016import java.lang.annotation.*;
017import java.lang.reflect.*;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.collections.*;
022import org.apache.juneau.internal.*;
023import org.apache.juneau.utils.*;
024
025/**
026 * Subclass of {@link Parser} for byte-based parsers.
027 *
028 * <h5 class='topic'>Description</h5>
029 * <p>
030 * This class is typically the parent class of all character-based parsers.
031 * It has 1 abstract method to implement on the session object...
032 * <ul>
033 *    <li><c>parse(ParserSession, ClassMeta)</c>
034 * </ul>
035 *
036 * <h5 class='section'>Notes:</h5><ul>
037 *    <li class='note'>This class is thread safe and reusable.
038 * </ul>
039 *
040 * <h5 class='section'>See Also:</h5><ul>
041 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a>
042
043 * </ul>
044 */
045public class InputStreamParser extends Parser {
046
047   //-------------------------------------------------------------------------------------------------------------------
048   // Static
049   //-------------------------------------------------------------------------------------------------------------------
050
051   /**
052    * Creates a new builder for this object.
053    *
054    * @return A new builder.
055    */
056   public static Builder create() {
057      return new Builder();
058   }
059
060   //-------------------------------------------------------------------------------------------------------------------
061   // Builder
062   //-------------------------------------------------------------------------------------------------------------------
063
064   /**
065    * Builder class.
066    */
067   @FluentSetters
068   public static class Builder extends Parser.Builder {
069
070      BinaryFormat binaryFormat;
071
072      /**
073       * Constructor, default settings.
074       */
075      protected Builder() {
076         binaryFormat = env("InputStreamParser.binaryFormat", BinaryFormat.HEX);
077      }
078
079      /**
080       * Copy constructor.
081       *
082       * @param copyFrom The bean to copy from.
083       */
084      protected Builder(InputStreamParser copyFrom) {
085         super(copyFrom);
086         binaryFormat = copyFrom.binaryFormat;
087      }
088
089      /**
090       * Copy constructor.
091       *
092       * @param copyFrom The builder to copy from.
093       */
094      protected Builder(Builder copyFrom) {
095         super(copyFrom);
096         binaryFormat = copyFrom.binaryFormat;
097      }
098
099      @Override /* Context.Builder */
100      public Builder copy() {
101         return new Builder(this);
102      }
103
104      @Override /* Context.Builder */
105      public InputStreamParser build() {
106         return build(InputStreamParser.class);
107      }
108
109      @Override /* Context.Builder */
110      public HashKey hashKey() {
111         return HashKey.of(
112            super.hashKey(),
113            binaryFormat
114         );
115      }
116
117      //-----------------------------------------------------------------------------------------------------------------
118      // Properties
119      //-----------------------------------------------------------------------------------------------------------------
120
121      /**
122       * <i><l>InputStreamParser</l> configuration property:&emsp;</i>  Binary input format.
123       *
124       * <p>
125       * When using the {@link InputStreamParser#parse(Object,Class)} method on stream-based parsers and the input is a string, this defines the format to use
126       * when converting the string into a byte array.
127       *
128       * <h5 class='section'>Example:</h5>
129       * <p class='bjava'>
130       *    <jc>// Create a parser that parses from BASE64.</jc>
131       *    InputStreamParser <jv>parser</jv> = MsgPackParser
132       *       .<jsm>create</jsm>()
133       *       .binaryFormat(<jsf>BASE64</jsf>)
134       *       .build();
135       *
136       *    String <jv>input</jv> = <js>"base64-encoded-string"</js>;
137       *
138       *    MyBean <jv>myBean</jv> = <jv>parser</jv>.parse(<jv>input</jv> , MyBean.<jk>class</jk>);
139       * </p>
140       *
141       * @param value
142       *    The new value for this property.
143       *    <br>The default value is {@link BinaryFormat#HEX}.
144       * @return This object.
145       */
146      @FluentSetter
147      public Builder binaryFormat(BinaryFormat value) {
148         binaryFormat = value;
149         return this;
150      }
151
152      // <FluentSetters>
153
154      @Override /* GENERATED - org.apache.juneau.Context.Builder */
155      public Builder annotations(Annotation...values) {
156         super.annotations(values);
157         return this;
158      }
159
160      @Override /* GENERATED - org.apache.juneau.Context.Builder */
161      public Builder apply(AnnotationWorkList work) {
162         super.apply(work);
163         return this;
164      }
165
166      @Override /* GENERATED - org.apache.juneau.Context.Builder */
167      public Builder applyAnnotations(java.lang.Class<?>...fromClasses) {
168         super.applyAnnotations(fromClasses);
169         return this;
170      }
171
172      @Override /* GENERATED - org.apache.juneau.Context.Builder */
173      public Builder applyAnnotations(Method...fromMethods) {
174         super.applyAnnotations(fromMethods);
175         return this;
176      }
177
178      @Override /* GENERATED - org.apache.juneau.Context.Builder */
179      public Builder cache(Cache<HashKey,? extends org.apache.juneau.Context> value) {
180         super.cache(value);
181         return this;
182      }
183
184      @Override /* GENERATED - org.apache.juneau.Context.Builder */
185      public Builder debug() {
186         super.debug();
187         return this;
188      }
189
190      @Override /* GENERATED - org.apache.juneau.Context.Builder */
191      public Builder debug(boolean value) {
192         super.debug(value);
193         return this;
194      }
195
196      @Override /* GENERATED - org.apache.juneau.Context.Builder */
197      public Builder impl(Context value) {
198         super.impl(value);
199         return this;
200      }
201
202      @Override /* GENERATED - org.apache.juneau.Context.Builder */
203      public Builder type(Class<? extends org.apache.juneau.Context> value) {
204         super.type(value);
205         return this;
206      }
207
208      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
209      public Builder beanClassVisibility(Visibility value) {
210         super.beanClassVisibility(value);
211         return this;
212      }
213
214      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
215      public Builder beanConstructorVisibility(Visibility value) {
216         super.beanConstructorVisibility(value);
217         return this;
218      }
219
220      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
221      public Builder beanContext(BeanContext value) {
222         super.beanContext(value);
223         return this;
224      }
225
226      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
227      public Builder beanContext(BeanContext.Builder value) {
228         super.beanContext(value);
229         return this;
230      }
231
232      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
233      public Builder beanDictionary(java.lang.Class<?>...values) {
234         super.beanDictionary(values);
235         return this;
236      }
237
238      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
239      public Builder beanFieldVisibility(Visibility value) {
240         super.beanFieldVisibility(value);
241         return this;
242      }
243
244      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
245      public Builder beanInterceptor(Class<?> on, Class<? extends org.apache.juneau.swap.BeanInterceptor<?>> value) {
246         super.beanInterceptor(on, value);
247         return this;
248      }
249
250      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
251      public Builder beanMapPutReturnsOldValue() {
252         super.beanMapPutReturnsOldValue();
253         return this;
254      }
255
256      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
257      public Builder beanMethodVisibility(Visibility value) {
258         super.beanMethodVisibility(value);
259         return this;
260      }
261
262      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
263      public Builder beanProperties(Map<String,Object> values) {
264         super.beanProperties(values);
265         return this;
266      }
267
268      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
269      public Builder beanProperties(Class<?> beanClass, String properties) {
270         super.beanProperties(beanClass, properties);
271         return this;
272      }
273
274      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
275      public Builder beanProperties(String beanClassName, String properties) {
276         super.beanProperties(beanClassName, properties);
277         return this;
278      }
279
280      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
281      public Builder beanPropertiesExcludes(Map<String,Object> values) {
282         super.beanPropertiesExcludes(values);
283         return this;
284      }
285
286      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
287      public Builder beanPropertiesExcludes(Class<?> beanClass, String properties) {
288         super.beanPropertiesExcludes(beanClass, properties);
289         return this;
290      }
291
292      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
293      public Builder beanPropertiesExcludes(String beanClassName, String properties) {
294         super.beanPropertiesExcludes(beanClassName, properties);
295         return this;
296      }
297
298      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
299      public Builder beanPropertiesReadOnly(Map<String,Object> values) {
300         super.beanPropertiesReadOnly(values);
301         return this;
302      }
303
304      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
305      public Builder beanPropertiesReadOnly(Class<?> beanClass, String properties) {
306         super.beanPropertiesReadOnly(beanClass, properties);
307         return this;
308      }
309
310      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
311      public Builder beanPropertiesReadOnly(String beanClassName, String properties) {
312         super.beanPropertiesReadOnly(beanClassName, properties);
313         return this;
314      }
315
316      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
317      public Builder beanPropertiesWriteOnly(Map<String,Object> values) {
318         super.beanPropertiesWriteOnly(values);
319         return this;
320      }
321
322      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
323      public Builder beanPropertiesWriteOnly(Class<?> beanClass, String properties) {
324         super.beanPropertiesWriteOnly(beanClass, properties);
325         return this;
326      }
327
328      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
329      public Builder beanPropertiesWriteOnly(String beanClassName, String properties) {
330         super.beanPropertiesWriteOnly(beanClassName, properties);
331         return this;
332      }
333
334      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
335      public Builder beansRequireDefaultConstructor() {
336         super.beansRequireDefaultConstructor();
337         return this;
338      }
339
340      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
341      public Builder beansRequireSerializable() {
342         super.beansRequireSerializable();
343         return this;
344      }
345
346      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
347      public Builder beansRequireSettersForGetters() {
348         super.beansRequireSettersForGetters();
349         return this;
350      }
351
352      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
353      public Builder dictionaryOn(Class<?> on, java.lang.Class<?>...values) {
354         super.dictionaryOn(on, values);
355         return this;
356      }
357
358      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
359      public Builder disableBeansRequireSomeProperties() {
360         super.disableBeansRequireSomeProperties();
361         return this;
362      }
363
364      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
365      public Builder disableIgnoreMissingSetters() {
366         super.disableIgnoreMissingSetters();
367         return this;
368      }
369
370      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
371      public Builder disableIgnoreTransientFields() {
372         super.disableIgnoreTransientFields();
373         return this;
374      }
375
376      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
377      public Builder disableIgnoreUnknownNullBeanProperties() {
378         super.disableIgnoreUnknownNullBeanProperties();
379         return this;
380      }
381
382      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
383      public Builder disableInterfaceProxies() {
384         super.disableInterfaceProxies();
385         return this;
386      }
387
388      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
389      public <T> Builder example(Class<T> pojoClass, T o) {
390         super.example(pojoClass, o);
391         return this;
392      }
393
394      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
395      public <T> Builder example(Class<T> pojoClass, String json) {
396         super.example(pojoClass, json);
397         return this;
398      }
399
400      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
401      public Builder findFluentSetters() {
402         super.findFluentSetters();
403         return this;
404      }
405
406      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
407      public Builder findFluentSetters(Class<?> on) {
408         super.findFluentSetters(on);
409         return this;
410      }
411
412      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
413      public Builder ignoreInvocationExceptionsOnGetters() {
414         super.ignoreInvocationExceptionsOnGetters();
415         return this;
416      }
417
418      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
419      public Builder ignoreInvocationExceptionsOnSetters() {
420         super.ignoreInvocationExceptionsOnSetters();
421         return this;
422      }
423
424      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
425      public Builder ignoreUnknownBeanProperties() {
426         super.ignoreUnknownBeanProperties();
427         return this;
428      }
429
430      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
431      public Builder ignoreUnknownEnumValues() {
432         super.ignoreUnknownEnumValues();
433         return this;
434      }
435
436      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
437      public Builder implClass(Class<?> interfaceClass, Class<?> implClass) {
438         super.implClass(interfaceClass, implClass);
439         return this;
440      }
441
442      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
443      public Builder implClasses(Map<Class<?>,Class<?>> values) {
444         super.implClasses(values);
445         return this;
446      }
447
448      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
449      public Builder interfaceClass(Class<?> on, Class<?> value) {
450         super.interfaceClass(on, value);
451         return this;
452      }
453
454      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
455      public Builder interfaces(java.lang.Class<?>...value) {
456         super.interfaces(value);
457         return this;
458      }
459
460      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
461      public Builder locale(Locale value) {
462         super.locale(value);
463         return this;
464      }
465
466      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
467      public Builder mediaType(MediaType value) {
468         super.mediaType(value);
469         return this;
470      }
471
472      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
473      public Builder notBeanClasses(java.lang.Class<?>...values) {
474         super.notBeanClasses(values);
475         return this;
476      }
477
478      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
479      public Builder notBeanPackages(String...values) {
480         super.notBeanPackages(values);
481         return this;
482      }
483
484      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
485      public Builder propertyNamer(Class<? extends org.apache.juneau.PropertyNamer> value) {
486         super.propertyNamer(value);
487         return this;
488      }
489
490      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
491      public Builder propertyNamer(Class<?> on, Class<? extends org.apache.juneau.PropertyNamer> value) {
492         super.propertyNamer(on, value);
493         return this;
494      }
495
496      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
497      public Builder sortProperties() {
498         super.sortProperties();
499         return this;
500      }
501
502      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
503      public Builder sortProperties(java.lang.Class<?>...on) {
504         super.sortProperties(on);
505         return this;
506      }
507
508      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
509      public Builder stopClass(Class<?> on, Class<?> value) {
510         super.stopClass(on, value);
511         return this;
512      }
513
514      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
515      public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction) {
516         super.swap(normalClass, swappedClass, swapFunction);
517         return this;
518      }
519
520      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
521      public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction) {
522         super.swap(normalClass, swappedClass, swapFunction, unswapFunction);
523         return this;
524      }
525
526      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
527      public Builder swaps(java.lang.Class<?>...values) {
528         super.swaps(values);
529         return this;
530      }
531
532      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
533      public Builder timeZone(TimeZone value) {
534         super.timeZone(value);
535         return this;
536      }
537
538      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
539      public Builder typeName(Class<?> on, String value) {
540         super.typeName(on, value);
541         return this;
542      }
543
544      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
545      public Builder typePropertyName(String value) {
546         super.typePropertyName(value);
547         return this;
548      }
549
550      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
551      public Builder typePropertyName(Class<?> on, String value) {
552         super.typePropertyName(on, value);
553         return this;
554      }
555
556      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
557      public Builder useEnumNames() {
558         super.useEnumNames();
559         return this;
560      }
561
562      @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */
563      public Builder useJavaBeanIntrospector() {
564         super.useJavaBeanIntrospector();
565         return this;
566      }
567
568      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
569      public Builder autoCloseStreams() {
570         super.autoCloseStreams();
571         return this;
572      }
573
574      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
575      public Builder autoCloseStreams(boolean value) {
576         super.autoCloseStreams(value);
577         return this;
578      }
579
580      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
581      public Builder consumes(String value) {
582         super.consumes(value);
583         return this;
584      }
585
586      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
587      public Builder debugOutputLines(int value) {
588         super.debugOutputLines(value);
589         return this;
590      }
591
592      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
593      public Builder listener(Class<? extends org.apache.juneau.parser.ParserListener> value) {
594         super.listener(value);
595         return this;
596      }
597
598      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
599      public Builder strict() {
600         super.strict();
601         return this;
602      }
603
604      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
605      public Builder strict(boolean value) {
606         super.strict(value);
607         return this;
608      }
609
610      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
611      public Builder trimStrings() {
612         super.trimStrings();
613         return this;
614      }
615
616      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
617      public Builder trimStrings(boolean value) {
618         super.trimStrings(value);
619         return this;
620      }
621
622      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
623      public Builder unbuffered() {
624         super.unbuffered();
625         return this;
626      }
627
628      @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */
629      public Builder unbuffered(boolean value) {
630         super.unbuffered(value);
631         return this;
632      }
633
634      // </FluentSetters>
635   }
636
637   //-------------------------------------------------------------------------------------------------------------------
638   // Instance
639   //-------------------------------------------------------------------------------------------------------------------
640
641   final BinaryFormat binaryFormat;
642
643   /**
644    * Constructor.
645    *
646    * @param builder The builder for this object.
647    */
648   protected InputStreamParser(Builder builder) {
649      super(builder);
650      binaryFormat = builder.binaryFormat;
651   }
652
653   @Override /* Parser */
654   public final boolean isReaderParser() {
655      return false;
656   }
657
658   @Override /* Context */
659   public InputStreamParserSession.Builder createSession() {
660      return InputStreamParserSession.create(this);
661   }
662
663   @Override /* Context */
664   public InputStreamParserSession getSession() {
665      return createSession().build();
666   }
667
668   //-----------------------------------------------------------------------------------------------------------------
669   // Properties
670   //-----------------------------------------------------------------------------------------------------------------
671
672   /**
673    * Binary input format.
674    *
675    * @see Builder#binaryFormat(BinaryFormat)
676    * @return
677    *    The format to use when converting strings to byte arrays.
678    */
679   protected final BinaryFormat getBinaryFormat() {
680      return binaryFormat;
681   }
682
683   //-----------------------------------------------------------------------------------------------------------------
684   // Other methods
685   //-----------------------------------------------------------------------------------------------------------------
686
687   @Override /* Context */
688   protected JsonMap properties() {
689      return filteredMap("binaryFormat", binaryFormat);
690   }
691}