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.serializer.annotation;
014
015import static org.apache.juneau.serializer.Serializer.*;
016import static org.apache.juneau.serializer.OutputStreamSerializer.*;
017import static org.apache.juneau.serializer.WriterSerializer.*;
018
019import java.nio.charset.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.reflect.*;
023import org.apache.juneau.serializer.*;
024import org.apache.juneau.svl.*;
025
026/**
027 * Applies {@link SerializerConfig} annotations to a {@link PropertyStoreBuilder}.
028 */
029public class SerializerConfigApply extends ConfigApply<SerializerConfig> {
030
031   /**
032    * Constructor.
033    *
034    * @param c The annotation class.
035    * @param r The resolver for resolving values in annotations.
036    */
037   public SerializerConfigApply(Class<SerializerConfig> c, VarResolverSession r) {
038      super(c, r);
039   }
040
041   @Override
042   public void apply(AnnotationInfo<SerializerConfig> ai, PropertyStoreBuilder psb) {
043      SerializerConfig a = ai.getAnnotation();
044      if (! a.addBeanTypes().isEmpty())
045         psb.set(SERIALIZER_addBeanTypes, bool(a.addBeanTypes()));
046      if (! a.addRootType().isEmpty())
047         psb.set(SERIALIZER_addRootType, bool(a.addRootType()));
048      if (a.listener() != SerializerListener.Null.class)
049         psb.set(SERIALIZER_listener, a.listener());
050      if (! a.sortCollections().isEmpty())
051         psb.set(SERIALIZER_sortCollections, bool(a.sortCollections()));
052      if (! a.sortMaps().isEmpty())
053         psb.set(SERIALIZER_sortMaps, bool(a.sortMaps()));
054      if (! a.trimEmptyCollections().isEmpty())
055         psb.set(SERIALIZER_trimEmptyCollections, bool(a.trimEmptyCollections()));
056      if (! a.trimEmptyMaps().isEmpty())
057         psb.set(SERIALIZER_trimEmptyMaps, bool(a.trimEmptyMaps()));
058      if (! a.trimNullProperties().isEmpty())
059         psb.set(SERIALIZER_trimNullProperties, bool(a.trimNullProperties()));
060      if (! a.trimStrings().isEmpty())
061         psb.set(SERIALIZER_trimStrings, bool(a.trimStrings()));
062      if (! a.uriContext().isEmpty())
063         psb.set(SERIALIZER_uriContext, string(a.uriContext()));
064      if (! a.uriRelativity().isEmpty())
065         psb.set(SERIALIZER_uriRelativity, string(a.uriRelativity()));
066      if (! a.uriResolution().isEmpty())
067         psb.set(SERIALIZER_uriResolution, string(a.uriResolution()));
068
069      if (! a.binaryFormat().isEmpty())
070         psb.set(OSSERIALIZER_binaryFormat, string(a.binaryFormat()));
071
072      if (! a.fileCharset().isEmpty())
073         psb.set(WSERIALIZER_fileCharset, charset(a.fileCharset()));
074      if (! a.maxIndent().isEmpty())
075         psb.set(WSERIALIZER_maxIndent, integer(a.maxIndent(), "maxIndent"));
076      if (! a.quoteChar().isEmpty())
077         psb.set(WSERIALIZER_quoteChar, character(a.quoteChar(), "quoteChar"));
078      if (! a.streamCharset().isEmpty())
079         psb.set(WSERIALIZER_streamCharset, charset(a.streamCharset()));
080      if (! a.useWhitespace().isEmpty())
081         psb.set(WSERIALIZER_useWhitespace, bool(a.useWhitespace()));
082   }
083
084   private Object charset(String in) {
085      String s = string(in);
086      if ("default".equalsIgnoreCase(s))
087         return Charset.defaultCharset();
088      return s;
089   }
090
091   private char character(String in, String loc) {
092      String s = string(in);
093      if (s.length() != 1)
094         throw new ConfigException("Invalid syntax for character on annotation @{0}({1}): {2}", "SerializerConfig", loc, in);
095      return s.charAt(0);
096   }
097}