KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > UnionPattern


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.xpath.Env;
32 import com.caucho.xpath.ExprEnvironment;
33 import com.caucho.xpath.XPathException;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 /**
38  * Implementation of the 'a|b' pattern. It's made public
39  * so XSLT can separate the left and right sides.
40  */

41 public class UnionPattern extends AbstractPattern {
42   private AbstractPattern _left;
43   private AbstractPattern _right;
44
45   public UnionPattern(AbstractPattern left, AbstractPattern right)
46   {
47     super(null);
48
49     if (right.getParent() instanceof FromAttributes &&
50         left.getParent() instanceof FromChildren) {
51       _left = right;
52       _right = left;
53     }
54     else {
55       _left = left;
56       _right = right;
57     }
58   }
59
60   /**
61    * Match if either pattern matches.
62    *
63    * @param node the node to test
64    * @param env the variable environment
65    *
66    * @return true if the pattern matches.
67    */

68   public boolean match(Node JavaDoc node, ExprEnvironment env)
69     throws XPathException
70   {
71     return (_left.match(node, env) || _right.match(node, env));
72   }
73
74   /**
75    * Returns true if the nodes are ascending.
76    */

77   public boolean isStrictlyAscending()
78   {
79     // @*|node()
80
if (_left.getParent() instanceof FromAttributes &&
81         _right.getParent() instanceof FromChildren)
82       return true;
83     else
84       return false;
85   }
86
87
88   /**
89    * Creates a new node iterator.
90    *
91    * @param node the starting node
92    * @param env the xpath environment
93    * @param match the axis match pattern
94    *
95    * @return the node iterator
96    */

97   public NodeIterator createNodeIterator(Node JavaDoc node, ExprEnvironment env,
98                                          AbstractPattern match)
99     throws XPathException
100   {
101     NodeIterator leftIter = _left.createNodeIterator(node, env,
102                              _left.copyPosition());
103     NodeIterator rightIter = _right.createNodeIterator(node, env,
104                                _right.copyPosition());
105
106     return new UnionIterator(env, leftIter, rightIter);
107   }
108
109   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
110     throws XPathException
111   {
112     NodeIterator iter = select(node, env);
113
114     int i = 1;
115     while (iter.hasNext()) {
116       if (iter.next() == node)
117     return i;
118       i++;
119     }
120
121     return 0;
122   }
123
124   public int count(Node JavaDoc node, Env env, AbstractPattern pattern)
125     throws XPathException
126   {
127     NodeIterator iter = select(node, env);
128     int count = 0;
129
130     while (iter.hasNext()) {
131       iter.next();
132       count++;
133     }
134
135     return count;
136   }
137
138   /**
139    * Return left node of the union.
140    */

141   public AbstractPattern getLeft()
142   {
143     return _left;
144   }
145
146   /**
147    * Return right node of the union.
148    */

149   public AbstractPattern getRight()
150   {
151     return _right;
152   }
153
154   public String JavaDoc toString()
155   {
156     // XXX: '(' ?? ')'
157
return _left.toString() + "|" + _right.toString();
158   }
159 }
160
Popular Tags