View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2002-2004 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml.descriptors;
36  
37  import org.exolab.castor.mapping.AccessMode;
38  import org.exolab.castor.mapping.ClassDescriptor;
39  import org.exolab.castor.mapping.FieldDescriptor;
40  import org.exolab.castor.mapping.FieldHandler;
41  import org.exolab.castor.xml.NodeType;
42  import org.exolab.castor.xml.TypeValidator;
43  import org.exolab.castor.xml.XMLFieldDescriptor;
44  import org.exolab.castor.xml.XMLFieldHandler;
45  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
46  
47  /**
48   * The default class descriptor for Arrays that are passed in as the root-level class.
49   *
50   * @author <a href="mailto:kvisco-at-intalio.com">Keith Visco</a>
51   * @version $Revision$ $Date: 2004-12-16 22:49:25 -0700 (Thu, 16 Dec 2004) $
52   */
53  public class RootArrayDescriptor extends BaseDescriptor {
54  
55    /** Our field descriptor for our "content". */
56    private static final XMLFieldDescriptor NO_CONTENT = null;
57    /** The set of attribute descriptors. */
58    private static final XMLFieldDescriptor[] NO_ATTRIBUTES = new XMLFieldDescriptor[0];
59  
60    // --------------------/
61    // - Member Variables -/
62    // --------------------/
63  
64    /** Our field descriptor array. Lists the fields we describe. */
65    private final FieldDescriptor[] _fields = new FieldDescriptor[1];
66    /** The set of element descriptors. */
67    private final XMLFieldDescriptor[] _elements = new XMLFieldDescriptor[1];
68    /** Class object for the type contained in the array. */
69    private final Class _javaClass;
70  
71    /** The XML name for the described object.. */
72    private String _xmlName = "array";
73    /** The desired namespace for the described object. */
74    private String _nsURI = null;
75    /** Type validator to use to validate an instance of this type. */
76    private TypeValidator _validator = null;
77  
78    // ----------------/
79    // - Constructors -/
80    // ----------------/
81  
82    /**
83     * Creates a new RootArrayDescriptor for an array of the provided type.
84     * 
85     * @param array An array to create a RootArrayDescriptor for.
86     */
87    public RootArrayDescriptor(final Class array) {
88      super();
89  
90      if (array == null) {
91        String err = "Argument array must not be null.";
92        throw new IllegalArgumentException(err);
93      }
94  
95      if (!array.isArray()) {
96        String err = "Argument array must be an array.";
97        throw new IllegalArgumentException(err);
98      }
99  
100     _javaClass = array.getComponentType();
101 
102     // -- create element descriptor
103     XMLFieldDescriptorImpl desc =
104         new XMLFieldDescriptorImpl(_javaClass, "_elements", null, NodeType.Element);
105     FieldHandler handler = (new XMLFieldHandler() {
106 
107       /**
108        * {@inheritDoc}
109        */
110       public Object getValue(final Object object) throws IllegalStateException {
111         return object;
112       }
113 
114       /**
115        * {@inheritDoc}
116        */
117       public void setValue(final Object object, final Object value)
118           throws IllegalStateException, IllegalArgumentException {
119         // -- do nothing
120       }
121 
122       /**
123        * {@inheritDoc}
124        */
125       public Object newInstance(final Object parent) {
126         // -- not used
127         return null;
128       }
129     });
130     desc.setHandler(handler);
131     desc.setMultivalued(true);
132 
133     _elements[0] = desc;
134     _fields[0] = desc;
135   } // -- RootArrayDescriptor()
136 
137   // -----------/
138   // - Methods -/
139   // -----------/
140 
141   /**
142    * Returns the set of attribute XMLFieldDescriptors.
143    *
144    * @return an array of XMLFieldDescriptors for all members that should be marshaled as attributes
145    */
146   public XMLFieldDescriptor[] getAttributeDescriptors() {
147     return NO_ATTRIBUTES;
148   } // -- getAttributeDescriptors()
149 
150   /**
151    * Returns the Class that this ClassDescriptor describes.
152    * 
153    * @return the Class that this ClassDescriptor describes.
154    */
155   public Class getJavaClass() {
156     return _javaClass;
157   } // -- getClassType()
158 
159   /**
160    * Returns the set of element MarshalDescriptors.
161    *
162    * @return an array of MarshalDescriptors for all members that should be marshaled as Elements
163    */
164   public XMLFieldDescriptor[] getElementDescriptors() {
165     return _elements;
166   } // -- getElementDescriptors()
167 
168   /**
169    * Returns the class descriptor of the class extended by this class.
170    *
171    * @return The extended class descriptor
172    */
173   public ClassDescriptor getExtends() {
174     return null;
175   } // -- getExtends
176 
177   /**
178    * Returns a list of fields represented by this descriptor.
179    *
180    * @return A list of fields
181    */
182   public FieldDescriptor[] getFields() {
183     return _fields;
184   } // -- getFields
185 
186   /**
187    * Returns the descriptor for dealing with Text content.
188    *
189    * @return the XMLFieldDescriptor for dealing with Text content
190    */
191   public XMLFieldDescriptor getContentDescriptor() {
192     return NO_CONTENT;
193   } // -- getContentDescriptor()
194 
195   /**
196    * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null,
197    * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if
198    * no matching descriptor is available.
199    *
200    * @param name the xml name to match against
201    * @param namespace the namespace uri
202    * @param nodeType the NodeType to match against, or null if the node type is not known.
203    * @return the matching descriptor, or null if no matching descriptor is available.
204    */
205   public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace,
206       final NodeType nodeType) {
207     return _elements[0];
208   } // -- getFieldDescriptor
209 
210   /**
211    * Returns the namespace prefix to use when marshalling as XML.
212    * 
213    * @return the namespace prefix to use when marshalling as XML.
214    */
215   public String getNameSpacePrefix() {
216     return null;
217   } // -- getNameSpacePrefix
218 
219   /**
220    * Returns the namespace URI used when marshaling and unmarshaling as XML.
221    * 
222    * @return the namespace URI used when marshaling and unmarshaling as XML.
223    */
224   public String getNameSpaceURI() {
225     return _nsURI;
226   } // -- getNameSpaceURI
227 
228   /**
229    * Returns the identity field, or null if this class has no identity.
230    *
231    * @return The identity field
232    */
233   public FieldDescriptor getIdentity() {
234     return null;
235   } // -- getIdentity
236 
237   /**
238    * Returns the access mode specified for this class.
239    *
240    * @return The access mode
241    */
242   public AccessMode getAccessMode() {
243     return null;
244   } // -- getAccessMode
245 
246   /**
247    * Returns a specific validator for the class described by this ClassDescriptor. A null value may
248    * be returned if no specific validator exists.
249    *
250    * @return the type validator for the class described by this ClassDescriptor.
251    */
252   public TypeValidator getValidator() {
253     return _validator;
254   } // -- getValidator
255 
256   /**
257    * Returns the XML Name for the Class being described.
258    *
259    * @return the XML name.
260    */
261   public String getXMLName() {
262     return _xmlName;
263   } // -- getXMLName
264 
265   /**
266    * Sets the XML Name for the described object.
267    *
268    * @param xmlName the XML name to use for the described object.
269    */
270   public void setXMLName(final String xmlName) {
271     this._xmlName = xmlName;
272   } // -- setXMLName
273 
274   /**
275    * Sets the desired namespace URI for the described object.
276    *
277    * @param nsURI is the desired namespace URI
278    */
279   public void setNameSpaceURI(final String nsURI) {
280     this._nsURI = nsURI;
281   } // -- setNameSpaceURI
282 
283 } // -- RootArrayDescriptor