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;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021
022import java.util.*;
023
024import org.apache.juneau.*;
025import org.apache.juneau.xml.annotation.*;
026
027/**
028 * Metadata on classes specific to the XML serializers and parsers pulled from the {@link Xml @Xml} annotation on the
029 * class.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlBasics">XML Basics</a>
033 * </ul>
034 */
035public class XmlClassMeta extends ExtendedClassMeta {
036
037   private final Namespace namespace;
038   private final XmlFormat format;
039   private final String childName;
040
041   /**
042    * Constructor.
043    *
044    * @param cm The class that this annotation is defined on.
045    * @param mp XML metadata provider (for finding information about other artifacts).
046    */
047   public XmlClassMeta(ClassMeta<?> cm, XmlMetaProvider mp) {
048      super(cm);
049      List<Xml> xmls = list();
050      List<XmlSchema> schemas = list();
051      if (nn(cm)) {
052         cm.forEachAnnotation(Xml.class, x -> true, x -> xmls.add(x));
053         cm.forEachAnnotation(XmlSchema.class, x -> true, x -> schemas.add(x));
054      }
055      this.namespace = XmlUtils.findNamespace(xmls, schemas);
056
057      String _childName = null;
058      var _format = XmlFormat.DEFAULT;
059      for (var a : xmls) {
060         if (a.format() != XmlFormat.DEFAULT)
061            _format = a.format();
062         if (! a.childName().isEmpty())
063            _childName = a.childName();
064      }
065      this.format = _format;
066      this.childName = _childName;
067   }
068
069   /**
070    * Returns the XML namespace associated with this class.
071    *
072    * <p>
073    * Namespace is determined in the following order of {@link Xml#prefix() @Xml(prefix)} annotation:
074    * <ol>
075    *    <li>Class.
076    *    <li>Package.
077    *    <li>Superclasses.
078    *    <li>Superclass packages.
079    *    <li>Interfaces.
080    *    <li>Interface packages.
081    * </ol>
082    *
083    * @return The namespace associated with this class, or <jk>null</jk> if no namespace is associated with it.
084    */
085   public Namespace getNamespace() { return namespace; }
086
087   /**
088    * Returns the {@link Xml#childName() @Xml(childName)} annotation defined on the class.
089    *
090    * @return The value of the annotation, or <jk>null</jk> if not specified.
091    */
092   protected String getChildName() { return childName; }
093
094   /**
095    * Returns the {@link Xml#format() @Xml(format)} annotation defined on the class.
096    *
097    * @return The value of the annotation, or {@link XmlFormat#DEFAULT} if not specified.
098    */
099   protected XmlFormat getFormat() { return format; }
100}