KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > XSElementImpl


1 /*
2  * Copyright 2003, 2004 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.jaxme.xs.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ws.jaxme.xs.XSAnnotation;
23 import org.apache.ws.jaxme.xs.XSElement;
24 import org.apache.ws.jaxme.xs.XSGroup;
25 import org.apache.ws.jaxme.xs.XSIdentityConstraint;
26 import org.apache.ws.jaxme.xs.XSKeyRef;
27 import org.apache.ws.jaxme.xs.XSObject;
28 import org.apache.ws.jaxme.xs.XSObjectFactory;
29 import org.apache.ws.jaxme.xs.XSSchema;
30 import org.apache.ws.jaxme.xs.XSType;
31 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
32 import org.apache.ws.jaxme.xs.xml.XsAnyURI;
33 import org.apache.ws.jaxme.xs.xml.XsBlockSet;
34 import org.apache.ws.jaxme.xs.xml.XsEKey;
35 import org.apache.ws.jaxme.xs.xml.XsEKeyref;
36 import org.apache.ws.jaxme.xs.xml.XsESchema;
37 import org.apache.ws.jaxme.xs.xml.XsEUnique;
38 import org.apache.ws.jaxme.xs.xml.XsFormChoice;
39 import org.apache.ws.jaxme.xs.xml.XsNCName;
40 import org.apache.ws.jaxme.xs.xml.XsQName;
41 import org.apache.ws.jaxme.xs.xml.XsTElement;
42 import org.apache.ws.jaxme.xs.xml.XsTIdentityConstraint;
43 import org.apache.ws.jaxme.xs.xml.XsTTopLevelElement;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /**
48  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
49  */

