KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.XmlUtil;
32 import com.caucho.xpath.Env;
33 import com.caucho.xpath.ExprEnvironment;
34 import com.caucho.xpath.XPathException;
35
36 import org.w3c.dom.Node JavaDoc;
37
38 /**
39  * Implements the ancestor:: axis.
40  */

41 public class FromAncestors extends Axis {
42   private boolean _self;
43
44   public FromAncestors(AbstractPattern parent, boolean self)
45   {
46     super(parent)
47       ;
48     _self = self;
49
50     if (parent == null)
51       throw new RuntimeException JavaDoc();
52   }
53   /**
54    * Matches if a descendant matches the parent pattern.
55    *
56    * @param node the current node
57    * @param env the variable environment
58    *
59    * @return true if the pattern matches
60    */

61   public boolean match(Node JavaDoc node, ExprEnvironment env)
62     throws XPathException
63   {
64     if (node == null)
65       return false;
66
67     Node JavaDoc lastNode = lastDescendantNode(node);
68     
69     if (! _self)
70       node = XmlUtil.getNext(node);
71     
72     for (;
73      node != null && node != lastNode;
74      node = XmlUtil.getNext(node)) {
75       if (_parent.match(node, env))
76     return true;
77     }
78     
79     return false;
80   }
81
82   public boolean isAscending()
83   {
84     return false;
85   }
86
87   /**
88    * Returns the first node in the selection order.
89    *
90    * @param node the current node
91    *
92    * @return the first node
93    */

94   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
95   {
96     if (_self)
97       return node;
98     else
99       return node.getParentNode();
100   }
101
102   /**
103    * Returns the next node in the selection order.
104    *
105    * @param node the current node
106    * @param last the last node
107    *
108    * @return the next node
109    */

110   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc last)
111   {
112     return (node == null) ? null : node.getParentNode();
113   }
114
115   /**
116    * The ancestor position is the number of matching nodes between it
117    * and an axis-context.
118    */

119   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
120     throws XPathException
121   {
122     int index = env.getPositionIndex();
123
124     Node JavaDoc lastNode = lastDescendantNode(node);
125
126     Node JavaDoc axis = _self ? node : XmlUtil.getNext(node);
127
128     for (; index >= 0; index--) {
129       for (; axis != lastNode && axis != null; axis = XmlUtil.getNext(axis)) {
130         if (_parent.match(axis, env))
131           break;
132       }
133
134       if (index > 0)
135         axis = XmlUtil.getNext(axis);
136     }
137
138     if (axis == lastNode)
139       return 0;
140
141     Node JavaDoc next = XmlUtil.getNext(axis);
142     for (; next != lastNode; next = XmlUtil.getNext(next)) {
143       if (_parent.match(next, env)) {
144         env.setMorePositions(true);
145         break;
146       }
147     }
148
149     Node JavaDoc a1 = axis;
150     
151     if (! _self && axis != null)
152       axis = axis.getParentNode();
153
154     int count = 0;
155     for (; axis != null; axis = axis.getParentNode()) {
156       if (pattern.match(axis, env))
157     count++;
158
159       if (node == axis)
160         break;
161     }
162
163     return count;
164   }
165
166   /**
167    * Returns the last node in the selection order.
168    *
169    * @param node the current node
170    *
171    * @return the last node
172    */

173   private Node JavaDoc lastDescendantNode(Node JavaDoc node)
174   {
175     Node JavaDoc last = node;
176
177     for (;
178          last != null && last.getNextSibling() == null;
179          last = last.getParentNode()) {
180     }
181
182     return last != null ? last.getNextSibling() : null;
183   }
184
185   /**
186    * counts the number of matching ancestors from the axis context
187    */

188   public int count(Node JavaDoc node, Env env, AbstractPattern pattern)
189   {
190     throw new RuntimeException JavaDoc();
191   }
192
193   public String JavaDoc toString()
194   {
195     return getPrefix() + (_self ? "ancestor-or-self::" : "ancestor::");
196   }
197 }
198
Popular Tags