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.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020/**
021 * Annotation for specifying various XML options for the XML and RDF/XML serializers.
022 *
023 * <p>
024 * Can be applied to Java packages, types, fields, and methods.
025 *
026 * <p>
027 * Can be used for the following:
028 * <ul>
029 *    <li>Override the child element name on the XML representation of collection or array properties.
030 *    <li>Specify the XML namespace on a package, class, or method.
031 *    <li>Override the XML format on a POJO.
032 * </ul>
033 */
034@Documented
035@Target({TYPE,FIELD,METHOD})
036@Retention(RUNTIME)
037@Inherited
038public @interface Xml {
039
040   /**
041    * Sets the name of the XML child elements for bean properties of type collection and array.
042    *
043    * <p>
044    * Applies only to collection and array bean properties.
045    *
046    * <h5 class='section'>Example:</h5>
047    * <p class='bcode w800'>
048    *    <jk>public class</jk> MyBean {
049    *       <ja>@Xml</ja>(childName=<js>"child"</js>}
050    *       <jk>public</jk> String[] <jf>children</jf> = {<js>"foo"</js>,<js>"bar"</js>};
051    *    }
052    * </p>
053    *
054    * <p>
055    * Without the <ja>@Xml</ja> annotation, serializing this bean as XML would have produced the following...
056    * <p class='bcode w800'>
057    *    <xt>&lt;object&gt;</xt>
058    *       <xt>&lt;children&gt;</xt>
059    *          <xt>&lt;string&gt;</xt>foo<xt>&lt;/string&gt;</xt>
060    *          <xt>&lt;string&gt;</xt>bar<xt>&lt;/string&gt;</xt>
061    *       <xt>&lt;/children&gt;</xt>
062    *    <xt>&lt;/object&gt;</xt>
063    * </p>
064    *
065    * <p>
066    * With the annotations, serializing this bean as XML produces the following...
067    * <p class='bcode w800'>
068    *    <xt>&lt;object&gt;</xt>
069    *       <xt>&lt;children&gt;</xt>
070    *          <xt>&lt;child&gt;</xt>foo<xt>&lt;/child&gt;</xt>
071    *          <xt>&lt;child&gt;</xt>bar<xt>&lt;/child&gt;</xt>
072    *       <xt>&lt;/children&gt;</xt>
073    *    <xt>&lt;/object&gt;</xt>
074    * </p>
075    */
076   String childName() default "";
077
078   /**
079    * The {@link XmlFormat} to use for serializing this object type.
080    *
081    * <h5 class='section'>Example:</h5>
082    * <p class='bcode w800'>
083    *    <jk>public class</jk> MyBean {
084    *
085    *       <jc>// Normally, bean properties would be rendered as child elements of the bean element.</jc>
086    *       <jc>// Override so that it's rendered as a "f1='123'" attribute on the bean element instead.</jc>
087    *       <ja>@Xml</ja>(format=XmlFormat.<jsf>ATTR</jsf>}
088    *       <jk>public int</jk> f1 = 123;
089    *
090    *       <jc>// Normally, bean URL properties would be rendered as XML attributes on the bean element.</jc>
091    *       <jc>// Override so that it's rendered as an &lt;href&gt;http://foo&lt;/href&gt; child element instead.</jc>
092    *       <ja>@BeanProperty</ja>(uri=<jk>true</jk>)
093    *       <ja>@Xml</ja>(format=XmlFormat.<jsf>ELEMENT</jsf>}
094    *       <jk>public</jk> URL <jf>href</jf> = <jk>new</jk> URL(<js>"http://foo"</js>);
095    *
096    *       <jc>// Normally, collection properties would be grouped under a single &lt;children&gt; child element on the bean element.</jc>
097    *       <jc>// Override so that entries are directly children of the bean element with each entry having an element name of &lt;child&gt;.</jc>
098    *       <ja>@Xml</ja>(format=XmlFormat.<jsf>COLLAPSED</jsf>, childName=<js>"child"</js>}
099    *       <jk>public</jk> String[] <jf>children</jf> = <js>"foo"</js>,<js>"bar"</js>};
100    *    }
101    * </p>
102    *
103    * <p>
104    * Without the <ja>@Xml</ja> annotations, serializing this bean as XML would have produced the following...
105    * <p class='bcode w800'>
106    *    <xt>&lt;object</xt> <xa>href</xa>=<js>'http://foo'</js><xt>&gt;</xt>
107    *       <xt>&lt;f1&gt;</xt>123<xt>&lt;/f1&gt;</xt>
108    *       <xt>&lt;children&gt;</xt>
109    *          <xt>&lt;string&gt;</xt>foo<xt>&lt;/string&gt;</xt>
110    *          <xt>&lt;string&gt;</xt>bar<xt>&lt;/string&gt;</xt>
111    *       <xt>&lt;/children&gt;</xt>
112    *    <xt>&lt;/object&gt;</xt>
113    * </p>
114    *
115    * <p>
116    * With the annotations, serializing this bean as XML produces the following...
117    * <p class='bcode w800'>
118    *    <xt>&lt;object</xt> <xa>f1</xa>=<js>'123'</js><xt>&gt;</xt>
119    *       <xt>&lt;href&gt;</xt>http://foo<xt>&lt;/href&gt;</xt>
120    *       <xt>&lt;child&gt;</xt>foo<xt>&lt;/child&gt;</xt>
121    *       <xt>&lt;child&gt;</xt>bar<xt>&lt;/child&gt;</xt>
122    *    <xt>&lt;/object&gt;</xt>
123    * </p>
124    */
125   XmlFormat format() default XmlFormat.DEFAULT;
126
127   /**
128    * Sets the namespace URI of this property or class.
129    *
130    * <p>
131    * Must be matched with a {@link #prefix()} annotation on this object, a parent object, or a {@link XmlNs @XmlNs} with the
132    * same name through the {@link XmlSchema#xmlNs() @XmlSchema(xmlNs)} annotation on the package.
133    */
134   String namespace() default "";
135
136   /**
137    * Sets the XML prefix of this property or class.
138    *
139    * <ul class='spaced-list'>
140    *    <li>
141    *       When applied to a {@link ElementType#TYPE}, namespace is applied to all properties in the class, and all
142    *       subclasses of the class.
143    *    <li>
144    *       When applied to bean properties on {@link ElementType#METHOD} and {@link ElementType#FIELD}, applies
145    *       to the bean property.
146    * </ul>
147    *
148    * <p>
149    * Must either be matched to a {@link #namespace()} annotation on the same object, parent object, or a
150    * {@link XmlNs @XmlNs} with the same name through the {@link XmlSchema#xmlNs() @XmlSchema(xmlNs)} annotation on the package.
151    */
152   String prefix() default "";
153}