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 2003 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * $Id$ 34 */ 35 package org.exolab.castor.types; 36 37 import java.util.HashMap; 38 import java.util.HashSet; 39 import java.util.Map; 40 import java.util.Set; 41 42 import org.exolab.castor.mapping.AccessMode; 43 import org.exolab.castor.mapping.ClassDescriptor; 44 import org.exolab.castor.mapping.FieldDescriptor; 45 import org.exolab.castor.xml.NodeType; 46 import org.exolab.castor.xml.TypeValidator; 47 import org.exolab.castor.xml.UnmarshalState; 48 import org.exolab.castor.xml.ValidationException; 49 import org.exolab.castor.xml.XMLClassDescriptor; 50 import org.exolab.castor.xml.XMLFieldDescriptor; 51 52 /** 53 * The Base Descriptor class, this class is extended by the descriptors for the custom Castor schema 54 * types. 55 * 56 * @author <a href="kvisco@intalio.com">Keith Visco</a> 57 * @version $Revision$ $Date: 2004-12-16 22:49:25 -0700 (Thu, 16 Dec 2004) $ 58 */ 59 public class BaseDescriptor implements XMLClassDescriptor { 60 61 62 63 /** Used for returning no attribute and no element fields */ 64 protected static final XMLFieldDescriptor[] noXMLFields = new XMLFieldDescriptor[0]; 65 /** Used for returning no attribute and no element fields */ 66 private static final FieldDescriptor[] noJavaFields = new FieldDescriptor[0]; 67 /** The name of the XML element. */ 68 private String _xmlName = null; 69 /** The class type for the descriptor */ 70 private Class _class = null; 71 72 /** 73 * Map holding the properties set and read by Natures. 74 */ 75 private Map _properties = new HashMap(); 76 77 /** 78 * Map holding the available natures. 79 */ 80 private Set _natures = new HashSet(); 81 82 protected BaseDescriptor(String xmlName, Class type) { 83 super(); 84 _xmlName = xmlName; 85 _class = type; 86 } // -- BaseDescriptor 87 88 /** 89 * Returns the set of XMLFieldDescriptors for all members that should be marshalled as XML 90 * attributes. 91 * 92 * @return an array of XMLFieldDescriptors for all members that should be marshalled as XML 93 * attributes. 94 */ 95 public XMLFieldDescriptor[] getAttributeDescriptors() { 96 return noXMLFields; 97 } // getAttributeDescriptors 98 99 /** 100 * Returns the XMLFieldDescriptor for the member that should be marshalled as text content. 101 * 102 * @return the XMLFieldDescriptor for the member that should be marshalled as text content. 103 */ 104 public XMLFieldDescriptor getContentDescriptor() { 105 return null; 106 } // getContentDescriptor 107 108 /** 109 * Returns the set of XMLFieldDescriptors for all members that should be marshalled as XML 110 * elements. 111 * 112 * @return an array of XMLFieldDescriptors for all members that should be marshalled as XML 113 * elements. 114 */ 115 public XMLFieldDescriptor[] getElementDescriptors() { 116 return noXMLFields; 117 } // getElementDescriptors 118 119 /** 120 * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null, 121 * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if 122 * no matching descriptor is available. 123 * 124 * @param name The xml name to match against. 125 * @param namespace The namespace uri. 126 * @param nodeType The NodeType to match against, or null if the node type is not known. 127 * @return The matching descriptor, or null if no matching descriptor is available. 128 */ 129 public XMLFieldDescriptor getFieldDescriptor(String name, String namespace, NodeType nodeType) { 130 return null; 131 } // -- getFieldDescriptor 132 133 /** 134 * @return the namespace prefix to use when marshalling as XML. 135 */ 136 public String getNameSpacePrefix() { 137 return null; 138 } // -- getNameSpacePrefix 139 140 /** 141 * @return the namespace URI used when marshalling and unmarshalling as XML. 142 */ 143 public String getNameSpaceURI() { 144 return null; 145 } // -- getNameSpaceURI 146 147 /** 148 * Returns a specific validator for the class described by this ClassDescriptor. A null value may 149 * be returned if no specific validator exists. 150 * 151 * @return the type validator for the class described by this ClassDescriptor. 152 */ 153 public TypeValidator getValidator() { 154 return null; 155 } // -- getValidator 156 157 /** 158 * Returns the XML Name for the Class being described. 159 * 160 * @return the XML name. 161 */ 162 public String getXMLName() { 163 return _xmlName; 164 } // -- getXMLName 165 166 /** 167 * Returns the String representation of this XMLClassDescriptor. 168 * 169 * @return the String representation of this XMLClassDescriptor. 170 */ 171 public String toString() { 172 String className = null; 173 Class type = getJavaClass(); 174 if (type != null) { 175 className = type.getName(); 176 } else { 177 className = "unspecified"; 178 } 179 180 return super.toString() + "; descriptor for class: " + className + "; xml name: " 181 + getXMLName(); 182 } // -- toString 183 184 /** 185 * Returns the Java class represented by this descriptor. 186 * 187 * @return The Java class 188 */ 189 public Class getJavaClass() { 190 return _class; 191 } // -- getJavaClass 192 193 194 /** 195 * Returns a list of fields represented by this descriptor. 196 * 197 * @return A list of fields 198 */ 199 public FieldDescriptor[] getFields() { 200 return noJavaFields; 201 } // -- getFields 202 203 204 /** 205 * Returns the class descriptor of the class extended by this class. 206 * 207 * @return The extended class descriptor 208 */ 209 public ClassDescriptor getExtends() { 210 return null; 211 } // -- getExtends 212 213 /** 214 * Returns the identity field, null if this class has no identity. 215 * 216 * @return The identity field 217 */ 218 public FieldDescriptor getIdentity() { 219 return null; 220 } // -- getIdentity 221 222 /** 223 * Returns the access mode specified for this class. 224 * 225 * @return The access mode 226 */ 227 public AccessMode getAccessMode() { 228 return null; 229 } // -- getAccessMode 230 231 /** 232 * Returns true if the given object represented by this XMLClassDescriptor can accept a member 233 * whose name is given. An XMLClassDescriptor can accept a field if it contains a descriptor that 234 * matches the given name and if the given object can hold this field (i.e a value is not already 235 * set for this field). 236 * <p> 237 * This is mainly used for container object (that can contains other object), in this particular 238 * case the implementation will return null. 239 * 240 * @param name the xml name of the field to check 241 * @param namespace the namespace uri 242 * @param object the object represented by this XMLCLassDescriptor 243 * @return true if the given object represented by this XMLClassDescriptor can accept a member 244 * whose name is given. 245 */ 246 public boolean canAccept(String name, String namespace, Object object) { 247 return false; 248 } 249 250 /** 251 * {@inheritDoc} 252 * 253 * @see org.exolab.castor.xml.XMLClassDescriptor#checkDescriptorForCorrectOrderWithinSequence(org.exolab.castor.xml.XMLFieldDescriptor, 254 * org.exolab.castor.xml.UnmarshalState, java.lang.String) 255 */ 256 public void checkDescriptorForCorrectOrderWithinSequence( 257 final XMLFieldDescriptor elementDescriptor, final UnmarshalState parentState, 258 final String xmlName) throws ValidationException { 259 // nothing to check, iow empty implementation 260 } 261 262 /** 263 * {@inheritDoc} 264 * 265 * @see org.exolab.castor.xml.XMLClassDescriptor#isChoice() 266 */ 267 public boolean isChoice() { 268 return false; 269 } 270 271 /** 272 * @see org.exolab.castor.builder.info.nature.PropertyHolder# getProperty(java.lang.String) 273 * @param name of the property 274 * @return value of the property 275 */ 276 public Object getProperty(final String name) { 277 return _properties.get(name); 278 } 279 280 /** 281 * @see org.exolab.castor.builder.info.nature.PropertyHolder# setProperty(java.lang.String, 282 * java.lang.Object) 283 * @param name of the property 284 * @param value of the property 285 */ 286 public void setProperty(final String name, final Object value) { 287 _properties.put(name, value); 288 } 289 290 /** 291 * @see org.exolab.castor.builder.info.nature.NatureExtendable# addNature(java.lang.String) 292 * @param nature ID of the Nature 293 */ 294 public void addNature(final String nature) { 295 _natures.add(nature); 296 } 297 298 /** 299 * @see org.exolab.castor.builder.info.nature.NatureExtendable# hasNature(java.lang.String) 300 * @param nature ID of the Nature 301 * @return true if the Nature ID was added. 302 */ 303 public boolean hasNature(final String nature) { 304 return _natures.contains(nature); 305 } 306 307 } // -- BaseDescriptor