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