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.xml.annotation;
018
019import org.apache.juneau.*;
020import org.apache.juneau.commons.reflect.*;
021import org.apache.juneau.svl.*;
022import org.apache.juneau.xml.*;
023
024/**
025 * Utility classes and methods for the {@link XmlConfig @XmlConfig} annotation.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlBasics">XML Basics</a>
029 * </ul>
030 */
031public class XmlConfigAnnotation {
032
033   /**
034    * Applies {@link XmlConfig} annotations to a {@link org.apache.juneau.xml.XmlParser.Builder}.
035    */
036   public static class ParserApply extends AnnotationApplier<XmlConfig,XmlParser.Builder> {
037
038      /**
039       * Constructor.
040       *
041       * @param vr The resolver for resolving values in annotations.
042       */
043      public ParserApply(VarResolverSession vr) {
044         super(XmlConfig.class, XmlParser.Builder.class, vr);
045      }
046
047      @Override
048      public void apply(AnnotationInfo<XmlConfig> ai, XmlParser.Builder b) {
049         XmlConfig a = ai.inner();
050
051         type(a.eventAllocator()).ifPresent(x -> b.eventAllocator(x));
052         bool(a.preserveRootElement()).ifPresent(x -> b.preserveRootElement(x));
053         type(a.reporter()).ifPresent(x -> b.reporter(x));
054         type(a.resolver()).ifPresent(x -> b.resolver(x));
055         bool(a.validating()).ifPresent(x -> b.validating(x));
056      }
057   }
058
059   /**
060    * Applies {@link XmlConfig} annotations to a {@link org.apache.juneau.xml.XmlSerializer.Builder}.
061    */
062   public static class SerializerApply extends AnnotationApplier<XmlConfig,XmlSerializer.Builder> {
063
064      /**
065       * Constructor.
066       *
067       * @param vr The resolver for resolving values in annotations.
068       */
069      public SerializerApply(VarResolverSession vr) {
070         super(XmlConfig.class, XmlSerializer.Builder.class, vr);
071      }
072
073      @Override
074      public void apply(AnnotationInfo<XmlConfig> ai, XmlSerializer.Builder b) {
075         XmlConfig a = ai.inner();
076
077         bool(a.addBeanTypes()).ifPresent(x -> b.addBeanTypesXml(x));
078         bool(a.addNamespaceUrisToRoot()).ifPresent(x -> b.addNamespaceUrisToRoot(x));
079         bool(a.disableAutoDetectNamespaces()).ifPresent(x -> b.disableAutoDetectNamespaces(x));
080         string(a.defaultNamespace()).map(Namespace::create).ifPresent(x -> b.defaultNamespace(x));
081         bool(a.enableNamespaces()).ifPresent(x -> b.enableNamespaces(x));
082         strings(a.namespaces()).map(Namespace::createArray).ifPresent(x -> b.namespaces(x));
083      }
084   }
085}