001/*
002 * ElementList.java July 2006
003 *
004 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
015 * implied. See the License for the specific language governing 
016 * permissions and limitations under the License.
017 */
018
019package org.simpleframework.xml;
020
021import java.lang.annotation.RetentionPolicy;
022import java.lang.annotation.Retention;
023
024/**
025 * The <code>ElementList</code> annotation represents a method or
026 * field that is a <code>Collection</code> for storing entries. The
027 * collection object deserialized is typically of the same type as
028 * the field. However, a <code>class</code> attribute can be used to
029 * override the field type, however the type must be assignable.
030 * <pre>
031 * 
032 *    &lt;list class="java.util.ArrayList"&gt;
033 *       &lt;entry name="one"/&gt;
034 *       &lt;entry name="two"/&gt;
035 *       &lt;entry name="three"/&gt;  
036 *    &lt;/list&gt;
037 * 
038 * </pre>
039 * If a <code>class</code> attribute is not provided and the type or
040 * the field or method is abstract, a suitable match is searched for 
041 * from the collections available from the Java collections framework.
042 * This annotation can also compose an inline list of XML elements. 
043 * An inline list contains no parent or containing element.
044 * <pre>
045 *
046 *    &lt;entry name="one"/&gt;
047 *    &lt;entry name="two"/&gt;
048 *    &lt;entry name="three"/&gt;  
049 * 
050 * </pre>
051 * The above XML is an example of the output for an inline list of
052 * XML elements. In such a list the annotated field or method must
053 * not be given a name. Instead the name is acquired from the name of
054 * the entry type. For example if the <code>type</code> attribute of
055 * this was set to an object <code>example.Entry</code> then the name 
056 * of the entry list would be taken as the root name of the object
057 * as taken from the <code>Root</code> annotation for that object.
058 * 
059 * @author Niall Gallagher
060 */
061@Retention(RetentionPolicy.RUNTIME)
062public @interface ElementList {
063   
064   /**
065    * This represents the name of the XML element. Annotated fields
066    * can optionally provide the name of the element. If no name is
067    * provided then the name of the annotated field or method will
068    * be used in its place. The name is provided if the field or
069    * method name is not suitable as an XML element name. Also, if
070    * the list is inline then this must not be specified.
071    * 
072    * @return the name of the XML element this represents
073    */
074   String name() default "";
075   
076   /**
077    * This is used to provide a name of the XML element representing
078    * the entry within the list. An entry name is optional and is
079    * used when the name needs to be overridden. This also ensures
080    * that entry, regardless of type has the same root name.   
081    * 
082    * @return this returns the entry XML element for each value
083    */
084   String entry() default "";
085   
086   /**
087    * Represents the type of object the element list contains. This
088    * type is used to deserialize the XML elements from the list. 
089    * The object typically represents the deserialized type, but can
090    * represent a subclass of the type deserialized as determined
091    * by the <code>class</code> attribute value for the list. If 
092    * this is not specified then the type can be determined from the
093    * generic parameter of the annotated <code>Collection</code>.
094    * 
095    * @return the type of the element deserialized from the XML
096    */
097   Class type() default void.class;
098   
099   /**
100    * This is used to determine whether the element data is written
101    * in a CDATA block or not. If this is set to true then the text
102    * is written within a CDATA block, by default the text is output
103    * as escaped XML. Typically this is useful when this annotation
104    * is applied to an array of primitives, such as strings.
105    * 
106    * @return true if entries are to be wrapped in a CDATA block
107    */
108   boolean data() default false;
109   
110   /**
111    * Determines whether the element is required within the XML
112    * document. Any field marked as not required will not have its
113    * value set when the object is deserialized. If an object is to
114    * be serialized only a null attribute will not appear as XML.
115    * 
116    * @return true if the element is required, false otherwise
117    */        
118   boolean required() default true;
119   
120   /**
121    * Determines whether the element list is inlined with respect
122    * to the parent XML element. An inlined element list does not
123    * contain an enclosing element. It is simple a sequence of 
124    * elements that appear one after another within an element.
125    * As such an inline element list must not have a name. 
126    *
127    * @return this returns true if the element list is inline
128    */
129   boolean inline() default false;
130   
131   /**
132    * This is used to determine if an optional field or method can
133    * remain null if it does not exist. If this is false then the
134    * optional element is given an empty list. This is a convenience
135    * attribute which avoids having to check if the element is null
136    * before providing it with a suitable default instance.
137    * 
138    * @return false if an optional element is always instantiated
139    */
140   boolean empty() default true;
141}