KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 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.XSTypeDefinition;
62 import com.sun.org.apache.xerces.internal.util.SymbolTable;
63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
64 import com.sun.org.apache.xerces.internal.xni.QName;
65 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
66
67 /**
68  * Schema identity constraint selector.
69  *
70  * @author Andy Clark, IBM
71  * @version $Id: Selector.java,v 1.16 2003/11/15 22:03:22 neilg 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     // the Identity constraint we're the matcher for. Only
86
// used for selectors!
87
protected IdentityConstraint fIDConstraint;
88
89     //
90
// Constructors
91
//
92

93     /** Constructs a selector. */
94     public Selector(Selector.XPath xpath,
95                     IdentityConstraint identityConstraint) {
96         fXPath = xpath;
97         fIdentityConstraint = identityConstraint;
98     } // <init>(Selector.XPath,IdentityConstraint)
99

100     //
101
// Public methods
102
//
103

104     /** Returns the selector XPath. */
105     public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() {
106         return fXPath;
107     } // getXPath():com.sun.org.apache.xerces.internal.v1.schema.identity.XPath
108

109     /** Returns the identity constraint. */
110     public IdentityConstraint getIDConstraint() {
111         return fIdentityConstraint;
112     } // getIDConstraint():IdentityConstraint
113

114     // factory method
115

116     /** Creates a selector matcher.
117      * @param activator The activator for this selector's fields.
118      * @param initialDepth The depth in the document at which this matcher began its life;
119      * used in correctly handling recursive elements.
120      */

121     public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) {
122         return new Selector.Matcher(fXPath, activator, initialDepth);
123     } // createMatcher(FieldActivator):XPathMatcher
124

125     //
126
// Object methods
127
//
128

129     /** Returns a string representation of this object. */
130     public String JavaDoc toString() {
131         return fXPath.toString();
132     } // toString():String
133

134     //
135
// Classes
136
//
137

138     /**
139      * Schema identity constraint selector XPath expression.
140      *
141      * @author Andy Clark, IBM
142      * @version $Id: Selector.java,v 1.16 2003/11/15 22:03:22 neilg Exp $
143      */

144     public static class XPath
145     extends com.sun.org.apache.xerces.internal.impl.xpath.XPath {
146
147         //
148
// Constructors
149
//
150

151         /** Constructs a selector XPath expression. */
152         public XPath(String JavaDoc xpath, SymbolTable symbolTable,
153                      NamespaceContext context) throws XPathException {
154             super(normalize(xpath), symbolTable, context);
155             // verify that an attribute is not selected
156
for (int i=0;i<fLocationPaths.length;i++) {
157                 com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis =
158                 fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
159                 if (axis.type == XPath.Axis.ATTRIBUTE) {
160                     throw new XPathException("c-selector-xpath");
161                 }
162             }
163
164         } // <init>(String,SymbolTable,NamespacesScope)
165

166         private static String JavaDoc normalize(String JavaDoc xpath) {
167             // NOTE: We have to prefix the selector XPath with "./" in
168
// order to handle selectors such as "." that select
169
// the element container because the fields could be
170
// relative to that element. -Ac
171
// Unless xpath starts with a descendant node -Achille Fokoue
172
// ... or a '.' or a '/' - NG
173
// And we also need to prefix exprs to the right of | with ./ - NG
174
StringBuffer JavaDoc modifiedXPath = new StringBuffer JavaDoc(xpath.length()+5);
175             int unionIndex = -1;
176             do {
177                 if(!(xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))) {
178                     modifiedXPath.append("./");
179                 }
180                 unionIndex = xpath.indexOf('|');
181                 if(unionIndex == -1) {
182                     modifiedXPath.append(xpath);
183                     break;
184                 }
185                 modifiedXPath.append(xpath.substring(0,unionIndex+1));
186                 xpath = xpath.substring(unionIndex+1, xpath.length());
187             } while(true);
188             return modifiedXPath.toString();
189         }
190
191     } // class Selector.XPath
192

193     /**
194      * Selector matcher.
195      *
196      * @author Andy Clark, IBM
197      */

198     public class Matcher
199     extends XPathMatcher {
200
201         //
202
// Data
203
//
204

205         /** Field activator. */
206         protected FieldActivator fFieldActivator;
207
208         /** Initial depth in the document at which this matcher was created. */
209         protected int fInitialDepth;
210
211         /** Element depth. */
212         protected int fElementDepth;
213
214         /** Depth at match. */
215         protected int fMatchedDepth;
216
217         //
218
// Constructors
219
//
220

221         /** Constructs a selector matcher. */
222         public Matcher(Selector.XPath xpath, FieldActivator activator,
223                 int initialDepth) {
224             super(xpath);
225             fFieldActivator = activator;
226             fInitialDepth = initialDepth;
227         } // <init>(Selector.XPath,FieldActivator)
228

229         //
230
// XMLDocumentFragmentHandler methods
231
//
232

233         public void startDocumentFragment(){
234             super.startDocumentFragment();
235             fElementDepth = 0;
236             fMatchedDepth = -1;
237         } // startDocumentFragment()
238

239         /**
240          * The start of an element. If the document specifies the start element
241          * by using an empty tag, then the startElement method will immediately
242          * be followed by the endElement method, with no intervening methods.
243          *
244          * @param element The name of the element.
245          * @param attributes The element attributes.
246          * @param elementDecl: The element declaration
247          *
248          */

249         public void startElement(QName element, XMLAttributes attributes) {
250             super.startElement(element, attributes);
251             fElementDepth++;
252             // activate the fields, if selector is matched
253
//int matched = isMatched();
254

255             if (isMatched()) {
256 /* (fMatchedDepth == -1 && ((matched & MATCHED) == MATCHED)) ||
257                     ((matched & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)) { */

258                 fMatchedDepth = fElementDepth;
259                 fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth);
260                 int count = fIdentityConstraint.getFieldCount();
261                 for (int i = 0; i < count; i++) {
262                     Field field = fIdentityConstraint.getFieldAt(i);
263                     XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth);
264                     matcher.startElement(element, attributes);
265                 }
266             }
267
268         } // startElement(QName,XMLAttrList,int)
269

270         public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object JavaDoc actualValue) {
271             super.endElement(element, type, nillable, actualValue);
272             if (fElementDepth-- == fMatchedDepth) {
273                 fMatchedDepth = -1;
274                 fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth);
275             }
276         }
277
278         /** Returns the identity constraint. */
279         public IdentityConstraint getIdentityConstraint() {
280             return fIdentityConstraint;
281         } // getIdentityConstraint():IdentityConstraint
282

283         /** get the initial depth at which this selector matched. */
284         public int getInitialDepth() {
285             return fInitialDepth;
286         } // getInitialDepth(): int
287

288
289     } // class Matcher
290

291 } // class Selector
292
Popular Tags