KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > identity > Field


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation.
6  * All rights 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) 1999, 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.identity;
59
60 import com.sun.org.apache.xerces.internal.impl.xpath.XPathException;
61 import com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition;
62 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
63 import com.sun.org.apache.xerces.internal.util.SymbolTable;
64 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
65
66 /**
67  * Schema identity constraint field.
68  *
69  * @author Andy Clark, IBM
70  * @version $Id: Field.java,v 1.17 2004/03/11 16:10:46 mrglavas Exp $
71  */

72 public class Field {
73
74     //
75
// Data
76
//
77

78     /** Field XPath. */
79     protected Field.XPath fXPath;
80
81
82     /** Identity constraint. */
83     protected IdentityConstraint fIdentityConstraint;
84
85     //
86
// Constructors
87
//
88

89     /** Constructs a field. */
90     public Field(Field.XPath xpath,
91                  IdentityConstraint identityConstraint) {
92         fXPath = xpath;
93         fIdentityConstraint = identityConstraint;
94     } // <init>(Field.XPath,IdentityConstraint)
95

96     //
97
// Public methods
98
//
99

100     /** Returns the field XPath. */
101     public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() {
102         return fXPath;
103     } // getXPath():com.sun.org.apache.xerces.internal.impl.v1.schema.identity.XPath
104

105     /** Returns the identity constraint. */
106     public IdentityConstraint getIdentityConstraint() {
107         return fIdentityConstraint;
108     } // getIdentityConstraint():IdentityConstraint
109

110     // factory method
111

112     /** Creates a field matcher. */
113     public XPathMatcher createMatcher(FieldActivator activator, ValueStore store) {
114         return new Field.Matcher(fXPath, activator, store);
115     } // createMatcher(ValueStore):XPathMatcher
116

117     //
118
// Object methods
119
//
120

121     /** Returns a string representation of this object. */
122     public String JavaDoc toString() {
123         return fXPath.toString();
124     } // toString():String
125

126     //
127
// Classes
128
//
129

130     /**
131      * Field XPath.
132      *
133      * @author Andy Clark, IBM
134      */

135     public static class XPath
136         extends com.sun.org.apache.xerces.internal.impl.xpath.XPath {
137
138         //
139
// Constructors
140
//
141

142         /** Constructs a field XPath expression. */
143         public XPath(String JavaDoc xpath,
144                      SymbolTable symbolTable,
145                      NamespaceContext context) throws XPathException {
146             // NOTE: We have to prefix the field XPath with "./" in
147
// order to handle selectors such as "@attr" that
148
// select the attribute because the fields could be
149
// relative to the selector element. -Ac
150
// Unless xpath starts with a descendant node -Achille Fokoue
151
// ... or a / or a . - NG
152
super(((xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))?
153                     xpath:"./"+xpath),
154                   symbolTable, context);
155             
156             // verify that only one attribute is selected per branch
157
for (int i=0;i<fLocationPaths.length;i++) {
158                 for(int j=0; j<fLocationPaths[i].steps.length; j++) {
159                     com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis =
160                         fLocationPaths[i].steps[j].axis;
161                     if (axis.type == XPath.Axis.ATTRIBUTE &&
162                             (j < fLocationPaths[i].steps.length-1)) {
163                         throw new XPathException("c-fields-xpaths");
164                     }
165                 }
166             }
167         } // <init>(String,SymbolTable,NamespacesContext)
168

169     } // class XPath
170

171     /**
172      * Field matcher.
173      *
174      * @author Andy Clark, IBM
175      */

176     protected class Matcher
177         extends XPathMatcher {
178
179         //
180
// Data
181
//
182

183         /** Field activator. */
184         protected FieldActivator fFieldActivator;
185
186         /** Value store for data values. */
187         protected ValueStore fStore;
188
189         //
190
// Constructors
191
//
192

193         /** Constructs a field matcher. */
194         public Matcher(Field.XPath xpath, FieldActivator activator, ValueStore store) {
195             super(xpath);
196             fFieldActivator = activator;
197             fStore = store;
198         } // <init>(Field.XPath,ValueStore)
199

200         //
201
// XPathHandler methods
202
//
203

204         /**
205          * This method is called when the XPath handler matches the
206          * XPath expression.
207          */

208         protected void matched(Object JavaDoc actualValue, boolean isNil) {
209             super.matched(actualValue, isNil);
210             if(isNil && (fIdentityConstraint.getCategory() == IdentityConstraint.IC_KEY)) {
211                 String JavaDoc code = "KeyMatchesNillable";
212                 fStore.reportError(code, new Object JavaDoc[]{fIdentityConstraint.getElementName()});
213             }
214             fStore.addValue(Field.this, actualValue,fCurrMatchedType);
215             // once we've stored the value for this field, we set the mayMatch
216
// member to false so that, in the same scope, we don't match any more
217
// values (and throw an error instead).
218
fFieldActivator.setMayMatch(Field.this, Boolean.FALSE);
219         } // matched(String)
220

221         protected void handleContent(XSTypeDefinition type, boolean nillable, Object JavaDoc actualValue) {
222             if (type == null ||
223                type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE &&
224                ((XSComplexTypeDefinition) type).getContentType()
225                 != XSComplexTypeDefinition.CONTENTTYPE_SIMPLE) {
226
227                     // the content must be simpleType content
228
fStore.reportError( "cvc-id.3", new Object JavaDoc[] {
229                             fIdentityConstraint.getName(),
230                             fIdentityConstraint.getElementName()});
231                 
232             }
233             fCurrMatchedType = type;
234             fMatchedString = actualValue;
235             matched(fMatchedString, nillable);
236         } // handleContent(XSElementDecl, String)
237

238     } // class Matcher
239

240 } // class Field
241
Popular Tags