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 2000-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.xml.NodeType; 41 import org.exolab.castor.xml.TypeValidator; 42 import org.exolab.castor.xml.XMLFieldDescriptor; 43 import org.exolab.castor.xml.XMLFieldHandler; 44 import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; 45 46 /** 47 * The default java.util.Vector class descriptor. 48 * 49 * @author <a href="mailto:kvisco-at-intalio.com">Keith Visco</a> 50 * @version $Revision$ $Date: 2004-12-16 22:49:25 -0700 (Thu, 16 Dec 2004) $ 51 */ 52 public class VectorClassDescriptor extends BaseDescriptor { 53 54 /** The set of attribute descriptors. */ 55 private static final XMLFieldDescriptor[] NO_ATTRIBUTES = new XMLFieldDescriptor[0]; 56 /** The content descriptor. */ 57 private static final XMLFieldDescriptorImpl NO_CONTENT = null; 58 59 // --------------------/ 60 // - Member Variables -/ 61 // --------------------/ 62 63 /** The set of element descriptors. */ 64 private final XMLFieldDescriptor[] _elements; 65 /** Our field descriptor array. Lists the fields we describe. */ 66 private final FieldDescriptor[] _fields; 67 /** Our field descriptor. */ 68 private final XMLFieldDescriptorImpl _desc; 69 70 /** The XML name for the described object. */ 71 private String _xmlName = null; 72 /** The desired namespace for the described object. */ 73 private String _nsURI = null; 74 /** Type validator to use to validate an instance of this type. */ 75 private TypeValidator _validator = null; 76 77 // ----------------/ 78 // - Constructors -/ 79 // ----------------/ 80 81 /** 82 * No-arg constructor. 83 */ 84 public VectorClassDescriptor() { 85 this(null); 86 } // -- VectorClassDescriptor 87 88 /** 89 * Creates a VectorClassDescriptor for the given XML name. 90 * 91 * @param xmlName name of the XML type 92 */ 93 public VectorClassDescriptor(final String xmlName) { 94 super(); 95 96 _xmlName = xmlName; 97 98 // -- Create FieldDescriptor 99 _desc = new XMLFieldDescriptorImpl(Object.class, "item", _xmlName, NodeType.Element); 100 101 _desc.setMultivalued(true); 102 _desc.setMatches("*"); 103 _desc.setHandler(new XMLFieldHandler() { 104 105 /** 106 * {@inheritDoc} 107 */ 108 public Object getValue(final Object object) throws IllegalStateException { 109 return ((java.util.Vector) object).elements(); 110 } 111 112 /** 113 * {@inheritDoc} 114 */ 115 public void setValue(final Object object, final Object value) 116 throws IllegalStateException, IllegalArgumentException { 117 try { 118 ((java.util.Vector) object).addElement(value); 119 } catch (Exception ex) { 120 throw new IllegalStateException(ex.toString()); 121 } 122 } 123 124 /** 125 * {@inheritDoc} 126 */ 127 public Object newInstance(final Object parent) { 128 return null; 129 } 130 }); 131 132 _fields = new FieldDescriptor[1]; 133 _fields[0] = _desc; 134 135 _elements = new XMLFieldDescriptor[1]; 136 _elements[0] = _desc; 137 } // -- VectorClassDescriptor() 138 139 // -----------/ 140 // - Methods -/ 141 // -----------/ 142 143 /** 144 * Returns the set of attribute XMLFieldDescriptors. 145 * 146 * @return an array of XMLFieldDescriptors for all members that should be marshaled as attributes 147 */ 148 public XMLFieldDescriptor[] getAttributeDescriptors() { 149 return NO_ATTRIBUTES; 150 } // -- getAttributeDescriptors() 151 152 /** 153 * Returns the Class that this ClassDescriptor describes. 154 * 155 * @return the Class that this ClassDescriptor describes 156 */ 157 public Class getJavaClass() { 158 return java.util.Vector.class; 159 } // -- getClassType() 160 161 /** 162 * Returns the set of element MarshalDescriptors. 163 * 164 * @return an array of MarshalDescriptors for all members that should be marshaled as Elements 165 */ 166 public XMLFieldDescriptor[] getElementDescriptors() { 167 return _elements; 168 } // -- getElementDescriptors() 169 170 /** 171 * Returns the class descriptor of the class extended by this class. 172 * 173 * @return The extended class descriptor 174 */ 175 public ClassDescriptor getExtends() { 176 return null; 177 } // -- getExtends 178 179 /** 180 * Returns a list of fields represented by this descriptor. 181 * 182 * @return A list of fields 183 */ 184 public FieldDescriptor[] getFields() { 185 return _fields; 186 } // -- getFields 187 188 /** 189 * Returns the descriptor for dealing with Text content. 190 * 191 * @return the XMLFieldDescriptor for dealing with Text content 192 */ 193 public XMLFieldDescriptor getContentDescriptor() { 194 return NO_CONTENT; 195 } // -- getContentDescriptor() 196 197 /** 198 * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null, 199 * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if 200 * no matching descriptor is available. 201 * 202 * @param name the xml name to match against 203 * @param namespace the namespace uri 204 * @param nodeType the NodeType to match against, or null if the node type is not known. 205 * @return the matching descriptor, or null if no matching descriptor is available. 206 */ 207 public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace, 208 final NodeType nodeType) { 209 if (nodeType == null || nodeType == NodeType.Element) { 210 for (int i = 0; i < _elements.length; i++) { 211 XMLFieldDescriptor desc = _elements[i]; 212 if (desc != null && desc.matches(name, namespace)) { 213 return desc; 214 } 215 } 216 } 217 return null; 218 } // -- getFieldDescriptor 219 220 /** 221 * @return the namespace prefix to use when marshaling as XML. 222 */ 223 public String getNameSpacePrefix() { 224 return null; 225 } // -- getNameSpacePrefix 226 227 /** 228 * @return the namespace URI used when marshaling and unmarshaling as XML. 229 */ 230 public String getNameSpaceURI() { 231 return _nsURI; 232 } // -- getNameSpaceURI 233 234 /** 235 * Returns the identity field, null if this class has no identity. 236 * 237 * @return The identity field 238 */ 239 public FieldDescriptor getIdentity() { 240 return null; 241 } // -- getIdentity 242 243 /** 244 * Returns the access mode specified for this class. 245 * 246 * @return The access mode 247 */ 248 public AccessMode getAccessMode() { 249 return null; 250 } // -- getAccessMode 251 252 /** 253 * Returns a specific validator for the class described by this ClassDescriptor. A null value may 254 * be returned if no specific validator exists. 255 * 256 * @return the type validator for the class described by this ClassDescriptor. 257 */ 258 public TypeValidator getValidator() { 259 return _validator; 260 } // -- getValidator 261 262 /** 263 * Returns the XML Name for the Class being described. 264 * 265 * @return the XML name. 266 */ 267 public String getXMLName() { 268 return _xmlName; 269 } // -- getXMLName 270 271 /** 272 * Sets the type validator to use to validate an instance of this type. 273 * 274 * @param validator the type validator to use. 275 */ 276 public void setValidator(final TypeValidator validator) { 277 this._validator = validator; 278 } // -- setValidator 279 280 /** 281 * Sets the XML Name for the described object. 282 * 283 * @param xmlName the XML name to use for the described object. 284 */ 285 public void setXMLName(final String xmlName) { 286 if (xmlName != null && xmlName.length() > 0) { 287 _xmlName = xmlName; 288 _desc.setXMLName(xmlName); 289 } 290 } // -- setXMLName 291 292 /** 293 * Sets the desired namespace URI for the described object. 294 * 295 * @param nsURI is the desired namespace URI 296 */ 297 public void setNameSpaceURI(final String nsURI) { 298 this._nsURI = nsURI; 299 } // -- setNameSpaceURI 300 301 /** 302 * Returns true if the given object represented by this XMLClassDescriptor can accept a member 303 * whose name is given. An XMLClassDescriptor can accept a field if it contains a descriptor that 304 * matches the given name and if the given object can hold this field (i.e a value is not already 305 * set for this field). 306 * 307 * @param name the xml name of the field to check 308 * @param namespace the namespace uri 309 * @param object the object represented by this XMLCLassDescriptor 310 * @return true if the given object represented by this XMLClassDescriptor can accept a member 311 * whose name is given. 312 */ 313 public boolean canAccept(final String name, final String namespace, final Object object) { 314 // ListClassDescriptor only contains one FieldDescriptor that matches 315 // with a wild-card '*', just return true since it can accept any object 316 return true; 317 } // -- canAccept 318 319 }