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.StringUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.reflect.*;
021import org.apache.juneau.xml.annotation.*;
022
023/**
024 * Metadata on classes specific to the XML serializers and parsers pulled from the {@link Xml @Xml} annotation on the
025 * class.
026 */
027public class XmlClassMeta extends ClassMetaExtended {
028
029   private final Namespace namespace;
030   private final Xml xml;
031   private final XmlFormat format;
032   private final String childName;
033
034   /**
035    * Constructor.
036    *
037    * @param cm The class that this annotation is defined on.
038    */
039   public XmlClassMeta(ClassMeta<?> cm) {
040      super(cm);
041      this.namespace = findNamespace(cm);
042      this.xml = cm.getInfo().getAnnotation(Xml.class);
043      if (xml != null) {
044         this.format = xml.format();
045         this.childName = nullIfEmpty(xml.childName());
046
047      } else {
048         this.format = XmlFormat.DEFAULT;
049         this.childName = null;
050      }
051   }
052
053   /**
054    * Returns the {@link Xml @Xml} annotation defined on the class.
055    *
056    * @return
057    *    The value of the annotation defined on the class, or <jk>null</jk> if annotation is not specified.
058    */
059   protected Xml getAnnotation() {
060      return xml;
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) {
102      if (cm == null)
103         return null;
104      ClassInfo ci = cm.getInfo();
105      List<Xml> xmls = ci.getAnnotations(Xml.class);
106      List<XmlSchema> schemas = ci.getAnnotations(XmlSchema.class);
107      return XmlUtils.findNamespace(xmls, schemas);
108   }
109}