KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > pattern > Pattern


1 package com.icl.saxon.pattern;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.expr.StaticContext;
4 import com.icl.saxon.expr.ExpressionParser;
5 import com.icl.saxon.om.NodeInfo;
6 import com.icl.saxon.expr.XPathException;
7
8
9 /**
10 * A Pattern represents the result of parsing an XSLT pattern string. <br>
11 * Patterns are created by calling the static method Pattern.make(string). <br>
12 * The pattern is used to test a particular node by calling match().
13 */

14
15 public abstract class Pattern {
16
17     protected StaticContext staticContext;
18     protected String JavaDoc originalText;
19
20     /**
21     * Static method to make a Pattern by parsing a String. <br>
22     * @param pattern The pattern text as a String
23     * @param env An object defining the compile-time context for the expression
24     * @return The pattern object
25     */

26
27     public static Pattern make(String JavaDoc pattern, StaticContext env) throws XPathException {
28
29         Pattern pat = (new ExpressionParser()).parsePattern(pattern, env).simplify();
30         // previously used a shared parser instance: this wasn't thread-safe (bug 4.5/005)
31
pat.staticContext = env;
32         
33         // set the pattern text for use in diagnostics
34
pat.setOriginalText(pattern);
35         return pat;
36     }
37
38     /**
39     * Set the original text of the pattern for use in diagnostics
40     */

41     
42     public void setOriginalText(String JavaDoc text) {
43         originalText = text;
44     }
45
46     /**
47     * Simplify the pattern by applying any context-independent optimisations.
48     * Default implementation does nothing.
49     * @return the optimised Pattern
50     */

51
52     public Pattern simplify() throws XPathException {
53         return this;
54     }
55
56     /**
57     * Set the static context used when the pattern was parsed
58     */

59
60     public final void setStaticContext(StaticContext sc) {
61         staticContext = sc;
62     }
63
64     /**
65     * Determine the static context used when the pattern was parsed
66     */

67
68     public StaticContext getStaticContext() {
69         return staticContext;
70     }
71
72     /**
73     * Determine whether this Pattern matches the given Node
74     * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
75     * @param context The context in which the match is to take place. Only relevant if the pattern
76     * uses variables.
77     * @return true if the node matches the Pattern, false otherwise
78     */

79
80     public abstract boolean matches(NodeInfo node, Context context) throws XPathException;
81
82     /**
83     * Determine the types of nodes to which this pattern applies. Used for optimisation.
84     * For patterns that match nodes of several types, return NodeInfo.NODE
85     * @return the type of node matched by this pattern. e.g. NodeInfo.ELEMENT or NodeInfo.TEXT
86     */

87
88     public short getNodeType() {
89         return NodeInfo.NODE;
90     }
91
92     /**
93     * Determine the name fingerprint of nodes to which this pattern applies. Used for
94     * optimisation.
95     * @return A fingerprint that the nodes must match, or null
96     * Otherwise return null.
97     */

98
99     public int getFingerprint() {
100         return -1;
101     }
102
103     /**
104     * Determine the default priority to use if this pattern appears as a match pattern
105     * for a template with no explicit priority attribute.
106     */

107
108     public double getDefaultPriority() {
109         return 0.5;
110     }
111
112     /**
113     * Get the system id of the entity in which the pattern occurred
114     */

115     
116     public String JavaDoc getSystemId() {
117         return staticContext.getSystemId();
118     }
119     
120     /**
121     * Get the line number on which the pattern was defined
122     */

123     
124     public int getLineNumber() {
125         return staticContext.getLineNumber();
126     }
127     
128     /**
129     * Get the original pattern text
130     */

131     
132     public String JavaDoc toString() {
133         return originalText;
134     }
135
136 }
137
138 //
139
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
140
// you may not use this file except in compliance with the License. You may obtain a copy of the
141
// License at http://www.mozilla.org/MPL/
142
//
143
// Software distributed under the License is distributed on an "AS IS" basis,
144
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
145
// See the License for the specific language governing rights and limitations under the License.
146
//
147
// The Original Code is: all this file.
148
//
149
// The Initial Developer of the Original Code is
150
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
151
//
152
// The line marked PB-SYNC is by Peter Bryant (pbryant@bigfoot.com). All Rights Reserved.
153
//
154
// Contributor(s): Michael Kay, Peter Bryant
155
//
156
Popular Tags