KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2005 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.identity;
18
19 import org.apache.xerces.impl.xpath.XPathException;
20 import org.apache.xerces.impl.xs.util.ShortListImpl;
21 import org.apache.xerces.util.SymbolTable;
22 import org.apache.xerces.xni.NamespaceContext;
23 import org.apache.xerces.xs.ShortList;
24 import org.apache.xerces.xs.XSComplexTypeDefinition;
25 import org.apache.xerces.xs.XSConstants;
26 import org.apache.xerces.xs.XSTypeDefinition;
27
28 /**
29  * Schema identity constraint field.
30  *
31  * @xerces.internal
32  *
33  * @author Andy Clark, IBM
34  * @version $Id: Field.java,v 1.20 2005/05/25 02:28:39 mrglavas Exp $
35  */

36 public class Field {
37
38     //
39
// Data
40
//
41

42     /** Field XPath. */
43     protected Field.XPath fXPath;
44
45
46     /** Identity constraint. */
47     protected IdentityConstraint fIdentityConstraint;
48
49     //
50
// Constructors
51
//
52

53     /** Constructs a field. */
54     public Field(Field.XPath xpath,
55                  IdentityConstraint identityConstraint) {
56         fXPath = xpath;
57         fIdentityConstraint = identityConstraint;
58     } // <init>(Field.XPath,IdentityConstraint)
59

60     //
61
// Public methods
62
//
63

64     /** Returns the field XPath. */
65     public org.apache.xerces.impl.xpath.XPath getXPath() {
66         return fXPath;
67     } // getXPath():org.apache.xerces.impl.v1.schema.identity.XPath
68

69     /** Returns the identity constraint. */
70     public IdentityConstraint getIdentityConstraint() {
71         return fIdentityConstraint;
72     } // getIdentityConstraint():IdentityConstraint
73

74     // factory method
75

76     /** Creates a field matcher. */
77     public XPathMatcher createMatcher(FieldActivator activator, ValueStore store) {
78         return new Field.Matcher(fXPath, activator, store);
79     } // createMatcher(ValueStore):XPathMatcher
80

81     //
82
// Object methods
83
//
84

85     /** Returns a string representation of this object. */
86     public String JavaDoc toString() {
87         return fXPath.toString();
88     } // toString():String
89

90     //
91
// Classes
92
//
93

94     /**
95      * Field XPath.
96      *
97      * @author Andy Clark, IBM
98      */

99     public static class XPath
100         extends org.apache.xerces.impl.xpath.XPath {
101
102         //
103
// Constructors
104
//
105

106         /** Constructs a field XPath expression. */
107         public XPath(String JavaDoc xpath,
108                      SymbolTable symbolTable,
109                      NamespaceContext context) throws XPathException {
110             // NOTE: We have to prefix the field XPath with "./" in
111
// order to handle selectors such as "@attr" that
112
// select the attribute because the fields could be
113
// relative to the selector element. -Ac
114
// Unless xpath starts with a descendant node -Achille Fokoue
115
// ... or a / or a . - NG
116
super(((xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))?
117                     xpath:"./"+xpath),
118                   symbolTable, context);
119             
120             // verify that only one attribute is selected per branch
121
for (int i=0;i<fLocationPaths.length;i++) {
122                 for(int j=0; j<fLocationPaths[i].steps.length; j++) {
123                     org.apache.xerces.impl.xpath.XPath.Axis axis =
124                         fLocationPaths[i].steps[j].axis;
125                     if (axis.type == XPath.Axis.ATTRIBUTE &&
126                             (j < fLocationPaths[i].steps.length-1)) {
127                         throw new XPathException("c-fields-xpaths");
128                     }
129                 }
130             }
131         } // <init>(String,SymbolTable,NamespacesContext)
132

133     } // class XPath
134

135     /**
136      * Field matcher.
137      *
138      * @author Andy Clark, IBM
139      */

140     protected class Matcher
141         extends XPathMatcher {
142
143         //
144
// Data
145
//
146

147         /** Field activator. */
148         protected FieldActivator fFieldActivator;
149
150         /** Value store for data values. */
151         protected ValueStore fStore;
152
153         //
154
// Constructors
155
//
156

157         /** Constructs a field matcher. */
158         public Matcher(Field.XPath xpath, FieldActivator activator, ValueStore store) {
159             super(xpath);
160             fFieldActivator = activator;
161             fStore = store;
162         } // <init>(Field.XPath,ValueStore)
163

164         //
165
// XPathHandler methods
166
//
167

168         /**
169          * This method is called when the XPath handler matches the
170          * XPath expression.
171          */

172         protected void matched(Object JavaDoc actualValue, short valueType, ShortList itemValueType, boolean isNil) {
173             super.matched(actualValue, valueType, itemValueType, isNil);
174             if(isNil && (fIdentityConstraint.getCategory() == IdentityConstraint.IC_KEY)) {
175                 String JavaDoc code = "KeyMatchesNillable";
176                 fStore.reportError(code, new Object JavaDoc[]{fIdentityConstraint.getElementName()});
177             }
178             fStore.addValue(Field.this, actualValue, convertToPrimitiveKind(valueType), convertToPrimitiveKind(itemValueType));
179             // once we've stored the value for this field, we set the mayMatch
180
// member to false so that, in the same scope, we don't match any more
181
// values (and throw an error instead).
182
fFieldActivator.setMayMatch(Field.this, Boolean.FALSE);
183         } // matched(String)
184

185         private short convertToPrimitiveKind(short valueType) {
186             /** Primitive datatypes. */
187             if (valueType <= XSConstants.NOTATION_DT) {
188                 return valueType;
189             }
190             /** Types derived from string. */
191             if (valueType <= XSConstants.ENTITY_DT) {
192                 return XSConstants.STRING_DT;
193             }
194             /** Types derived from decimal. */
195             if (valueType <= XSConstants.POSITIVEINTEGER_DT) {
196                 return XSConstants.DECIMAL_DT;
197             }
198             /** Other types. */
199             return valueType;
200         }
201
202         private ShortList convertToPrimitiveKind(ShortList itemValueType) {
203             if (itemValueType != null) {
204                 int i;
205                 final int length = itemValueType.getLength();
206                 for (i = 0; i < length; ++i) {
207                     short type = itemValueType.item(i);
208                     if (type != convertToPrimitiveKind(type)) {
209                         break;
210                     }
211                 }
212                 if (i != length) {
213                     final short [] arr = new short[length];
214                     for (int j = 0; j < i; ++j) {
215                         arr[j] = itemValueType.item(j);
216                     }
217                     for(; i < length; ++i) {
218                         arr[i] = convertToPrimitiveKind(itemValueType.item(i));
219                     }
220                     return new ShortListImpl(arr, arr.length);
221                 }
222             }
223             return itemValueType;
224         }
225
226         protected void handleContent(XSTypeDefinition type, boolean nillable, Object JavaDoc actualValue, short valueType, ShortList itemValueType) {
227             if (type == null ||
228                type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE &&
229                ((XSComplexTypeDefinition) type).getContentType()
230                 != XSComplexTypeDefinition.CONTENTTYPE_SIMPLE) {
231
232                     // the content must be simpleType content
233
fStore.reportError( "cvc-id.3", new Object JavaDoc[] {
234                             fIdentityConstraint.getName(),
235                             fIdentityConstraint.getElementName()});
236                 
237             }
238             fMatchedString = actualValue;
239             matched(fMatchedString, valueType, itemValueType, nillable);
240         } // handleContent(XSElementDecl, String)
241

242     } // class Matcher
243

244 } // class Field
245
Popular Tags