KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > schema > identity > Field


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001 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 org.enhydra.apache.xerces.validators.schema.identity;
59
60 import org.enhydra.apache.xerces.utils.NamespacesScope;
61 import org.enhydra.apache.xerces.utils.StringPool;
62 import org.enhydra.apache.xerces.validators.datatype.DatatypeValidator;
63
64 /**
65  * Schema identity constraint field.
66  *
67  * @author Andy Clark, IBM
68  * @version $Id: Field.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $
69  */

70 public class Field {
71
72     //
73
// Data
74
//
75

76     /** Field XPath. */
77     protected Field.XPath fXPath;
78
79     /** Datatype. */
80     // Unfortunately, a Field may conceivably match values of varying
81
// datatypes. Hence, this member no longer makes sense; see the IDValue class.
82
// protected DatatypeValidator fDatatypeValidator;
83

84     /** Identity constraint. */
85     protected IdentityConstraint fIdentityConstraint;
86
87     // whether this field can be matched; used to catch instance documents
88
// that try and match a field several times in the same scope.
89
protected boolean mayMatch = true;
90
91     //
92
// Constructors
93
//
94

95     /** Constructs a field. */
96     public Field(Field.XPath xpath,
97                  IdentityConstraint identityConstraint) {
98         fXPath = xpath;
99         fIdentityConstraint = identityConstraint;
100     } // <init>(Field.XPath,IdentityConstraint)
101

102     //
103
// Public methods
104
//
105

106     // sets mayMatch
107
public void setMayMatch(boolean b) {
108         mayMatch = b;
109     } // setMayMatch(boolean);
110

111     // returns mayMatch
112
public boolean mayMatch() {
113         return mayMatch;
114     } // mayMatch():boolean
115

116     /** Returns the field XPath. */
117     public org.enhydra.apache.xerces.validators.schema.identity.XPath getXPath() {
118         return fXPath;
119     } // getXPath():org.enhydra.apache.xerces.validators.schema.identity.XPath
120

121     /** Returns the identity constraint. */
122     public IdentityConstraint getIdentityConstraint() {
123         return fIdentityConstraint;
124     } // getIdentityConstraint():IdentityConstraint
125

126     // factory method
127

128     /** Creates a field matcher. */
129     public XPathMatcher createMatcher(ValueStore store) {
130         return new Field.Matcher(fXPath, store);
131     } // createMatcher(ValueStore):XPathMatcher
132

133     //
134
// Object methods
135
//
136

137     /** Returns a string representation of this object. */
138     public String JavaDoc toString() {
139         return fXPath.toString();
140     } // toString():String
141

142     //
143
// Classes
144
//
145

146     /**
147      * Field XPath.
148      *
149      * @author Andy Clark, IBM
150      */

151     public static class XPath
152         extends org.enhydra.apache.xerces.validators.schema.identity.XPath {
153
154         //
155
// Constructors
156
//
157

158         /** Constructs a field XPath expression. */
159         public XPath(String JavaDoc xpath, StringPool stringPool,
160                      NamespacesScope context) throws XPathException {
161             // NOTE: We have to prefix the field XPath with "./" in
162
// order to handle selectors such as "@attr" that
163
// select the attribute because the fields could be
164
// relative to the selector element. -Ac
165
// Unless xpath starts with a descendant node -Achille Fokoue
166
// ... or a / or a . - NG
167
super(((xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))?
168                 xpath:"./"+xpath), stringPool, context);
169             
170         } // <init>(String,StringPool,NamespacesScope)
171

172     } // class XPath
173

174     /**
175      * Field matcher.
176      *
177      * @author Andy Clark, IBM
178      */

179     protected class Matcher
180         extends XPathMatcher {
181
182         //
183
// Data
184
//
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, ValueStore store) {
195             super(xpath, true, null);
196             fStore = store;
197         } // <init>(Field.XPath,ValueStore)
198

199         //
200
// XPathHandler methods
201
//
202

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

207         protected void matched(String JavaDoc content, DatatypeValidator val, boolean isNil) throws Exception JavaDoc {
208             super.matched(content, val, isNil);
209             if(isNil) {
210                 fStore.reportNilError(fIdentityConstraint);
211             }
212             fStore.addValue(Field.this, new IDValue(content, val));
213             // once we've stored the value for this field, we set the mayMatch
214
// member to false so that, in the same scope, we don't match any more
215
// values (and throw an error instead).
216
mayMatch = false;
217         } // matched(String)
218

219     } // class Matcher
220

221 } // class Field
222
Popular Tags