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