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.annotation;
014
015import static org.apache.juneau.parser.Parser.*;
016import static org.apache.juneau.parser.ReaderParser.*;
017
018import java.nio.charset.*;
019
020import static org.apache.juneau.parser.InputStreamParser.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.parser.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Applies {@link ParserConfig} annotations to a {@link PropertyStoreBuilder}.
029 */
030public class ParserConfigApply extends ConfigApply<ParserConfig> {
031
032   /**
033    * Constructor.
034    *
035    * @param c The annotation class.
036    * @param r The resolver for resolving values in annotations.
037    */
038   public ParserConfigApply(Class<ParserConfig> c, VarResolverSession r) {
039      super(c, r);
040   }
041
042   @Override
043   public void apply(AnnotationInfo<ParserConfig> ai, PropertyStoreBuilder psb) {
044      ParserConfig a = ai.getAnnotation();
045      if (! a.autoCloseStreams().isEmpty())
046         psb.set(PARSER_autoCloseStreams, bool(a.autoCloseStreams()));
047      if (! a.debugOutputLines().isEmpty())
048         psb.set(PARSER_debugOutputLines, integer(a.debugOutputLines(), "debugOutputLines"));
049      if (a.listener() != ParserListener.Null.class)
050         psb.set(PARSER_listener, a.listener());
051      if (! a.strict().isEmpty())
052         psb.set(PARSER_strict, bool(a.strict()));
053      if (! a.trimStrings().isEmpty())
054         psb.set(PARSER_trimStrings, bool(a.trimStrings()));
055      if (! a.unbuffered().isEmpty())
056         psb.set(PARSER_unbuffered, bool(a.unbuffered()));
057
058      if (! a.binaryFormat().isEmpty())
059         psb.set(ISPARSER_binaryFormat, string(a.binaryFormat()));
060
061      if (! a.fileCharset().isEmpty())
062         psb.set(RPARSER_fileCharset, charset(a.fileCharset()));
063      if (! a.streamCharset().isEmpty())
064         psb.set(RPARSER_streamCharset, charset(a.streamCharset()));
065   }
066   
067   private Object charset(String in) {
068      String s = string(in);
069      if ("default".equalsIgnoreCase(s))
070         return Charset.defaultCharset();
071      return s;
072   }
073}