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 * <h2 class='section'>See Also:</h2><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.includeInputInErrors()).ifPresent(b::includeInputInErrors); 088 bool(a.unbuffered()).ifPresent(b::unbuffered); 089 string(a.nulls()).map(Nulls::valueOf).ifPresent(b::nulls); 090 } 091 } 092 093 /** 094 * Applies {@link ParserConfig} annotations to a {@link ReaderParser.Builder}. 095 */ 096 public static class ReaderParserApply extends AnnotationApplier<ParserConfig,ReaderParser.Builder> { 097 098 /** 099 * Constructor. 100 * 101 * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>. 102 */ 103 public ReaderParserApply(VarResolverSession vr) { 104 super(ParserConfig.class, ReaderParser.Builder.class, vr); 105 } 106 107 @Override 108 public void apply(AnnotationInfo<ParserConfig> ai, ReaderParser.Builder b) { 109 ParserConfig a = ai.inner(); 110 111 charset(a.streamCharset()).ifPresent(b::streamCharset); 112 } 113 } 114}