KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.expr.StaticContext;
3 import net.sf.saxon.expr.XPathContext;
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
9 /**
10 * A pattern formed as the union (or) of two other patterns
11 */

12
13 public class UnionPattern extends Pattern {
14
15     protected Pattern p1, p2;
16     private int nodeType = Type.NODE;
17
18     /**
19     * Constructor
20     * @param p1 the left-hand operand
21     * @param p2 the right-hand operand
22     */

23
24     public UnionPattern(Pattern p1, Pattern p2) {
25         this.p1 = p1;
26         this.p2 = p2;
27         if (p1.getNodeKind()==p2.getNodeKind()) {
28             nodeType = p1.getNodeKind();
29         }
30     }
31
32     /**
33     * Simplify the pattern: perform any context-independent optimisations
34     */

35
36     public Pattern simplify(StaticContext env) throws XPathException {
37         p1 = p1.simplify(env);
38         p2 = p2.simplify(env);
39         return this;
40     }
41
42     /**
43     * Type-check the pattern.
44     * This is only needed for patterns that contain variable references or function calls.
45     * @return the optimised Pattern
46     */

47
48     public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException {
49         p1 = p1.analyze(env, contextItemType);
50         p2 = p2.analyze(env, contextItemType);
51         return this;
52     }
53
54     /**
55     * Set the original text
56     */

57
58     public void setOriginalText(String JavaDoc pattern) {
59         super.setOriginalText(pattern);
60         p1.setOriginalText(pattern);
61         p2.setOriginalText(pattern);
62     }
63
64     /**
65     * Determine if the supplied node matches the pattern
66     * @param e the node to be compared
67     * @return true if the node matches either of the operand patterns
68     */

69
70     public boolean matches(NodeInfo e, XPathContext context) throws XPathException {
71         return p1.matches(e, context) || p2.matches(e, context);
72     }
73
74     /**
75     * Determine the types of nodes to which this pattern applies. Used for optimisation.
76     * For patterns that match nodes of several types, return Node.NODE
77     * @return the type of node matched by this pattern. e.g. Node.ELEMENT or Node.TEXT
78     */

79
80     public int getNodeKind() {
81         return nodeType;
82     }
83
84     /**
85     * Get a NodeTest that all the nodes matching this pattern must satisfy
86     */

87
88     public NodeTest getNodeTest() {
89         if (nodeType==Type.NODE) {
90             return AnyNodeTest.getInstance();
91         } else {
92             return NodeKindTest.makeNodeKindTest(nodeType);
93         }
94     }
95
96     /**
97     * Get the LHS of the union
98     */

99
100     public Pattern getLHS() {
101         return p1;
102     }
103
104     /**
105     * Get the RHS of the union
106     */

107
108     public Pattern getRHS() {
109         return p2;
110     }
111
112     /**
113      * Override method to set the system ID, so it's set on both halves
114      */

115
116     public void setSystemId(String JavaDoc systemId) {
117         super.setSystemId(systemId);
118         p1.setSystemId(systemId);
119         p2.setSystemId(systemId);
120     }
121
122     /**
123      * Override method to set the system ID, so it's set on both halves
124      */

125
126     public void setLineNumber(int lineNumber) {
127         super.setLineNumber(lineNumber);
128         p1.setLineNumber(lineNumber);
129         p2.setLineNumber(lineNumber);
130     }
131 }
132
133 //
134
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
135
// you may not use this file except in compliance with the License. You may obtain a copy of the
136
// License at http://www.mozilla.org/MPL/
137
//
138
// Software distributed under the License is distributed on an "AS IS" basis,
139
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
140
// See the License for the specific language governing rights and limitations under the License.
141
//
142
// The Original Code is: all this file.
143
//
144
// The Initial Developer of the Original Code is Michael H. Kay.
145
//
146
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
147
//
148
// Contributor(s): none.
149
//
150
Popular Tags