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