001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2001 The Apache Software Foundation.
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.validators.schema.identity;
059:
060: import org.apache.xerces.utils.NamespacesScope;
061: import org.apache.xerces.utils.StringPool;
062:
063: import org.apache.xerces.validators.datatype.DatatypeValidator;
064:
065: import org.xml.sax.SAXException;
066:
067: /**
068: * Schema identity constraint field.
069: *
070: * @author Andy Clark, IBM
071: * @version $Id: Field.java,v 1.10 2001/06/01 20:08:45 neilg Exp $
072: */
073: public class Field {
074:
075: //
076: // Data
077: //
078:
079: /** Field XPath. */
080: protected Field.XPath fXPath;
081:
082: /** Datatype. */
083: // Unfortunately, a Field may conceivably match values of varying
084: // datatypes. Hence, this member no longer makes sense; see the IDValue class.
085: // protected DatatypeValidator fDatatypeValidator;
086: /** Identity constraint. */
087: protected IdentityConstraint fIdentityConstraint;
088:
089: // whether this field can be matched; used to catch instance documents
090: // that try and match a field several times in the same scope.
091: protected boolean mayMatch = true;
092:
093: //
094: // Constructors
095: //
096:
097: /** Constructs a field. */
098: public Field(Field.XPath xpath,
099: IdentityConstraint identityConstraint) {
100: fXPath = xpath;
101: fIdentityConstraint = identityConstraint;
102: } // <init>(Field.XPath,IdentityConstraint)
103:
104: //
105: // Public methods
106: //
107:
108: // sets mayMatch
109: public void setMayMatch(boolean b) {
110: mayMatch = b;
111: } // setMayMatch(boolean);
112:
113: // returns mayMatch
114: public boolean mayMatch() {
115: return mayMatch;
116: } // mayMatch():boolean
117:
118: /** Returns the field XPath. */
119: public org.apache.xerces.validators.schema.identity.XPath getXPath() {
120: return fXPath;
121: } // getXPath():org.apache.xerces.validators.schema.identity.XPath
122:
123: /** Returns the identity constraint. */
124: public IdentityConstraint getIdentityConstraint() {
125: return fIdentityConstraint;
126: } // getIdentityConstraint():IdentityConstraint
127:
128: // factory method
129:
130: /** Creates a field matcher. */
131: public XPathMatcher createMatcher(ValueStore store) {
132: return new Field.Matcher(fXPath, store);
133: } // createMatcher(ValueStore):XPathMatcher
134:
135: //
136: // Object methods
137: //
138:
139: /** Returns a string representation of this object. */
140: public String toString() {
141: return fXPath.toString();
142: } // toString():String
143:
144: //
145: // Classes
146: //
147:
148: /**
149: * Field XPath.
150: *
151: * @author Andy Clark, IBM
152: */
153: public static class XPath extends
154: org.apache.xerces.validators.schema.identity.XPath {
155:
156: //
157: // Constructors
158: //
159:
160: /** Constructs a field XPath expression. */
161: public XPath(String xpath, StringPool stringPool,
162: NamespacesScope context) throws XPathException {
163: // NOTE: We have to prefix the field XPath with "./" in
164: // order to handle selectors such as "@attr" that
165: // select the attribute because the fields could be
166: // relative to the selector element. -Ac
167: // Unless xpath starts with a descendant node -Achille Fokoue
168: // ... or a / or a . - NG
169: super (((xpath.trim().startsWith("/") || xpath.trim()
170: .startsWith(".")) ? xpath : "./" + xpath),
171: stringPool, context);
172:
173: } // <init>(String,StringPool,NamespacesScope)
174:
175: } // class XPath
176:
177: /**
178: * Field matcher.
179: *
180: * @author Andy Clark, IBM
181: */
182: protected class Matcher extends XPathMatcher {
183:
184: //
185: // Data
186: //
187:
188: /** Value store for data values. */
189: protected ValueStore fStore;
190:
191: //
192: // Constructors
193: //
194:
195: /** Constructs a field matcher. */
196: public Matcher(Field.XPath xpath, ValueStore store) {
197: super (xpath, true, null);
198: fStore = store;
199: } // <init>(Field.XPath,ValueStore)
200:
201: //
202: // XPathHandler methods
203: //
204:
205: /**
206: * This method is called when the XPath handler matches the
207: * XPath expression.
208: */
209: protected void matched(String content, DatatypeValidator val,
210: boolean isNil) throws Exception {
211: super .matched(content, val, isNil);
212: if (isNil) {
213: fStore.reportNilError(fIdentityConstraint);
214: }
215: fStore.addValue(Field.this , new IDValue(content, val));
216: // once we've stored the value for this field, we set the mayMatch
217: // member to false so that, in the same scope, we don't match any more
218: // values (and throw an error instead).
219: mayMatch = false;
220: } // matched(String)
221:
222: } // class Matcher
223:
224: } // class Field
|