KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > traversers > XSDAbstractIDConstraintTraverser


1 /*
2  * Copyright 2001-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.xerces.impl.xs.traversers;
18
19 import org.apache.xerces.impl.xpath.XPathException;
20 import org.apache.xerces.impl.xs.SchemaSymbols;
21 import org.apache.xerces.impl.xs.identity.Field;
22 import org.apache.xerces.impl.xs.identity.IdentityConstraint;
23 import org.apache.xerces.impl.xs.identity.Selector;
24 import org.apache.xerces.util.DOMUtil;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * This class contains code that all three IdentityConstraint
29  * traversers (the XSDUniqueTraverser, XSDKeyTraverser and
30  * XSDKeyrefTraverser) rely upon.
31  *
32  * @xerces.internal
33  *
34  * @version $Id: XSDAbstractIDConstraintTraverser.java,v 1.12 2004/12/20 05:43:36 mrglavas Exp $
35  */

36 class XSDAbstractIDConstraintTraverser extends XSDAbstractTraverser {
37     
38     public XSDAbstractIDConstraintTraverser (XSDHandler handler,
39             XSAttributeChecker gAttrCheck) {
40         super(handler, gAttrCheck);
41     }
42     
43     void traverseIdentityConstraint(IdentityConstraint ic,
44             Element JavaDoc icElem, XSDocumentInfo schemaDoc, Object JavaDoc [] icElemAttrs) {
45         
46         // General Attribute Checking will have been done on icElem by caller
47

48         // check for <annotation> and get selector
49
Element JavaDoc sElem = DOMUtil.getFirstChildElement(icElem);
50         if(sElem == null) {
51             reportSchemaError("s4s-elt-must-match.2",
52                     new Object JavaDoc[]{"identity constraint", "(annotation?, selector, field+)"},
53                     icElem);
54             return;
55         }
56         
57         // General Attribute Checking on sElem
58
// first child could be an annotation
59
if (DOMUtil.getLocalName(sElem).equals(SchemaSymbols.ELT_ANNOTATION)) {
60             ic.addAnnotation(traverseAnnotationDecl(sElem, icElemAttrs, false, schemaDoc));
61             sElem = DOMUtil.getNextSiblingElement(sElem);
62             // if no more children report an error
63
if(sElem == null) {
64                 reportSchemaError("s4s-elt-must-match.2", new Object JavaDoc[]{"identity constraint", "(annotation?, selector, field+)"}, icElem);
65                 return;
66             }
67         }
68         else {
69             String JavaDoc text = DOMUtil.getSyntheticAnnotation(icElem);
70             if (text != null) {
71                 ic.addAnnotation(traverseSyntheticAnnotation(icElem, text, icElemAttrs, false, schemaDoc));
72             }
73         }
74         
75         Object JavaDoc [] attrValues = fAttrChecker.checkAttributes(sElem, false, schemaDoc);
76         
77         // if more than one annotation report an error
78
if(!DOMUtil.getLocalName(sElem).equals(SchemaSymbols.ELT_SELECTOR)) {
79             reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{"identity constraint", "(annotation?, selector, field+)", SchemaSymbols.ELT_SELECTOR}, sElem);
80         }
81         // and make sure <selector>'s content is fine:
82
Element JavaDoc selChild = DOMUtil.getFirstChildElement(sElem);
83         
84         if (selChild !=null) {
85             // traverse annotation if any
86
if (DOMUtil.getLocalName(selChild).equals(SchemaSymbols.ELT_ANNOTATION)) {
87                 ic.addAnnotation(traverseAnnotationDecl(selChild, attrValues, false, schemaDoc));
88                 selChild = DOMUtil.getNextSiblingElement(selChild);
89             }
90             else {
91                 reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{SchemaSymbols.ELT_SELECTOR, "(annotation?)", DOMUtil.getLocalName(selChild)}, selChild);
92             }
93             if (selChild != null) {
94                 reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc [] {SchemaSymbols.ELT_SELECTOR, "(annotation?)", DOMUtil.getLocalName(selChild)}, selChild);
95             }
96         }
97         else {
98             String JavaDoc text = DOMUtil.getSyntheticAnnotation(sElem);
99             if (text != null) {
100                 ic.addAnnotation(traverseSyntheticAnnotation(icElem, text, attrValues, false, schemaDoc));
101             }
102         }
103         
104         String JavaDoc sText = ((String JavaDoc)attrValues[XSAttributeChecker.ATTIDX_XPATH]);
105         if(sText == null) {
106             reportSchemaError("s4s-att-must-appear", new Object JavaDoc [] {SchemaSymbols.ELT_SELECTOR, SchemaSymbols.ATT_XPATH}, sElem);
107             return;
108         }
109         sText = sText.trim();
110         
111         Selector.XPath sXpath = null;
112         try {
113             sXpath = new Selector.XPath(sText, fSymbolTable,
114                     schemaDoc.fNamespaceSupport);
115             Selector selector = new Selector(sXpath, ic);
116             ic.setSelector(selector);
117         }
118         catch (XPathException e) {
119             reportSchemaError(e.getKey(), new Object JavaDoc[]{sText}, sElem);
120             // put back attr values...
121
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
122             return;
123         }
124         
125         // put back attr values...
126
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
127         
128         // get fields
129
Element JavaDoc fElem = DOMUtil.getNextSiblingElement(sElem);
130         if(fElem == null) {
131             reportSchemaError("s4s-elt-must-match.2", new Object JavaDoc[]{"identity constraint", "(annotation?, selector, field+)"}, sElem);
132         }
133         while (fElem != null) {
134             // General Attribute Checking
135
attrValues = fAttrChecker.checkAttributes(fElem, false, schemaDoc);
136             
137             if(!DOMUtil.getLocalName(fElem).equals(SchemaSymbols.ELT_FIELD))
138                 reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{"identity constraint", "(annotation?, selector, field+)", SchemaSymbols.ELT_FIELD}, fElem);
139             
140             // and make sure <field>'s content is fine:
141
Element JavaDoc fieldChild = DOMUtil.getFirstChildElement(fElem);
142             if (fieldChild != null) {
143                 // traverse annotation
144
if (DOMUtil.getLocalName(fieldChild).equals(SchemaSymbols.ELT_ANNOTATION)) {
145                     ic.addAnnotation(traverseAnnotationDecl(fieldChild, attrValues, false, schemaDoc));
146                     fieldChild = DOMUtil.getNextSiblingElement(fieldChild);
147                 }
148             }
149             if (fieldChild != null) {
150                 reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc [] {SchemaSymbols.ELT_FIELD, "(annotation?)", DOMUtil.getLocalName(fieldChild)}, fieldChild);
151             }
152             else {
153                 String JavaDoc text = DOMUtil.getSyntheticAnnotation(fElem);
154                 if (text != null) {
155                     ic.addAnnotation(traverseSyntheticAnnotation(icElem, text, attrValues, false, schemaDoc));
156                 }
157             }
158             String JavaDoc fText = ((String JavaDoc)attrValues[XSAttributeChecker.ATTIDX_XPATH]);
159             if(fText == null) {
160                 reportSchemaError("s4s-att-must-appear", new Object JavaDoc [] {SchemaSymbols.ELT_FIELD, SchemaSymbols.ATT_XPATH}, fElem);
161                 return;
162             }
163             fText = fText.trim();
164             try {
165                 Field.XPath fXpath = new Field.XPath(fText, fSymbolTable,
166                         schemaDoc.fNamespaceSupport);
167                 Field field = new Field(fXpath, ic);
168                 ic.addField(field);
169             }
170             catch (XPathException e) {
171                 reportSchemaError(e.getKey(), new Object JavaDoc[]{fText}, fElem);
172                 // put back attr values...
173
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
174                 return;
175             }
176             fElem = DOMUtil.getNextSiblingElement(fElem);
177             // put back attr values...
178
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
179         }
180         
181     } // traverseIdentityConstraint(IdentityConstraint,Element, XSDocumentInfo)
182
} // XSDAbstractIDConstraintTraverser
183

184
Popular Tags