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