001: package net.sf.saxon.pattern;
002:
003: import net.sf.saxon.expr.StaticContext;
004: import net.sf.saxon.expr.XPathContext;
005: import net.sf.saxon.om.NodeInfo;
006: import net.sf.saxon.trans.XPathException;
007: import net.sf.saxon.type.ItemType;
008: import net.sf.saxon.type.Type;
009:
010: /**
011: * A pattern formed as the union (or) of two other patterns
012: */
013:
014: public class UnionPattern extends Pattern {
015:
016: protected Pattern p1, p2;
017: private int nodeType = Type.NODE;
018:
019: /**
020: * Constructor
021: * @param p1 the left-hand operand
022: * @param p2 the right-hand operand
023: */
024:
025: public UnionPattern(Pattern p1, Pattern p2) {
026: this .p1 = p1;
027: this .p2 = p2;
028: if (p1.getNodeKind() == p2.getNodeKind()) {
029: nodeType = p1.getNodeKind();
030: }
031: }
032:
033: /**
034: * Simplify the pattern: perform any context-independent optimisations
035: */
036:
037: public Pattern simplify(StaticContext env) throws XPathException {
038: p1 = p1.simplify(env);
039: p2 = p2.simplify(env);
040: return this ;
041: }
042:
043: /**
044: * Type-check the pattern.
045: * This is only needed for patterns that contain variable references or function calls.
046: * @return the optimised Pattern
047: */
048:
049: public Pattern analyze(StaticContext env, ItemType contextItemType)
050: throws XPathException {
051: p1 = p1.analyze(env, contextItemType);
052: p2 = p2.analyze(env, contextItemType);
053: return this ;
054: }
055:
056: /**
057: * Set the original text
058: */
059:
060: public void setOriginalText(String pattern) {
061: super .setOriginalText(pattern);
062: p1.setOriginalText(pattern);
063: p2.setOriginalText(pattern);
064: }
065:
066: /**
067: * Determine if the supplied node matches the pattern
068: * @param e the node to be compared
069: * @return true if the node matches either of the operand patterns
070: */
071:
072: public boolean matches(NodeInfo e, XPathContext context)
073: throws XPathException {
074: return p1.matches(e, context) || p2.matches(e, context);
075: }
076:
077: /**
078: * Determine the types of nodes to which this pattern applies. Used for optimisation.
079: * For patterns that match nodes of several types, return Node.NODE
080: * @return the type of node matched by this pattern. e.g. Node.ELEMENT or Node.TEXT
081: */
082:
083: public int getNodeKind() {
084: return nodeType;
085: }
086:
087: /**
088: * Get a NodeTest that all the nodes matching this pattern must satisfy
089: */
090:
091: public NodeTest getNodeTest() {
092: if (nodeType == Type.NODE) {
093: return AnyNodeTest.getInstance();
094: } else {
095: return NodeKindTest.makeNodeKindTest(nodeType);
096: }
097: }
098:
099: /**
100: * Get the LHS of the union
101: */
102:
103: public Pattern getLHS() {
104: return p1;
105: }
106:
107: /**
108: * Get the RHS of the union
109: */
110:
111: public Pattern getRHS() {
112: return p2;
113: }
114:
115: /**
116: * Override method to set the system ID, so it's set on both halves
117: */
118:
119: public void setSystemId(String systemId) {
120: super .setSystemId(systemId);
121: p1.setSystemId(systemId);
122: p2.setSystemId(systemId);
123: }
124:
125: /**
126: * Override method to set the system ID, so it's set on both halves
127: */
128:
129: public void setLineNumber(int lineNumber) {
130: super .setLineNumber(lineNumber);
131: p1.setLineNumber(lineNumber);
132: p2.setLineNumber(lineNumber);
133: }
134: }
135:
136: //
137: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
138: // you may not use this file except in compliance with the License. You may obtain a copy of the
139: // License at http://www.mozilla.org/MPL/
140: //
141: // Software distributed under the License is distributed on an "AS IS" basis,
142: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
143: // See the License for the specific language governing rights and limitations under the License.
144: //
145: // The Original Code is: all this file.
146: //
147: // The Initial Developer of the Original Code is Michael H. Kay.
148: //
149: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
150: //
151: // Contributor(s): none.
152: //
|