KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > traversers > XSDAttributeTraverser


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.xs.traversers;
59
60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
61 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
62 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
63 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
64 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
65 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeDecl;
66 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
67 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeUseImpl;
68 import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl;
69 import com.sun.org.apache.xerces.internal.xs.XSConstants;
70 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
71 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
72 import com.sun.org.apache.xerces.internal.util.DOMUtil;
73 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
74 import com.sun.org.apache.xerces.internal.xni.QName;
75 import org.w3c.dom.Element JavaDoc;
76
77 /**
78  * The attribute declaration schema component traverser.
79  *
80  * <attribute
81  * default = string
82  * fixed = string
83  * form = (qualified | unqualified)
84  * id = ID
85  * name = NCName
86  * ref = QName
87  * type = QName
88  * use = (optional | prohibited | required) : optional
89  * {any attributes with non-schema namespace . . .}>
90  * Content: (annotation?, (simpleType?))
91  * </attribute>
92  *
93  * @author Sandy Gao, IBM
94  * @author Neeraj Bajaj, Sun Microsystems, inc.
95  * @version $Id: XSDAttributeTraverser.java,v 1.27 2003/11/11 20:15:00 sandygao Exp $
96  */

97 class XSDAttributeTraverser extends XSDAbstractTraverser {
98
99     public XSDAttributeTraverser (XSDHandler handler,
100                                   XSAttributeChecker gAttrCheck) {
101         super(handler, gAttrCheck);
102     }
103
104     protected XSAttributeUseImpl traverseLocal(Element JavaDoc attrDecl,
105                                                XSDocumentInfo schemaDoc,
106                                                SchemaGrammar grammar,
107                                                XSComplexTypeDecl enclosingCT) {
108
109         // General Attribute Checking
110
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(attrDecl, false, schemaDoc);
111
112         String JavaDoc defaultAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_DEFAULT];
113         String JavaDoc fixedAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_FIXED];
114         String JavaDoc nameAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
115         QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
116         XInt useAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_USE];
117
118         // get 'attribute declaration'
119
XSAttributeDecl attribute = null;
120         if (attrDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) {
121             if (refAtt != null) {
122                 attribute = (XSAttributeDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTE_TYPE, refAtt, attrDecl);
123
124                 Element JavaDoc child = DOMUtil.getFirstChildElement(attrDecl);
125                 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
126                     // REVISIT: put this somewhere
127
traverseAnnotationDecl(child, attrValues, false, schemaDoc);
128                     child = DOMUtil.getNextSiblingElement(child);
129                 }
130
131                 if (child != null) {
132                     reportSchemaError("src-attribute.3.2", new Object JavaDoc[]{refAtt.rawname}, child);
133                 }
134                 // for error reporting
135
nameAtt = refAtt.localpart;
136             } else {
137                 attribute = null;
138             }
139         } else {
140             attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, false, enclosingCT);
141         }
142
143         // get 'value constraint'
144
short consType = XSConstants.VC_NONE;
145         if (defaultAtt != null) {
146             consType = XSConstants.VC_DEFAULT;
147         } else if (fixedAtt != null) {
148             consType = XSConstants.VC_FIXED;
149             defaultAtt = fixedAtt;
150             fixedAtt = null;
151         }
152
153         XSAttributeUseImpl attrUse = null;
154         if (attribute != null) {
155             if (fSchemaHandler.fDeclPool !=null) {
156                 attrUse = fSchemaHandler.fDeclPool.getAttributeUse();
157             } else {
158                 attrUse = new XSAttributeUseImpl();
159             }
160             attrUse.fAttrDecl = attribute;
161             attrUse.fUse = useAtt.shortValue();
162             attrUse.fConstraintType = consType;
163             if (defaultAtt != null) {
164                 attrUse.fDefault = new ValidatedInfo();
165                 attrUse.fDefault.normalizedValue = defaultAtt;
166             }
167         }
168         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
169
170         //src-attribute
171

172         // 1 default and fixed must not both be present.
173
if (defaultAtt != null && fixedAtt != null) {
174             reportSchemaError("src-attribute.1", new Object JavaDoc[]{nameAtt}, attrDecl);
175         }
176
177         // 2 If default and use are both present, use must have the actual value optional.
178
if (consType == XSConstants.VC_DEFAULT &&
179             useAtt != null && useAtt.intValue() != SchemaSymbols.USE_OPTIONAL) {
180             reportSchemaError("src-attribute.2", new Object JavaDoc[]{nameAtt}, attrDecl);
181         }
182
183         // a-props-correct
184

