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