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 bean properties specific to the XML serializers and parsers pulled from the {@link Xml @Xml} annotation
022 * on the bean property.
023 */
024public class XmlBeanPropertyMeta extends ExtendedBeanPropertyMeta {
025
026   /**
027    * Default instance.
028    */
029   public static final XmlBeanPropertyMeta DEFAULT = new XmlBeanPropertyMeta();
030
031   private Namespace namespace = null;
032   private XmlFormat xmlFormat = XmlFormat.DEFAULT;
033   private String childName;
034   private final XmlMetaProvider xmlMetaProvider;
035
036   /**
037    * Constructor.
038    *
039    * @param bpm The metadata of the bean property of this additional metadata.
040    * @param mp XML metadata provider (for finding information about other artifacts).
041    */
042   public XmlBeanPropertyMeta(BeanPropertyMeta bpm, XmlMetaProvider mp) {
043      super(bpm);
044      this.xmlMetaProvider = mp;
045
046      for (Xml xml : bpm.getAnnotations(Xml.class))
047         findXmlInfo(xml, mp);
048
049      if (namespace == null)
050         namespace = mp.getXmlClassMeta(bpm.getBeanMeta().getClassMeta()).getNamespace();
051   }
052
053   private XmlBeanPropertyMeta() {
054      super(null);
055      this.xmlMetaProvider = null;
056   }
057
058   /**
059    * Returns the XML namespace associated with this bean property.
060    *
061    * <p>
062    * Namespace is determined in the following order of {@link Xml#prefix() @Xml(prefix)} annotation:
063    * <ol>
064    *    <li>Bean property field.
065    *    <li>Bean getter.
066    *    <li>Bean setter.
067    *    <li>Bean class.
068    *    <li>Bean package.
069    *    <li>Bean superclasses.
070    *    <li>Bean superclass packages.
071    *    <li>Bean interfaces.
072    *    <li>Bean interface packages.
073    * </ol>
074    *
075    * @return The namespace associated with this bean property, or <jk>null</jk> if no namespace is associated with it.
076    */
077   public Namespace getNamespace() {
078      return namespace;
079   }
080
081   /**
082    * Returns the XML format of this property from the {@link Xml#format} annotation on this bean property.
083    *
084    * @return The XML format, or {@link XmlFormat#DEFAULT} if annotation not specified.
085    */
086   public XmlFormat getXmlFormat() {
087      return xmlFormat;
088   }
089
090   /**
091    * Returns the child element of this property from the {@link Xml#childName} annotation on this bean property.
092    *
093    * @return The child element, or <jk>null</jk> if annotation not specified.
094    */
095   public String getChildName() {
096      return childName;
097   }
098
099   private void findXmlInfo(Xml xml, MetaProvider mp) {
100      if (xml == null)
101         return;
102      BeanPropertyMeta bpm = getBeanPropertyMeta();
103      ClassMeta<?> cmProperty = bpm.getClassMeta();
104      ClassMeta<?> cmBean = bpm.getBeanMeta().getClassMeta();
105      String name = bpm.getName();
106
107      List<Xml> xmls = bpm.getAllAnnotationsParentFirst(Xml.class);
108      List<XmlSchema> schemas = bpm.getAllAnnotationsParentFirst(XmlSchema.class);
109      namespace = XmlUtils.findNamespace(xmls, schemas);
110
111      if (xmlFormat == XmlFormat.DEFAULT)
112         xmlFormat = xml.format();
113
114      boolean isCollection = cmProperty.isCollectionOrArray();
115
116      String cen = xml.childName();
117      if ((! cen.isEmpty()) && (! isCollection))
118         throw new BeanRuntimeException(cmProperty.getInnerClass(),
119            "Annotation error on property ''{0}''.  @Xml.childName can only be specified on collections and arrays.", name);
120
121      if (xmlFormat == XmlFormat.COLLAPSED) {
122         if (isCollection) {
123            if (cen.isEmpty() && xmlMetaProvider != null)
124               cen = xmlMetaProvider.getXmlClassMeta(cmProperty).getChildName();
125            if (cen == null || cen.isEmpty())
126               cen = cmProperty.getElementType().getDictionaryName();
127            if (cen == null || cen.isEmpty())
128               cen = name;
129         } else {
130            throw new BeanRuntimeException(cmBean.getInnerClass(),
131               "Annotation error on property ''{0}''.  @Xml.format=COLLAPSED can only be specified on collections and arrays.", name);
132         }
133         if (cen.isEmpty() && isCollection)
134            cen = cmProperty.getDictionaryName();
135      }
136
137      if (! cen.isEmpty())
138         childName = cen;
139   }
140}