185         if (defaultAtt != null && attrUse != null) {
186             // 2 if there is a {value constraint}, the canonical lexical representation of its value must be valid with respect to the {type definition} as defined in String Valid (3.14.4).
187
fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
188             try {
189                 checkDefaultValid(attrUse);
190             }
191             catch (InvalidDatatypeValueException ide) {
192                 reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl);
193                 reportSchemaError ("a-props-correct.2", new Object JavaDoc[]{nameAtt, defaultAtt}, attrDecl);
194             }
195
196             // 3 If the {type definition} is or is derived from ID then there must not be a {value constraint}.
197
if (((XSSimpleType)attribute.getTypeDefinition()).isIDType() ) {
198                 reportSchemaError ("a-props-correct.3", new Object JavaDoc[]{nameAtt}, attrDecl);
199             }
200
201             // check 3.5.6 constraint
202
// Attribute Use Correct
203
// 2 If the {attribute declaration} has a fixed {value constraint}, then if the attribute use itself has a {value constraint}, it must also be fixed and its value must match that of the {attribute declaration}'s {value constraint}.
204
if (attrUse.fAttrDecl.getConstraintType() == XSConstants.VC_FIXED &&
205                 attrUse.fConstraintType != XSConstants.VC_NONE) {
206                 if (attrUse.fConstraintType != XSConstants.VC_FIXED ||
207                     !attrUse.fAttrDecl.getValInfo().actualValue.equals(attrUse.fDefault.actualValue)) {
208                     reportSchemaError ("au-props-correct.2", new Object JavaDoc[]{nameAtt, attrUse.fAttrDecl.getValInfo().stringValue()}, attrDecl);
209                 }
210             }
211         }
212
213         return attrUse;
214     }
215
216     protected XSAttributeDecl traverseGlobal(Element JavaDoc attrDecl,
217                                              XSDocumentInfo schemaDoc,
218                                              SchemaGrammar grammar) {
219
220         // General Attribute Checking
221
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(attrDecl, true, schemaDoc);
222         XSAttributeDecl attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, true, null);
223         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
224         return attribute;
225
226     }
227
228     /**
229      * Traverse a globally declared attribute.
230      *
231      * @param attrDecl
232      * @param attrValues
233      * @param schemaDoc
234      * @param grammar
235      * @param isGlobal
236      * @return the attribute declaration index
237      */

238     XSAttributeDecl traverseNamedAttr(Element JavaDoc attrDecl,
239                                       Object JavaDoc[] attrValues,
240                                       XSDocumentInfo schemaDoc,
241                                       SchemaGrammar grammar,
242                                       boolean isGlobal,
243                                       XSComplexTypeDecl enclosingCT) {
244
245         String JavaDoc defaultAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_DEFAULT];
246         String JavaDoc fixedAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_FIXED];
247         XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM];
248         String JavaDoc nameAtt = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
249         QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE];
250
251         // Step 1: get declaration information
252
XSAttributeDecl attribute = null;
253         if (fSchemaHandler.fDeclPool !=null) {
254             attribute = fSchemaHandler.fDeclPool.getAttributeDecl();
255         } else {
256             attribute = new XSAttributeDecl();
257         }
258
259         // get 'name'
260
if (nameAtt != null)
261             nameAtt = fSymbolTable.addSymbol(nameAtt);
262
263         // get 'target namespace'
264
String JavaDoc tnsAtt = null;
265         XSComplexTypeDecl enclCT = null;
266         short scope = XSAttributeDecl.SCOPE_ABSENT;
267         if (isGlobal) {
268             tnsAtt = schemaDoc.fTargetNamespace;
269             scope = XSAttributeDecl.SCOPE_GLOBAL;
270         }
271         else {
272             if (enclosingCT != null) {
273                 enclCT = enclosingCT;
274                 scope = XSAttributeDecl.SCOPE_LOCAL;
275             }
276             if (formAtt != null) {
277                 if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED)
278                     tnsAtt = schemaDoc.fTargetNamespace;
279             } else if (schemaDoc.fAreLocalAttributesQualified) {
280                 tnsAtt = schemaDoc.fTargetNamespace;
281             }
282         }
283         // get 'value constraint'
284
// for local named attribute, value constraint is absent
285
ValidatedInfo attDefault = null;
286         short constraintType = XSConstants.VC_NONE;
287         if (isGlobal) {
288             if (fixedAtt != null) {
289                 attDefault = new ValidatedInfo();
290                 attDefault.normalizedValue = fixedAtt;
291                 constraintType = XSConstants.VC_FIXED;
292             } else if (defaultAtt != null) {
293                 attDefault = new ValidatedInfo();
294                 attDefault.normalizedValue = defaultAtt;
295                 constraintType = XSConstants.VC_DEFAULT;
296             }
297         }
298
299         // get 'annotation'
300
Element JavaDoc child = DOMUtil.getFirstChildElement(attrDecl);
301         XSAnnotationImpl annotation = null;
302         if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
303             annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
304             child = DOMUtil.getNextSiblingElement(child);
305         }
306
307         // get 'type definition'
308
XSSimpleType attrType = null;
309         boolean haveAnonType = false;
310
311         // Handle Anonymous type if there is one
312
if (child != null) {
313             String JavaDoc childName = DOMUtil.getLocalName(child);
314
315             if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) {
316                 attrType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar);
317                 haveAnonType = true;
318                 child = DOMUtil.getNextSiblingElement(child);
319             }
320         }
321
322         // Handler type attribute
323
if (attrType == null && typeAtt != null) {
324             XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, attrDecl);
325             if (type != null && type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE)
326                 attrType = (XSSimpleType)type;
327             else
328                 reportSchemaError("src-resolve", new Object JavaDoc[]{typeAtt.rawname, "simpleType definition"}, attrDecl);
329         }
330
331         if (attrType == null) {
332             attrType = SchemaGrammar.fAnySimpleType;
333         }
334
335         attribute.setValues(nameAtt, tnsAtt, attrType,
336             constraintType, scope, attDefault, enclCT, annotation);
337
338         // Step 2: register attribute decl to the grammar
339
if (isGlobal && nameAtt != null)
340             grammar.addGlobalAttributeDecl(attribute);
341
342         // Step 3: check against schema for schemas
343

