KickJava   Java API By Example, From Geeks To Geeks.

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


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.framework.XMLAttrList;
61 import org.enhydra.apache.xerces.utils.NamespacesScope;
62 import org.enhydra.apache.xerces.utils.QName;
63 import org.enhydra.apache.xerces.utils.StringPool;
64 import org.enhydra.apache.xerces.validators.schema.SchemaGrammar;
65 import org.xml.sax.SAXException JavaDoc;
66
67 /**
68  * Schema identity constraint selector.
69  *
70  * @author Andy Clark, IBM
71  * @version $Id: Selector.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $
72  */

73 public class Selector {
74
75     //
76
// Data
77
//
78

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

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

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

100     /** Returns the selector XPath. */
101     public org.enhydra.apache.xerces.validators.schema.identity.XPath getXPath() {
102         return fXPath;
103     } // getXPath():org.enhydra.apache.xerces.impl.xpath.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 selector matcher. */
113     public XPathMatcher createMatcher(FieldActivator activator) {
114         return new Selector.Matcher(fXPath, activator);
115     } // createMatcher(FieldActivator):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      * Schema identity constraint selector XPath expression.
132      *
133      * @author Andy Clark, IBM
134      * @version $Id: Selector.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $
135      */

136     public static class XPath
137         extends org.enhydra.apache.xerces.validators.schema.identity.XPath {
138     
139         //
140
// Constructors
141
//
142

143         /** Constructs a selector XPath expression. */
144         public XPath(String JavaDoc xpath, StringPool stringPool,
145                      NamespacesScope context) throws XPathException {
146             // NOTE: We have to prefix the selector XPath with "./" in
147
// order to handle selectors such as "." that select
148
// the element container because the fields could be
149
// relative to that 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), stringPool, context);
154     
155             // verify that an attribute is not selected
156
for (int i=0;i<fLocationPaths.length;i++) {
157                 org.enhydra.apache.xerces.validators.schema.identity.XPath.Axis axis =
158                     fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
159             if (axis.type == Axis.ATTRIBUTE) {
160                 throw new XPathException("selectors cannot select attributes");
161             }
162             }
163     
164         } // <init>(String,StringPool,NamespacesScope)
165

166     } // class Selector.XPath
167

168     /**
169      * Selector matcher.
170      *
171      * @author Andy Clark, IBM
172      */

173     protected class Matcher
174         extends XPathMatcher {
175     
176         //
177
// Data
178
//
179

180         /** Field activator. */
181         protected FieldActivator fFieldActivator;
182
183         /** Element depth. */
184         protected int fElementDepth;
185
186         /** Depth at match. */
187         protected int fMatchedDepth;
188
189         //
190
// Constructors
191
//
192

193         /** Constructs a selector matcher. */
194         public Matcher(Selector.XPath xpath, FieldActivator activator) {
195             super(xpath, false, Selector.this.fIdentityConstraint);
196             fFieldActivator = activator;
197         } // <init>(Selector.XPath,FieldActivator)
198

199         //
200
// XMLDocumentFragmentHandler methods
201
//
202

203         public void startDocumentFragment(StringPool stringPool)
204             throws Exception JavaDoc {
205             super.startDocumentFragment(stringPool);
206             fElementDepth = 0;
207             fMatchedDepth = -1;
208         } // startDocumentFragment(StringPool,NamespacesScope)
209

210         /**
211          * The start of an element. If the document specifies the start element
212          * by using an empty tag, then the startElement method will immediately
213          * be followed by the endElement method, with no intervening methods.
214          *
215          * @param element The name of the element.
216          * @param attributes The element attributes.
217          * @param handle: beginning of the attribute list
218          * @param elemIndex: index of the element holding these attributes
219          * @param grammar: the SchemaGrammar that all this is being validated by
220          *
221          * @throws SAXException Thrown by handler to signal an error.
222          */

223         public void startElement(QName element, XMLAttrList attributes,
224                                  int handle, int elemIndex, SchemaGrammar grammar) throws Exception JavaDoc {
225             super.startElement(element, attributes, handle, elemIndex, grammar);
226             fElementDepth++;
227     
228             // activate the fields, if selector is matched
229
if (fMatchedDepth == -1 && isMatched()) {
230                 fMatchedDepth = fElementDepth;
231                 fFieldActivator.startValueScopeFor(fIdentityConstraint);
232                 int count = fIdentityConstraint.getFieldCount();
233                 for (int i = 0; i < count; i++) {
234                     Field field = fIdentityConstraint.getFieldAt(i);
235                     XPathMatcher matcher = fFieldActivator.activateField(field);
236                     matcher.startElement(element, attributes, handle, elemIndex, grammar);
237                 }
238             }
239     
240         } // startElement(QName,XMLAttrList,int)
241

242         public void endElement(QName element, int elemIndex, SchemaGrammar grammar) throws Exception JavaDoc {
243             super.endElement(element, elemIndex, grammar);
244             if (fElementDepth-- == fMatchedDepth) {
245                 fMatchedDepth = -1;
246                 fFieldActivator.endValueScopeFor(fIdentityConstraint);
247             }
248         }
249
250     } // class Matcher
251

252 } // class Selector
253
Popular Tags