KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001, 2002,2004,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.util.SymbolTable;
21 import org.apache.xerces.xni.NamespaceContext;
22 import org.apache.xerces.xni.QName;
23 import org.apache.xerces.xni.XMLAttributes;
24 import org.apache.xerces.xs.ShortList;
25 import org.apache.xerces.xs.XSTypeDefinition;
26
27 /**
28  * Schema identity constraint selector.
29  *
30  * @xerces.internal
31  *
32  * @author Andy Clark, IBM
33  * @version $Id: Selector.java,v 1.20 2005/05/09 21:03:33 ankitp Exp $
34  */

35 public class Selector {
36
37     //
38
// Data
39
//
40

41     /** XPath. */
42     protected Selector.XPath fXPath;
43
44     /** Identity constraint. */
45     protected IdentityConstraint fIdentityConstraint;
46
47     // the Identity constraint we're the matcher for. Only
48
// used for selectors!
49
protected IdentityConstraint fIDConstraint;
50
51     //
52
// Constructors
53
//
54

55     /** Constructs a selector. */
56     public Selector(Selector.XPath xpath,
57                     IdentityConstraint identityConstraint) {
58         fXPath = xpath;
59         fIdentityConstraint = identityConstraint;
60     } // <init>(Selector.XPath,IdentityConstraint)
61

62     //
63
// Public methods
64
//
65

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

71     /** Returns the identity constraint. */
72     public IdentityConstraint getIDConstraint() {
73         return fIdentityConstraint;
74     } // getIDConstraint():IdentityConstraint
75

76     // factory method
77

78     /** Creates a selector matcher.
79      * @param activator The activator for this selector's fields.
80      * @param initialDepth The depth in the document at which this matcher began its life;
81      * used in correctly handling recursive elements.
82      */

83     public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) {
84         return new Selector.Matcher(fXPath, activator, initialDepth);
85     } // createMatcher(FieldActivator):XPathMatcher
86

87     //
88
// Object methods
89
//
90

91     /** Returns a string representation of this object. */
92     public String JavaDoc toString() {
93         return fXPath.toString();
94     } // toString():String
95

96     //
97
// Classes
98
//
99

100     /**
101      * Schema identity constraint selector XPath expression.
102      *
103      * @author Andy Clark, IBM
104      * @version $Id: Selector.java,v 1.20 2005/05/09 21:03:33 ankitp Exp $
105      */

106     public static class XPath
107     extends org.apache.xerces.impl.xpath.XPath {
108
109         //
110
// Constructors
111
//
112

113         /** Constructs a selector XPath expression. */
114         public XPath(String JavaDoc xpath, SymbolTable symbolTable,
115                      NamespaceContext context) throws XPathException {
116             super(normalize(xpath), symbolTable, context);
117             // verify that an attribute is not selected
118
for (int i=0;i<fLocationPaths.length;i++) {
119                 org.apache.xerces.impl.xpath.XPath.Axis axis =
120                 fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
121                 if (axis.type == XPath.Axis.ATTRIBUTE) {
122                     throw new XPathException("c-selector-xpath");
123                 }
124             }
125
126         } // <init>(String,SymbolTable,NamespacesScope)
127

128         private static String JavaDoc normalize(String JavaDoc xpath) {
129             // NOTE: We have to prefix the selector XPath with "./" in
130
// order to handle selectors such as "." that select
131
// the element container because the fields could be
132
// relative to that element. -Ac
133
// Unless xpath starts with a descendant node -Achille Fokoue
134
// ... or a '.' or a '/' - NG
135
// And we also need to prefix exprs to the right of | with ./ - NG
136
StringBuffer JavaDoc modifiedXPath = new StringBuffer JavaDoc(xpath.length()+5);
137             int unionIndex = -1;
138             do {
139                 if(!(xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))) {
140                     modifiedXPath.append("./");
141                 }
142                 unionIndex = xpath.indexOf('|');
143                 if(unionIndex == -1) {
144                     modifiedXPath.append(xpath);
145                     break;
146                 }
147                 modifiedXPath.append(xpath.substring(0,unionIndex+1));
148                 xpath = xpath.substring(unionIndex+1, xpath.length());
149             } while(true);
150             return modifiedXPath.toString();
151         }
152
153     } // class Selector.XPath
154

155     /**
156      * Selector matcher.
157      *
158      * @author Andy Clark, IBM
159      */

160     public class Matcher
161     extends XPathMatcher {
162
163         //
164
// Data
165
//
166

167         /** Field activator. */
168         protected FieldActivator fFieldActivator;
169
170         /** Initial depth in the document at which this matcher was created. */
171         protected int fInitialDepth;
172
173         /** Element depth. */
174         protected int fElementDepth;
175
176         /** Depth at match. */
177         protected int fMatchedDepth;
178
179         //
180
// Constructors
181
//
182

183         /** Constructs a selector matcher. */
184         public Matcher(Selector.XPath xpath, FieldActivator activator,
185                 int initialDepth) {
186             super(xpath);
187             fFieldActivator = activator;
188             fInitialDepth = initialDepth;
189         } // <init>(Selector.XPath,FieldActivator)
190

191         //
192
// XMLDocumentFragmentHandler methods
193
//
194

195         public void startDocumentFragment(){
196             super.startDocumentFragment();
197             fElementDepth = 0;
198             fMatchedDepth = -1;
199         } // startDocumentFragment()
200

201         /**
202          * The start of an element. If the document specifies the start element
203          * by using an empty tag, then the startElement method will immediately
204          * be followed by the endElement method, with no intervening methods.
205          *
206          * @param element The name of the element.
207          * @param attributes The element attributes.
208          *
209          */

210         public void startElement(QName element, XMLAttributes attributes) {
211             super.startElement(element, attributes);
212             fElementDepth++;
213             // activate the fields, if selector is matched
214
//int matched = isMatched();
215

216             if (isMatched()) {
217 /* (fMatchedDepth == -1 && ((matched & MATCHED) == MATCHED)) ||
218                     ((matched & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)) { */

219                 fMatchedDepth = fElementDepth;
220                 fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth);
221                 int count = fIdentityConstraint.getFieldCount();
222                 for (int i = 0; i < count; i++) {
223                     Field field = fIdentityConstraint.getFieldAt(i);
224                     XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth);
225                     matcher.startElement(element, attributes);
226                 }
227             }
228
229         } // startElement(QName,XMLAttrList,int)
230

231         public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object JavaDoc actualValue, short valueType, ShortList itemValueType) {
232             super.endElement(element, type, nillable, actualValue, valueType, itemValueType);
233             if (fElementDepth-- == fMatchedDepth) {
234                 fMatchedDepth = -1;
235                 fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth);
236             }
237         }
238
239         /** Returns the identity constraint. */
240         public IdentityConstraint getIdentityConstraint() {
241             return fIdentityConstraint;
242         } // getIdentityConstraint():IdentityConstraint
243

244         /** get the initial depth at which this selector matched. */
245         public int getInitialDepth() {
246             return fInitialDepth;
247         } // getInitialDepth(): int
248

249
250     } // class Matcher
251

252 } // class Selector
253
Popular Tags