KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > pattern > IDPattern


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.expr.PromotionOffer;
6 import net.sf.saxon.om.DocumentInfo;
7 import net.sf.saxon.om.NodeInfo;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.type.Type;
11 import net.sf.saxon.value.AtomicValue;
12
13 import java.util.StringTokenizer JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17 * An IDPattern is a pattern of the form id("literal") or id($variable)
18 */

19
20 public final class IDPattern extends Pattern {
21
22     private Expression idExpression;
23
24     /**
25      * Create an id pattern.
26      * @param id Either a StringValue or a VariableReference
27      */

28     public IDPattern(Expression id) {
29         idExpression = id;
30     }
31
32     /**
33     * Type-check the pattern.
34     * Default implementation does nothing. This is only needed for patterns that contain
35     * variable references or function calls.
36     * @return the optimised Pattern
37     */

38
39     public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException {
40         idExpression = idExpression.typeCheck(env, contextItemType);
41         return this;
42     }
43
44     /**
45      * Get the dependencies of the pattern. The only possible dependency for a pattern is
46      * on local variables. This is analyzed in those patterns where local variables may appear.
47      */

48
49     public int getDependencies() {
50         return idExpression.getDependencies();
51     }
52
53     /**
54      * Iterate over the subexpressions within this pattern
55      */

56
57     public Iterator JavaDoc iterateSubExpressions() {
58         return idExpression.iterateSubExpressions();
59     }
60
61     /**
62      * Offer promotion for subexpressions within this pattern. The offer will be accepted if the subexpression
63      * is not dependent on the factors (e.g. the context item) identified in the PromotionOffer.
64      * By default the offer is not accepted - this is appropriate in the case of simple expressions
65      * such as constant values and variable references where promotion would give no performance
66      * advantage. This method is always called at compile time.
67      * <p/>
68      * <p>Unlike the corresponding method on {@link net.sf.saxon.expr.Expression}, this method does not return anything:
69      * it can make internal changes to the pattern, but cannot return a different pattern. Only certain
70      * kinds of promotion are applicable within a pattern: specifically, promotions affecting local
71      * variable references within the pattern.
72      *
73      * @param offer details of the offer, for example the offer to move
74      * expressions that don't depend on the context to an outer level in
75      * the containing expression
76      * @throws net.sf.saxon.trans.XPathException
77      * if any error is detected
78      */

79
80     public void promote(PromotionOffer offer) throws XPathException {
81         idExpression = idExpression.promote(offer);
82     }
83
84     /**
85     * Determine whether this Pattern matches the given Node
86     * @param e The NodeInfo representing the Element or other node to be tested against the Pattern
87     * @return true if the node matches the Pattern, false otherwise
88     */

89
90     public boolean matches(NodeInfo e, XPathContext context) throws XPathException {
91         if (e.getNodeKind() != Type.ELEMENT) {
92             return false;
93         }
94         DocumentInfo doc = e.getDocumentRoot();
95         if (doc==null) {
96             return false;
97         }
98         AtomicValue idValue = (AtomicValue)idExpression.evaluateItem(context);
99         if (idValue == null) {
100             return false;
101         }
102         String JavaDoc ids = idValue.getStringValue();
103         if (ids.indexOf(' ') < 0 &&
104                 ids.indexOf(0x09) < 0 &&
105                 ids.indexOf(0x0a) < 0 &&
106                 ids.indexOf(0x0c) < 0) {
107             NodeInfo element = doc.selectID(ids);
108             if (element==null) return false;
109             return (element.isSameNodeInfo(e));
110         } else {
111             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(ids);
112             while (tokenizer.hasMoreElements()) {
113                 String JavaDoc id = (String JavaDoc)tokenizer.nextElement();
114                 NodeInfo element = doc.selectID(id);
115                 if (element != null && e.isSameNodeInfo(element)) {
116                     return true;
117                 }
118             }
119             return false;
120         }
121
122     }
123
124     /**
125     * Determine the type of nodes to which this pattern applies.
126     * @return Type.ELEMENT
127     */

128
129     public int getNodeKind() {
130         return Type.ELEMENT;
131     }
132
133     /**
134     * Get a NodeTest that all the nodes matching this pattern must satisfy
135     */

136
137     public NodeTest getNodeTest() {
138         return NodeKindTest.ELEMENT;
139     }
140
141 }
142
143 //
144
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
145
// you may not use this file except in compliance with the License. You may obtain a copy of the
146
// License at http://www.mozilla.org/MPL/
147
//
148
// Software distributed under the License is distributed on an "AS IS" basis,
149
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
150
// See the License for the specific language governing rights and limitations under the License.
151
//
152
// The Original Code is: all this file.
153
//
154
// The Initial Developer of the Original Code is Michael H. Kay.
155
//
156
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
157
//
158
// Contributor(s): none.
159
//
160
Popular Tags