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 @SuppressWarnings("deprecation") 042 @Override 043 public void apply(AnnotationInfo<SerializerConfig> ai, PropertyStoreBuilder psb) { 044 SerializerConfig a = ai.getAnnotation(); 045 if (! a.addBeanTypes().isEmpty()) 046 psb.set(SERIALIZER_addBeanTypes, bool(a.addBeanTypes())); 047 if (! a.addRootType().isEmpty()) 048 psb.set(SERIALIZER_addRootType, bool(a.addRootType())); 049 if (! a.keepNullProperties().isEmpty()) 050 psb.set(SERIALIZER_keepNullProperties, bool(a.keepNullProperties())); 051 if (a.listener() != SerializerListener.Null.class) 052 psb.set(SERIALIZER_listener, a.listener()); 053 if (! a.sortCollections().isEmpty()) 054 psb.set(SERIALIZER_sortCollections, bool(a.sortCollections())); 055 if (! a.sortMaps().isEmpty()) 056 psb.set(SERIALIZER_sortMaps, bool(a.sortMaps())); 057 if (! a.trimEmptyCollections().isEmpty()) 058 psb.set(SERIALIZER_trimEmptyCollections, bool(a.trimEmptyCollections())); 059 if (! a.trimEmptyMaps().isEmpty()) 060 psb.set(SERIALIZER_trimEmptyMaps, bool(a.trimEmptyMaps())); 061 if (! a.trimNullProperties().isEmpty()) 062 psb.set(SERIALIZER_trimNullProperties, bool(a.trimNullProperties())); 063 if (! a.trimStrings().isEmpty()) 064 psb.set(SERIALIZER_trimStrings, bool(a.trimStrings())); 065 if (! a.uriContext().isEmpty()) 066 psb.set(SERIALIZER_uriContext, string(a.uriContext())); 067 if (! a.uriRelativity().isEmpty()) 068 psb.set(SERIALIZER_uriRelativity, string(a.uriRelativity())); 069 if (! a.uriResolution().isEmpty()) 070 psb.set(SERIALIZER_uriResolution, string(a.uriResolution())); 071 072 if (! a.binaryFormat().isEmpty()) 073 psb.set(OSSERIALIZER_binaryFormat, string(a.binaryFormat())); 074 075 if (! a.fileCharset().isEmpty()) 076 psb.set(WSERIALIZER_fileCharset, charset(a.fileCharset())); 077 if (! a.maxIndent().isEmpty()) 078 psb.set(WSERIALIZER_maxIndent, integer(a.maxIndent(), "maxIndent")); 079 if (! a.quoteChar().isEmpty()) 080 psb.set(WSERIALIZER_quoteChar, character(a.quoteChar(), "quoteChar")); 081 if (! a.streamCharset().isEmpty()) 082 psb.set(WSERIALIZER_streamCharset, charset(a.streamCharset())); 083 if (! a.useWhitespace().isEmpty()) 084 psb.set(WSERIALIZER_useWhitespace, bool(a.useWhitespace())); 085 } 086 087 private Object charset(String in) { 088 String s = string(in); 089 if ("default".equalsIgnoreCase(s)) 090 return Charset.defaultCharset(); 091 return s; 092 } 093 094 private char character(String in, String loc) { 095 String s = string(in); 096 if (s.length() != 1) 097 throw new ConfigException("Invalid syntax for character on annotation @{0}({1}): {2}", "SerializerConfig", loc, in); 098 return s.charAt(0); 099 } 100}