View Javadoc

1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
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.apache.ws.commons.schema;
18  
19  import org.apache.ws.commons.schema.constants.Constants;
20  
21  import javax.xml.namespace.QName;
22  
23  
24  /**
25   * Class for elements. Represents the World Wide Web Consortium (W3C) element element.
26   */
27  
28  public class XmlSchemaElement extends XmlSchemaParticle implements TypeReceiver {
29  
30      /**
31       * Attribute used to block a type derivation.
32       */
33      XmlSchemaDerivationMethod block;
34  
35      /**
36       * The value after an element has been compiled to post-schema infoset.
37       * This value is either from the type itself or, if not defined on the type, taken from the schema element.
38       */
39      XmlSchemaDerivationMethod blockResolved;
40      XmlSchemaObjectCollection constraints;
41  
42      /**
43       * Provides the default value of the element if its content
44       * is a simple type or the element's content is textOnly.
45       */
46      String defaultValue;
47      String fixedValue;
48  
49      /**
50       * Returns the correct common runtime library
51       * object based upon the SchemaType for the element.
52       */
53      Object elementType;
54  
55      XmlSchemaDerivationMethod finalDerivation;
56      XmlSchemaDerivationMethod finalDerivationResolved;
57  
58      /**
59       * The default value is the value of the elementFormDefault attribute for the schema element containing the attribute.
60       * The default is Unqualified.
61       */
62      XmlSchemaForm form;
63      boolean isAbstract;
64      boolean isNillable;
65      String name;
66      QName qualifiedName;
67      QName refName;
68  
69      /**
70       * Returns the type of the element.
71       * This can either be a complex type or a simple type.
72       */
73      XmlSchemaType schemaType;
74  
75      /**
76       * QName of a built-in data type defined in this schema or another
77       * schema indicated by the specified namespace.
78       */
79      QName schemaTypeName;
80  
81      /**
82       * QName of an element that can be a substitute for this element.
83       */
84      QName substitutionGroup;
85  
86      /**
87       * Creates new XmlSchemaElement
88       */
89      public XmlSchemaElement() {
90          constraints = new XmlSchemaObjectCollection();
91          isAbstract = false;
92          isNillable = false;
93          form = new XmlSchemaForm(XmlSchemaForm.NONE);
94          finalDerivation = new XmlSchemaDerivationMethod(Constants.BlockConstants.NONE);
95          block = new XmlSchemaDerivationMethod(Constants.BlockConstants.NONE);
96      }
97  
98      /**
99       * Returns a collection of constraints on the element.
100      */
101     public XmlSchemaObjectCollection getConstraints() {
102         return constraints;
103     }
104 
105     public String getDefaultValue() {
106         return defaultValue;
107     }
108 
109     public void setDefaultValue(String defaultValue) {
110         this.defaultValue = defaultValue;
111     }
112 
113     public XmlSchemaDerivationMethod getBlock() {
114         return block;
115     }
116 
117     public void setBlock(XmlSchemaDerivationMethod block) {
118         this.block = block;
119     }
120 
121     public XmlSchemaDerivationMethod getFinal() {
122         return finalDerivation;
123     }
124 
125     public void setFinal(XmlSchemaDerivationMethod finalDerivation) {
126         this.finalDerivation = finalDerivation;
127     }
128 
129     public XmlSchemaDerivationMethod getBlockResolved() {
130         return blockResolved;
131     }
132 
133     public String getFixedValue() {
134         return fixedValue;
135     }
136 
137     public void setFixedValue(String fixedValue) {
138         this.fixedValue = fixedValue;
139     }
140 
141     public Object getElementType() {
142         return elementType;
143     }
144 
145     public XmlSchemaForm getForm() {
146         return form;
147     }
148 
149     public void setForm(XmlSchemaForm form) {
150         this.form = form;
151     }
152 
153     public boolean isAbstract() {
154         return isAbstract;
155     }
156 
157     public void setAbstract(boolean isAbstract) {
158         this.isAbstract = isAbstract;
159     }
160 
161     public boolean isNillable() {
162         return isNillable;
163     }
164 
165     public void setNillable(boolean isNillable) {
166         this.isNillable = isNillable;
167     }
168 
169     public String getName() {
170         return name;
171     }
172 
173     public void setName(String name) {
174         this.name = name;
175     }
176 
177     public QName getRefName() {
178         return refName;
179     }
180 
181     public void setRefName(QName refName) {
182         this.refName = refName;
183     }
184 
185     public QName getQName() {
186         return qualifiedName;
187     }
188 
189     public void setQName(QName qualifiedName) {
190         this.qualifiedName = qualifiedName;
191     }
192 
193     public XmlSchemaType getSchemaType() {
194         return schemaType;
195     }
196 
197     public void setSchemaType(XmlSchemaType schemaType) {
198         this.schemaType = schemaType;
199     }
200 
201     public QName getSchemaTypeName() {
202         return schemaTypeName;
203     }
204 
205     public void setSchemaTypeName(QName schemaTypeName) {
206         this.schemaTypeName = schemaTypeName;
207     }
208 
209     public QName getSubstitutionGroup() {
210         return substitutionGroup;
211     }
212 
213     public void setSubstitutionGroup(QName substitutionGroup) {
214         this.substitutionGroup = substitutionGroup;
215     }
216 
217     public String toString(String prefix, int tab) {
218         String xml = new String();
219 
220         if (!prefix.equals("") && prefix.indexOf(":") == -1)
221             prefix += ":";
222 
223         for (int i = 0; i < tab; i++)
224             xml += "\t";
225 
226         xml += "<" + prefix + "element ";
227 
228         if (!name.equals(""))
229             xml += "name=\"" + name + "\" ";
230 
231         if (schemaTypeName != null)
232             xml += "type=\"" + schemaTypeName + "\"";
233 
234         if (refName != null)
235             xml += "ref=\"" + refName + "\" ";
236 
237         if (minOccurs != 1)
238             xml += "minOccurs=\"" + minOccurs + "\" ";
239 
240         if (maxOccurs != 1)
241             xml += "maxOccurs=\"" + maxOccurs + "\" ";
242         
243         if (isNillable)
244           xml += "nillable=\"" + isNillable + "\" ";
245 
246         xml += ">\n";
247 
248         if (constraints != null)
249             xml += constraints.toString(prefix, (tab + 1));
250 
251         if (schemaType != null) {
252             xml += schemaType.toString(prefix, (tab + 1));
253         }
254         for (int i = 0; i < tab; i++)
255             xml += "\t";
256 
257         xml += "</" + prefix + "element>\n";
258 
259         return xml;
260     }
261 
262     public void setType(XmlSchemaType type) {
263         this.schemaType = type;
264     }
265 }