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