KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.pattern;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.expr.XPathException;
5
6 /**
7 * A pattern formed as the union (or) of two other patterns
8 */

9
10 public class UnionPattern extends Pattern {
11
12     protected Pattern p1, p2;
13     private short nodeType = NodeInfo.NODE;
14
15     /**
16     * Constructor
17     * @param p1 the left-hand operand
18     * @param p2 the right-hand operand
19     */

20
21     public UnionPattern(Pattern p1, Pattern p2) {
22         this.p1 = p1;
23         this.p2 = p2;
24         if (p1.getNodeType()==p2.getNodeType()) nodeType = p1.getNodeType();
25     }
26
27     /**
28     * Simplify the pattern: perform any context-independent optimisations
29     */

30
31     public Pattern simplify() throws XPathException {
32         return new UnionPattern(p1.simplify(), p2.simplify());
33     }
34
35     /**
36     * Set the original text
37     */

38     
39     public void setOriginalText(String JavaDoc pattern) {
40         this.originalText = pattern;
41         p1.setOriginalText(pattern);
42         p2.setOriginalText(pattern);
43     }
44
45     /**
46     * Determine if the supplied node matches the pattern
47     * @e the node to be compared
48     * @return true if the node matches either of the operand patterns
49     */

50
51     public boolean matches(NodeInfo e, Context c) throws XPathException {
52         return p1.matches(e, c) || p2.matches(e, c);
53     }
54
55     /**
56     * Determine the types of nodes to which this pattern applies. Used for optimisation.
57     * For patterns that match nodes of several types, return Node.NODE
58     * @return the type of node matched by this pattern. e.g. Node.ELEMENT or Node.TEXT
59     */

60
61     public short getNodeType() {
62         return nodeType;
63     }
64
65     /**
66     * Get the LHS of the union
67     */

68
69     public Pattern getLHS() {
70         return p1;
71     }
72
73     /**
74     * Get the RHS of the union
75     */

76
77     public Pattern getRHS() {
78         return p2;
79     }
80
81 }
82
83 //
84
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
85
// you may not use this file except in compliance with the License. You may obtain a copy of the
86
// License at http://www.mozilla.org/MPL/
87
//
88
// Software distributed under the License is distributed on an "AS IS" basis,
89
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
90
// See the License for the specific language governing rights and limitations under the License.
91
//
92
// The Original Code is: all this file.
93
//
94
// The Initial Developer of the Original Code is
95
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
96
//
97
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
98
//
99
// Contributor(s): none.
100
//
101
Popular Tags