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.xml;
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 XmlConfig @XmlConfig} annotation.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlSupport">XML</a>
028 * </ul>
029 */
030@SuppressWarnings({
031   "rawtypes" // Raw types required for reflective annotation application.
032})
033public class XmlConfigAnnotation {
034
035   /**
036    * Prevents instantiation.
037    */
038   private XmlConfigAnnotation() {}
039
040   /**
041    * Applies {@link XmlConfig} annotations to a {@link XmlParser.Builder}.
042    */
043   @SuppressWarnings({
044      "unchecked" // Raw types required for reflective annotation application.
045   })
046   public static class ParserApply extends AnnotationApplier<XmlConfig,XmlParser.Builder> {
047
048      /**
049       * Constructor.
050       *
051       * @param vr The resolver for resolving values in annotations.
052       *    <br>Must not be <jk>null</jk>.
053       */
054      public ParserApply(VarResolverSession vr) {
055         super(XmlConfig.class, XmlParser.Builder.class, vr);
056      }
057
058      @Override
059      public void apply(AnnotationInfo<XmlConfig> ai, XmlParser.Builder b) {
060         XmlConfig a = ai.inner();
061
062         type(a.eventAllocator()).ifPresent(b::eventAllocator);
063         bool(a.preserveRootElement()).ifPresent(b::preserveRootElement);
064         type(a.reporter()).ifPresent(b::reporter);
065         type(a.resolver()).ifPresent(b::resolver);
066         bool(a.validating()).ifPresent(b::validating);
067      }
068   }
069
070   /**
071    * Applies {@link XmlConfig} annotations to a {@link XmlSerializer.Builder}.
072    */
073   public static class SerializerApply extends AnnotationApplier<XmlConfig,XmlSerializer.Builder> {
074
075      /**
076       * Constructor.
077       *
078       * @param vr The resolver for resolving values in annotations.
079       *    <br>Must not be <jk>null</jk>.
080       */
081      public SerializerApply(VarResolverSession vr) {
082         super(XmlConfig.class, XmlSerializer.Builder.class, vr);
083      }
084
085      @Override
086      public void apply(AnnotationInfo<XmlConfig> ai, XmlSerializer.Builder b) {
087         XmlConfig a = ai.inner();
088
089         bool(a.addBeanTypes()).ifPresent(b::addBeanTypesXml);
090         bool(a.addNamespaceUrisToRoot()).ifPresent(b::addNamespaceUrisToRoot);
091         bool(a.disableAutoDetectNamespaces()).ifPresent(b::disableAutoDetectNamespaces);
092         string(a.defaultNamespace()).map(Namespace::create).ifPresent(b::defaultNamespace);
093         bool(a.enableNamespaces()).ifPresent(b::enableNamespaces);
094         strings(a.namespaces()).map(Namespace::createArray).ifPresent(b::namespaces);
095      }
096   }
097}