1 /* 2 * Copyright 2008 Lukas Lang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 package org.exolab.castor.builder.info.nature; 18 19 import java.util.List; 20 21 import org.castor.core.nature.BaseNature; 22 import org.castor.core.nature.PropertyHolder; 23 import org.exolab.castor.builder.info.GroupInfo; 24 import org.exolab.castor.builder.info.NodeType; 25 import org.exolab.castor.builder.types.XSType; 26 27 /** 28 * A XML specific view of a {@link PropertyHolder}, which can be a {@link ClassInfo} or a 29 * {@link FieldInfo}. Property based implementation. 30 * 31 * @author Lukas Lang 32 * @since 1.2.1 33 */ 34 public final class XMLInfoNature extends BaseNature { 35 36 /** 37 * Property namespace prefix. 38 */ 39 private static final String NAMESPACE_PREFIX = "namespaceprefix"; 40 41 /** 42 * Property namespace URI. 43 */ 44 private static final String NAMESPACE_URI = "namespaceuri"; 45 46 /** 47 * Property node name. 48 */ 49 private static final String NODE_NAME = "nodename"; 50 51 /** 52 * Property node type. 53 */ 54 private static final String NODE_TYPE = "nodetype"; 55 56 /** 57 * Property schema type. 58 */ 59 private static final String SCHEMA_TYPE = "schematype"; 60 61 /** 62 * Indicates XML schema definition is global element or element with 63 * anonymous type. 64 */ 65 private static final String ELEMENT_DEFINITION = "elementdefinition"; 66 67 /** 68 * A flag indicating if the object described by this XML info can appear 69 * more than once. 70 */ 71 private static final String MULTIVALUED = "multivalued"; 72 73 /** 74 * Indicates the XML object must appear at least once. 75 */ 76 private static final String REQUIRED = "required"; 77 78 /** 79 * Property substitution groups. 80 */ 81 private static final String SUBSTITUTION_GROUP = "substitutionGroup"; 82 83 /** 84 * Property container. 85 */ 86 private static final String IS_CONTAINER = "isContainer"; 87 88 /** 89 * Property group info. 90 */ 91 private static final String GROUP_INFO = "groupInfo"; 92 93 /** 94 * Constructor taking a PropertyHolder. 95 * 96 * @param holder 97 * in focus. 98 */ 99 public XMLInfoNature(final PropertyHolder holder) { 100 super(holder); 101 } 102 103 /** 104 * Implementation returns the fully qualified class name. 105 * 106 * @return the Nature id. 107 * @see org.exolab.castor.builder.info.nature.Nature#getId() 108 */ 109 public String getId() { 110 return this.getClass().getName(); 111 } 112 113 /** 114 * Returns the namespace prefix of the object described by this XMLInfo. 115 * 116 * @return the namespace prefix of the object described by this XMLInfo 117 */ 118 public String getNamespacePrefix() { 119 return (String) this.getProperty(NAMESPACE_PREFIX); 120 } 121 122 /** 123 * Returns the namespace URI of the object described by this XMLInfo. 124 * 125 * @return the namespace URI of the object described by this XMLInfo 126 */ 127 public String getNamespaceURI() { 128 return (String) this.getProperty(NAMESPACE_URI); 129 } 130 131 /** 132 * Returns the XML name for the object described by this XMLInfo. 133 * 134 * @return the XML name for the object described by this XMLInfo, or null if 135 * no name has been set 136 */ 137 public String getNodeName() { 138 return (String) this.getProperty(NODE_NAME); 139 } 140 141 /** 142 * Returns the node type for the object described by this XMLInfo. 143 * <code>XMLInfo.ELEMENT_TYPE</code> if property is not set. 144 * 145 * @return the node type for the object described by this XMLInfo 146 */ 147 public NodeType getNodeType() { 148 NodeType nodeType = (NodeType) this.getProperty(NODE_TYPE); 149 if (nodeType == null) { 150 return NodeType.ELEMENT; 151 } 152 return nodeType; 153 } 154 155 /** 156 * Returns the string name of the nodeType, either "attribute", "element" or 157 * "text". 158 * 159 * @return the name of the node-type of the object described by this 160 * XMLInfo. 161 */ 162 public String getNodeTypeName() { 163 NodeType nodeType = getNodeType(); 164 switch (nodeType) { 165 case ATTRIBUTE: 166 return "attribute"; 167 case ELEMENT: 168 return "element"; 169 case TEXT: 170 return "text"; 171 default: 172 return "unknown"; 173 } 174 } 175 176 /** 177 * Returns the XML Schema type for the described object. 178 * 179 * @return the XML Schema type. 180 */ 181 public XSType getSchemaType() { 182 return (XSType) this.getProperty(SCHEMA_TYPE); 183 } 184 185 /** 186 * Returns true if XSD is global element or element with anonymous type or 187 * false if property is not set. 188 * 189 * @return true if xsd is element, false if not or null. 190 */ 191 public boolean isElementDefinition() { 192 return getBooleanPropertyDefaultFalse(ELEMENT_DEFINITION); 193 } 194 195 /** 196 * Returns whether or not the object described by this XMLInfo is 197 * multi-valued (appears more than once in the XML document). Returns false 198 * if the property was not set. 199 * 200 * @return true if this object can appear more than once, false if not or 201 * not set. 202 */ 203 public boolean isMultivalued() { 204 return getBooleanPropertyDefaultFalse(MULTIVALUED); 205 } 206 207 /** 208 * Return true if the XML object described by this XMLInfo must appear at 209 * least once in the XML document (or object model). Returns false if the 210 * property was not set. 211 * 212 * @return true if the XML object must appear at least once, false if not or 213 * not set. 214 */ 215 public boolean isRequired() { 216 return getBooleanPropertyDefaultFalse(REQUIRED); 217 } 218 219 /** 220 * Sets whether or not XSD is element or not. 221 * 222 * @param elementDef 223 * The flag indicating whether or not XSD is global element, 224 * element with anonymous type or not. 225 */ 226 public void setElementDefinition(final boolean elementDef) { 227 this.setProperty(ELEMENT_DEFINITION, new Boolean(elementDef)); 228 } 229 230 /** 231 * Sets whether the XML object can appear more than once in the XML 232 * document. 233 * 234 * @param multivalued 235 * The boolean indicating whether or not the object can appear 236 * more than once. 237 */ 238 public void setMultivalued(final boolean multivalued) { 239 this.setProperty(MULTIVALUED, new Boolean(multivalued)); 240 } 241 242 /** 243 * Sets the desired namespace prefix for this XMLInfo There is no guarantee 244 * that this prefix will be used. 245 * 246 * @param nsPrefix 247 * the desired namespace prefix 248 */ 249 public void setNamespacePrefix(final String nsPrefix) { 250 this.setProperty(NAMESPACE_PREFIX, nsPrefix); 251 } 252 253 /** 254 * Sets the Namespace URI for this XMLInfo. 255 * 256 * @param nsURI 257 * the Namespace URI for this XMLInfo 258 */ 259 public void setNamespaceURI(final String nsURI) { 260 this.setProperty(NAMESPACE_URI, nsURI); 261 } 262 263 /** 264 * Sets the XML name of the object described by this XMLInfo. 265 * 266 * @param name 267 * the XML node name of the described object. 268 */ 269 public void setNodeName(final String name) { 270 this.setProperty(NODE_NAME, name); 271 } 272 273 /** 274 * Sets the nodeType for this XMLInfo. 275 * 276 * @param nodeType 277 * the node type of the described object 278 */ 279 public void setNodeType(final NodeType nodeType) { 280 this.setProperty(NODE_TYPE, nodeType); 281 } 282 283 /** 284 * Sets whether or not the XML object must appear at least once. 285 * 286 * @param required 287 * the flag indicating whether or not this XML object is required 288 */ 289 public void setRequired(final boolean required) { 290 Boolean b = new Boolean(required); 291 this.setProperty(REQUIRED, b); 292 } 293 294 /** 295 * Sets the XML Schema type for this XMLInfo. 296 * 297 * @param xsType 298 * the XML Schema type 299 */ 300 public void setSchemaType(final XSType xsType) { 301 this.setProperty(SCHEMA_TYPE, xsType); 302 } 303 304 /** 305 * Returns the possible substitution groups. 306 * 307 * @return the possible substitution groups. 308 */ 309 @SuppressWarnings("unchecked") 310 public List<String> getSubstitutionGroups() { 311 return getPropertyAsList(SUBSTITUTION_GROUP); 312 } 313 314 /** 315 * Sets the possible substitution groups. 316 * 317 * @param substitutionGroups 318 * Possible substitution groups. 319 */ 320 public void setSubstitutionGroups(final List<String> substitutionGroups) { 321 this.setProperty(SUBSTITUTION_GROUP, substitutionGroups); 322 } 323 324 /** 325 * Returns true if this ClassInfo describes a container class. A container 326 * class is a class which should not be marshalled as XML, but whose members 327 * should be. 328 * 329 * @return true if this ClassInfo describes a container class. 330 */ 331 public boolean isContainer() { 332 return this.getBooleanPropertyDefaultFalse(IS_CONTAINER); 333 } 334 335 /** 336 * Sets whether or not this ClassInfo describes a container class. A 337 * container class is a class which should not be marshalled as XML, but 338 * whose members should be. By default this is false. 339 * 340 * @param isContainer the boolean value when true indicates this class 341 * should be a container class. 342 */ 343 public void setContainer(final boolean isContainer) { 344 this.setProperty(IS_CONTAINER, new Boolean(isContainer)); 345 } 346 347 /** 348 * Returns the {@link GroupInfo} for this XML nature. 349 * 350 * @return the {@link GroupInfo} instance. 351 */ 352 public GroupInfo getGroupInfo() { 353 return (GroupInfo) this.getProperty(GROUP_INFO); 354 } 355 356 /** 357 * Sets the {@link GroupInfo} for this XML nature. 358 * 359 * @param groupInfo the {@link GroupInfo} instance. 360 */ 361 public void setGroupInfo(final GroupInfo groupInfo) { 362 this.setProperty(GROUP_INFO, groupInfo); 363 } 364 365 /** 366 * Returns true if the compositor of this GroupInfo is a choice. 367 * 368 * @return true if the compositor of this GroupInfo is a choice 369 */ 370 public boolean isChoice() { 371 return getGroupInfo().isChoice(); 372 } 373 374 /** 375 * Returns true if the compositor of this GroupInfo is a sequence. 376 * 377 * @return true if the compositor of this GroupInfo is a sequence 378 */ 379 public boolean isSequence() { 380 return getGroupInfo().isSequence(); 381 } 382 383 384 }