001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.parser;
018
019import org.apache.juneau.commons.reflect.*;
020import org.apache.juneau.commons.svl.*;
021import org.apache.juneau.marshall.*;
022
023/**
024 * Utility classes and methods for the {@link ParserConfig @ParserConfig} annotation.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
028 * </ul>
029 */
030@SuppressWarnings({
031   "rawtypes" // Raw types required for reflective annotation application.
032})
033public class ParserConfigAnnotation {
034
035   /**
036    * Prevents instantiation.
037    */
038   private ParserConfigAnnotation() {}
039
040   /**
041    * Applies {@link ParserConfig} annotations to a {@link InputStreamParser.Builder}.
042    */
043   public static class InputStreamParserApply extends AnnotationApplier<ParserConfig,InputStreamParser.Builder> {
044
045      /**
046       * Constructor.
047       *
048       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
049       */
050      public InputStreamParserApply(VarResolverSession vr) {
051         super(ParserConfig.class, InputStreamParser.Builder.class, vr);
052      }
053
054      @Override
055      public void apply(AnnotationInfo<ParserConfig> ai, InputStreamParser.Builder b) {
056         ParserConfig a = ai.inner();
057
058         string(a.binaryFormat()).map(BinaryFormat::valueOf).ifPresent(b::binaryFormat);
059      }
060   }
061
062   /**
063    * Applies {@link ParserConfig} annotations to a {@link Parser.Builder}.
064    */
065   @SuppressWarnings({
066      "unchecked" // Raw types required for reflective annotation application.
067   })
068   public static class ParserApply extends AnnotationApplier<ParserConfig,Parser.Builder> {
069
070      /**
071       * Constructor.
072       *
073       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
074       */
075      public ParserApply(VarResolverSession vr) {
076         super(ParserConfig.class, Parser.Builder.class, vr);
077      }
078
079      @Override
080      public void apply(AnnotationInfo<ParserConfig> ai, Parser.Builder b) {
081         ParserConfig a = ai.inner();
082
083         bool(a.autoCloseStreams()).ifPresent(b::autoCloseStreams);
084         integer(a.debugOutputLines(), "debugOutputLines").ifPresent(b::debugOutputLines);
085         type(a.listener()).ifPresent(b::listener);
086         bool(a.trimStrings()).ifPresent(b::trimStrings);
087         bool(a.unbuffered()).ifPresent(b::unbuffered);
088         string(a.nulls()).map(Nulls::valueOf).ifPresent(b::nulls);
089      }
090   }
091
092   /**
093    * Applies {@link ParserConfig} annotations to a {@link ReaderParser.Builder}.
094    */
095   public static class ReaderParserApply extends AnnotationApplier<ParserConfig,ReaderParser.Builder> {
096
097      /**
098       * Constructor.
099       *
100       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
101       */
102      public ReaderParserApply(VarResolverSession vr) {
103         super(ParserConfig.class, ReaderParser.Builder.class, vr);
104      }
105
106      @Override
107      public void apply(AnnotationInfo<ParserConfig> ai, ReaderParser.Builder b) {
108         ParserConfig a = ai.inner();
109
110         charset(a.streamCharset()).ifPresent(b::streamCharset);
111      }
112   }
113}