50 public class XSElementImpl extends XSOpenAttrsImpl implements XSElement {
51   private static final XSIdentityConstraint[] NO_CONSTRAINTS
52   = new XSIdentityConstraint[] {};
53
54   private static final XSKeyRef[] NO_KEY_REFS = new XSKeyRef[] {};
55
56   private final XsQName name;
57   private final boolean isGlobal;
58   private XSAnnotation[] annotations;
59   private boolean isValidated;
60   private boolean isNillable;
61   private XSType type;
62   private XSGroup substitutionGroup;
63   private XSIdentityConstraint[] identityConstraints;
64   private XSKeyRef[] keyReferences;
65
66   protected XsTElement getXsTElement() {
67     return (XsTElement) getXsObject();
68   }
69
70   protected boolean isReference() {
71     return getXsTElement().getRef() != null;
72   }
73
74   protected boolean isInnerSimpleType() {
75     return getXsTElement().getSimpleType() != null;
76   }
77
78   protected boolean isInnerComplexType() {
79     return getXsTElement().getComplexType() != null;
80   }
81
82   protected XSElementImpl(XSObject pParent, XsTElement pBaseElement)
83       throws SAXException
84   {
85     super(pParent, pBaseElement);
86
87     XsQName qName;
88
89     if (isReference()) {
90       qName = pBaseElement.getRef();
91     } else {
92       XsNCName myName = pBaseElement.getName();
93       if (myName == null) {
94         throw new LocSAXException("Invalid element: Must have either of its 'ref' or 'name' attributes set.",
95                                      getLocator());
96       }
97
98       XsESchema schema = pBaseElement.getXsESchema();
99       XsAnyURI namespace;
100       String JavaDoc namespacePrefix;
101       boolean qualified = pBaseElement.isGlobal();
102       if (!qualified) {
103         XsFormChoice form = pBaseElement.getForm();
104         if (form == null) {
105           form = schema.getElementFormDefault();
106         }
107         qualified = XsFormChoice.QUALIFIED.equals(form);
108       }
109       if (qualified) {
110         namespace = schema.getTargetNamespace();
111         namespacePrefix = schema.getTargetNamespacePrefix();
112       } else {
113         namespace = null;
114         namespacePrefix = null;
115       }
116       qName = new XsQName(namespace, myName.toString(), namespacePrefix);
117
118       configureIdentityConstraints(pParent, pBaseElement);
119     }
120
121     name = qName;
122     isGlobal = isReference() || (pBaseElement instanceof XsTTopLevelElement);
123     isNillable = pBaseElement.getNillable();
124
125     annotations = getXSSchema().getXSObjectFactory().newXSAnnotations(
126       this,
127       pBaseElement.getAnnotation()
128     );
129   }
130
131   public boolean isGlobal() {
132     return isGlobal;
133   }
134   
135   public boolean isNillable() {
136     return isNillable;
137   }
138
139   public XsQName getName() {
140     return name;
141   }
142
143   public XSType getType() {
144     return type;
145   }
146
147   protected boolean isValidated() {
148     return isValidated;
149   }
150
151   public void validate() throws SAXException {
152     if (isValidated()) {
153       return;
154     } else {
155       isValidated = true;
156     }
157
158     XSSchema schema = getXSSchema();
159     XSObjectFactory factory = schema.getXSObjectFactory();
160
161     XSType myType;
162     if (isReference()) {
163       XSElement element = schema.getElement(getName());
164       if (element == null) {
165         throw new LocSAXException("Invalid element reference: " + getName() + " is not defined.",
166                                      getLocator());
167       }
168       element.validate();
169       isNillable = element.isNillable();
170       myType = element.getType();
171       
172     } else {
173       XsTElement element = getXsTElement();
174       if (isInnerSimpleType()) {
175         myType = factory.newXSType(this, element.getSimpleType());
176       } else if (isInnerComplexType()) {
177         myType = factory.newXSType(this, element.getComplexType());
178       } else {
179         XsQName typeName = element.getType();
180         if (typeName == null) {
181           throw new LocSAXException("Invalid element: Either of its 'type' or 'ref' attributes or its 'simpleType' or 'complexType' children must be set.",
182                                        getLocator());
183         }
184         myType = schema.getType(typeName);
185         if (myType == null) {
186           throw new LocSAXException("Invalid element: The type " + typeName + " is not defined.",
187                                        getLocator());
188         }
189       }
190     }
191     this.type = myType;
192     
193
194     myType.validate();
195     validateAllIn( annotations );
196     validateAllIn( identityConstraints );
197     validateAllIn( keyReferences );
198   }
199
200   public XSAnnotation[] getAnnotations() {
201     return annotations;
202   }
203
204   public String JavaDoc getDefault() {
205     return getXsTElement().getDefault();
206   }
207
208   public String JavaDoc getFixed() {
209     return getXsTElement().getFixed();
210   }
211
212   public XsQName getSubstitutionGroupName() {
213     return getXsTElement().getSubstitutionGroup();
214   }
215
216   public boolean isBlockedForSubstitution() {
217     XsBlockSet blockSet = getXsTElement().getBlock();
218     if (blockSet == null) {
219       blockSet = getXsTElement().getXsESchema().getBlockDefault();
220     }
221     return !blockSet.isSubstitutionAllowed();
222   }
223
224   public boolean isAbstract() {
225     return getXsTElement().getAbstract();
226   }
227
228   public void setSubstitutionGroup(XSGroup pGroup) {
229     substitutionGroup = pGroup;
230   }
231
232   public XSGroup getSubstitutionGroup() {
233     return substitutionGroup;
234   }
235
236   public XSIdentityConstraint[] getIdentityConstraints() {
237     return identityConstraints;
238   }
239
240   public XSKeyRef[] getKeyRefs() {
241     return keyReferences;
242   }
243
244   /**
245    * Process the unique, key and keyref identity constraints into instances
246    * of XSIdentityConstraint and XSKeyRef.
247    */

248   private void configureIdentityConstraints(XSObject pParent, XsTElement base)
249     throws SAXException
250   {
251     XsTIdentityConstraint[] rawConstraints = base.getIdentityConstraints();
252     final int numRawConstraints = rawConstraints.length;
253
254     if ( numRawConstraints == 0 ) {
255       identityConstraints = NO_CONSTRAINTS;
256       keyReferences = NO_KEY_REFS;
257
258       return;
259     }
260
261     XSSchema schema = getXSSchema();
262     XSObjectFactory factory = schema.getXSObjectFactory();
263     List JavaDoc constraints = new ArrayList JavaDoc(1);
264     List JavaDoc refKeys = new ArrayList JavaDoc(1);
265
266     for ( int i=0; i<numRawConstraints; i++ ) {
267       XsTIdentityConstraint raw = rawConstraints[i];
268
269       if ( raw instanceof XsEKeyref ) {
270         XSKeyRef keyRef = factory.newXSKeyRef( this, (XsEKeyref) raw );
271
272         refKeys.add( keyRef );
273         schema.add( keyRef );
274       } else if ( raw instanceof XsEKey ) {
275         XSIdentityConstraint ic = factory.newXSIdentityConstraint(
276           this,
277           (XsEKey) raw
278         );
279
280         constraints.add( ic );
281         schema.add( ic );
282       } else if( raw instanceof XsEUnique ) {
283         XSIdentityConstraint ic = factory.newXSIdentityConstraint(
284           this,
285           (XsEUnique) raw
286         );
287
288         constraints.add( ic );
289         schema.add( ic );
290       }
291     }
292
293     int numConstraints = constraints.size();
294     if ( numConstraints == 0 ) {
295       identityConstraints = NO_CONSTRAINTS;
296     } else {
297       identityConstraints = (XSIdentityConstraint[]) constraints.toArray(
298         new XSIdentityConstraint[numConstraints]
299       );
300     }
301
302     int numKeyRefs = refKeys.size();
303     if ( numKeyRefs == 0 ) {
304       keyReferences = NO_KEY_REFS;
305     } else {
306       keyReferences = (XSKeyRef[]) refKeys.toArray(new XSKeyRef[numKeyRefs]);
307     }
308   }
309 }
310
Popular Tags