344         // required attributes
345
if (nameAtt == null) {
346             if (isGlobal)
347                 reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{SchemaSymbols.ELT_ATTRIBUTE, SchemaSymbols.ATT_NAME}, attrDecl);
348             else
349                 reportSchemaError("src-attribute.3.1", null, attrDecl);
350             nameAtt = NO_NAME;
351         }
352
353         // element
354
if (child != null) {
355             reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{nameAtt, "(annotation?, (simpleType?))", DOMUtil.getLocalName(child)}, child);
356         }
357
358         // Step 4: check 3.2.3 constraints
359

360         // src-attribute
361

362         // 1 default and fixed must not both be present.
363
if (defaultAtt != null && fixedAtt != null) {
364             reportSchemaError("src-attribute.1", new Object JavaDoc[]{nameAtt}, attrDecl);
365         }
366
367         // 2 If default and use are both present, use must have the actual value optional.
368
// This is checked in "traverse" method
369

370         // 3 If the item's parent is not <schema>, then all of the following must be true:
371
// 3.1 One of ref or name must be present, but not both.
372
// This is checked in XSAttributeChecker
373

374         // 3.2 If ref is present, then all of <simpleType>, form and type must be absent.
375
// Attributes are checked in XSAttributeChecker, elements are checked in "traverse" method
376

377         // 4 type and <simpleType> must not both be present.
378
if (haveAnonType && (typeAtt != null)) {
379             reportSchemaError( "src-attribute.4", new Object JavaDoc[]{nameAtt}, attrDecl);
380         }
381
382         // Step 5: check 3.2.6 constraints
383
// check for NOTATION type
384
checkNotationType(nameAtt, attrType, attrDecl);
385
386         // a-props-correct
387

388         // 2 if there is a {value constraint}, the canonical lexical representation of its value must be valid with respect to the {type definition} as defined in String Valid (3.14.4).
389
if (attDefault != null) {
390             fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
391             try {
392                 checkDefaultValid(attribute);
393             }
394             catch (InvalidDatatypeValueException ide) {
395                 reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl);
396                 reportSchemaError ("a-props-correct.2", new Object JavaDoc[]{nameAtt, attDefault.normalizedValue}, attrDecl);
397             }
398         }
399
400         // 3 If the {type definition} is or is derived from ID then there must not be a {value constraint}.
401
if (attDefault != null) {
402             if (attrType.isIDType() ) {
403                 reportSchemaError ("a-props-correct.3", new Object JavaDoc[]{nameAtt}, attrDecl);
404             }
405         }
406
407         // no-xmlns
408

409         // The {name} of an attribute declaration must not match xmlns.
410
if (nameAtt != null && nameAtt.equals(XMLSymbols.PREFIX_XMLNS)) {
411             reportSchemaError("no-xmlns", null, attrDecl);
412         }
413
414         // no-xsi
415

416         // The {target namespace} of an attribute declaration, whether local or top-level, must not match http://www.w3.org/2001/XMLSchema-instance (unless it is one of the four built-in declarations given in the next section).
417
if (tnsAtt != null && tnsAtt.equals(SchemaSymbols.URI_XSI)) {
418             reportSchemaError("no-xsi", new Object JavaDoc[]{SchemaSymbols.URI_XSI}, attrDecl);
419         }
420
421         // Attribute without a name. Return null.
422
if (attribute.getName() == null)
423             return null;
424
425         return attribute;
426     }
427
428     // throws an error if the constraint value is invalid for the given type
429
void checkDefaultValid(XSAttributeDecl attribute) throws InvalidDatatypeValueException {
430         // validate the original lexical rep, and set the actual value
431
((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().normalizedValue, fValidationState, attribute.getValInfo());
432         // validate the canonical lexical rep
433
((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().stringValue(), fValidationState, attribute.getValInfo());
434     }
435
436     // throws an error if the constraint value is invalid for the given type
437
void checkDefaultValid(XSAttributeUseImpl attrUse) throws InvalidDatatypeValueException {
438          // validate the original lexical rep, and set the actual value
439
((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.normalizedValue, fValidationState, attrUse.fDefault);
440         // validate the canonical lexical rep
441
((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.stringValue(), fValidationState, attrUse.fDefault);
442     }
443
444 }
445
Popular Tags