KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.instruct.Executable;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.ItemType;
7 import net.sf.saxon.type.Type;
8 import net.sf.saxon.event.LocationProvider;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Collections JavaDoc;
13
14 /**
15 * A Pattern represents the result of parsing an XSLT pattern string. <br>
16 * Patterns are created by calling the static method Pattern.make(string). <br>
17 * The pattern is used to test a particular node by calling match().
18 */

19
20 public abstract class Pattern implements Serializable JavaDoc, Container {
21
22     private String JavaDoc originalText;
23     private Executable executable;
24     private String JavaDoc systemId; // the module where the pattern occurred
25
private int lineNumber; // the line number where the pattern occurred
26

27     /**
28     * Static method to make a Pattern by parsing a String. <br>
29     * @param pattern The pattern text as a String
30     * @param env An object defining the compile-time context for the expression
31     * @return The pattern object
32     */

33
34     public static Pattern make(String JavaDoc pattern, StaticContext env, Executable exec) throws XPathException {
35
36         Pattern pat = (new ExpressionParser()).parsePattern(pattern, env);
37         pat.setSystemId(env.getSystemId());
38         pat.setLineNumber(env.getLineNumber());
39         // System.err.println("Simplified [" + pattern + "] to " + pat.getClass() + " default prio = " + pat.getDefaultPriority());
40
// set the pattern text for use in diagnostics
41
pat.setOriginalText(pattern);
42         pat.setExecutable(exec);
43         pat = pat.simplify(env);
44         return pat;
45     }
46
47
48     public Executable getExecutable() {
49         return executable;
50     }
51
52     public void setExecutable(Executable executable) {
53         this.executable = executable;
54     }
55
56     /**
57      * Get the LocationProvider allowing location identifiers to be resolved.
58      */

59
60     public LocationProvider getLocationProvider() {
61         return executable.getLocationMap();
62     }
63
64
65     /**
66     * Set the original text of the pattern for use in diagnostics
67     */

68
69     public void setOriginalText(String JavaDoc text) {
70         originalText = text;
71     }
72
73     /**
74     * Simplify the pattern by applying any context-independent optimisations.
75     * Default implementation does nothing.
76     * @return the optimised Pattern
77     */

78
79     public Pattern simplify(StaticContext env) throws XPathException {
80         return this;
81     }
82
83     /**
84     * Type-check the pattern.
85     * Default implementation does nothing. This is only needed for patterns that contain
86     * variable references or function calls.
87     * @return the optimised Pattern
88     */

89
90     public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException {
91         return this;
92     }
93
94     /**
95      * Get the dependencies of the pattern. The only possible dependency for a pattern is
96      * on local variables. This is analyzed in those patterns where local variables may appear.
97      */

98
99     public int getDependencies() {
100         return 0;
101     }
102
103     /**
104      * Iterate over the subexpressions within this pattern
105      */

106
107     public Iterator JavaDoc iterateSubExpressions() {
108         return Collections.EMPTY_LIST.iterator();
109     }
110
111     /**
112      * Offer promotion for subexpressions within this pattern. The offer will be accepted if the subexpression
113      * is not dependent on the factors (e.g. the context item) identified in the PromotionOffer.
114      * By default the offer is not accepted - this is appropriate in the case of simple expressions
115      * such as constant values and variable references where promotion would give no performance
116      * advantage. This method is always called at compile time.
117      *
118      * <p>Unlike the corresponding method on {@link Expression}, this method does not return anything:
119      * it can make internal changes to the pattern, but cannot return a different pattern. Only certain
120      * kinds of promotion are applicable within a pattern: specifically, promotions affecting local
121      * variable references within the pattern.
122      *
123      * @param offer details of the offer, for example the offer to move
124      * expressions that don't depend on the context to an outer level in
125      * the containing expression
126      * @throws net.sf.saxon.trans.XPathException
127      * if any error is detected
128      */

129
130     public void promote(PromotionOffer offer) throws XPathException {
131         // default implementation does nothing
132
}
133
134     /**
135     * Set the system ID where the pattern occurred
136     */

137
138     public void setSystemId(String JavaDoc systemId) {
139         this.systemId = systemId;
140     }
141
142     /**
143     * Set the line number where the pattern occurred
144     */

145
146     public void setLineNumber(int lineNumber) {
147         this.lineNumber = lineNumber;
148     }
149
150     /**
151     * Determine whether this Pattern matches the given Node. This is the main external interface
152     * for matching patterns: it sets current() to the node being tested
153     * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
154     * @param context The dynamic context. Only relevant if the pattern
155     * uses variables, or contains calls on functions such as document() or key().
156     * @return true if the node matches the Pattern, false otherwise
157     */

158
159     public abstract boolean matches(NodeInfo node, XPathContext context) throws XPathException;
160
161     /**
162     * Determine whether this Pattern matches the given Node. This is an internal interface used
163     * for matching sub-patterns; it does not alter the value of current(). The default implementation
164     * is identical to matches().
165     * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
166     * @param context The dynamic context. Only relevant if the pattern
167     * uses variables, or contains calls on functions such as document() or key().
168     * @return true if the node matches the Pattern, false otherwise
169     */

170
171     protected boolean internalMatches(NodeInfo node, XPathContext context) throws XPathException {
172         return matches(node, context);
173     }
174
175     /**
176     * Determine the types of nodes to which this pattern applies. Used for optimisation.
177     * For patterns that match nodes of several types, return Type.NODE
178     * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
179     */

180
181     public int getNodeKind() {
182         return Type.NODE;
183     }
184
185     /**
186     * Determine the name fingerprint of nodes to which this pattern applies. Used for
187     * optimisation.
188     * @return A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints
189     */

190
191     public int getFingerprint() {
192         return -1;
193     }
194
195     /**
196     * Get a NodeTest that all the nodes matching this pattern must satisfy
197     */

198
199     public abstract NodeTest getNodeTest();
200
201     /**
202     * Determine the default priority to use if this pattern appears as a match pattern
203     * for a template with no explicit priority attribute.
204     */

205
206     public double getDefaultPriority() {
207         return 0.5;
208     }
209
210     /**
211     * Get the system id of the entity in which the pattern occurred
212     */

213
214     public String JavaDoc getSystemId() {
215         return systemId;
216     }
217
218     /**
219     * Get the line number on which the pattern was defined
220     */

221
222     public int getLineNumber() {
223         return lineNumber;
224     }
225
226     /**
227      * Get the column number (always -1)
228      */

229
230     public int getColumnNumber() {
231         return -1;
232     }
233
234     /**
235      * Get the public ID (always null)
236      */

237
238     public String JavaDoc getPublicId() {
239         return null;
240     }
241     /**
242     * Get the original pattern text
243     */

244
245     public String JavaDoc toString() {
246         return originalText;
247     }
248
249 }
250
251 //
252
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
253
// you may not use this file except in compliance with the License. You may obtain a copy of the
254
// License at http://www.mozilla.org/MPL/
255
//
256
// Software distributed under the License is distributed on an "AS IS" basis,
257
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
258
// See the License for the specific language governing rights and limitations under the License.
259
//
260
// The Original Code is: all this file.
261
//
262
// The Initial Developer of the Original Code is Michael H. Kay.
263
//
264
// Contributor(s): Michael Kay
265
//
266
Popular Tags