KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Matches nodes following the current node in the current document.
40  */

41 public class FromNext extends Axis {
42   public FromNext(AbstractPattern parent)
43   {
44     super(parent);
45
46     if (parent == null)
47       throw new RuntimeException JavaDoc();
48   }
49
50   /**
51    * Matches if there is a previous node matching the parent pattern.
52    *
53    * @param node the current node
54    * @param env the variable environment
55    *
56    * @return true if the pattern matches
57    */

58   public boolean match(Node JavaDoc node, ExprEnvironment env)
59     throws XPathException
60   {
61     if (node == null)
62       return false;
63
64     return getAxisContext(node, env) != null;
65   }
66   
67   /**
68    * Returns true if the pattern is strictly ascending.
69    */

70   public boolean isStrictlyAscending()
71   {
72     if (_parent == null)
73       return true;
74     else
75       return _parent.isSingleSelect();
76   }
77
78   /**
79    * Returns the first node in the selection order.
80    *
81    * @param node the current node
82    *
83    * @return the first node
84    */

85   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
86   {
87     for (; node != null; node = node.getParentNode())
88       if (node.getNextSibling() != null)
89         return node.getNextSibling();
90     
91     return null;
92   }
93
94   /**
95    * Returns the next node in the selection order.
96    *
97    * @param node the current node
98    * @param lastNode the last node
99    *
100    * @return the next node
101    */

102   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc lastNode)
103   {
104     return XmlUtil.getNext(node);
105   }
106
107   /**
108    * Calculates position by counting previous nodes matching the pattern.
109    *
110    * The axis is a previous node matching the parent pattern.
111    */

112   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
113     throws XPathException
114   {
115     int index = env.getPositionIndex();
116
117     int count = 1;
118     for (Node JavaDoc ptr = XmlUtil.getPrevious(node);
119          ptr != null;
120          ptr = XmlUtil.getPrevious(ptr)) {
121       if (_parent.match(ptr, env)) {
122         boolean isParent = false;
123         
124         for (Node JavaDoc n = node; n != null; n = n.getParentNode()) {
125           if (n == ptr) {
126             isParent = true;
127             break;
128           }
129         }
130         if (! isParent && --index < 0) {
131           for (; ptr != null; ptr = XmlUtil.getPrevious(ptr)) {
132             if (_parent.match(ptr, env)) {
133               env.setMorePositions(true);
134               break;
135             }
136           }
137           return count;
138         }
139       }
140       if (pattern.match(ptr, env))
141         count++;
142     }
143
144     return count;
145   }
146
147   /**
148    * An axis context is a previous node matching the parent pattern.
149    */

150   private Node JavaDoc getAxisContext(Node JavaDoc node, ExprEnvironment env)
151     throws XPathException
152   {
153     if (node == null)
154       return null;
155
156     for (Node JavaDoc prev = node;
157      prev != null;
158      prev = XmlUtil.getPrevious(prev)) {
159       if (! isDescendant(node, prev) && _parent.match(prev, env))
160     return prev;
161     }
162     
163     return null;
164   }
165
166   /**
167    * Returns true if descendant is a descendant of node.
168    */

169   private boolean isDescendant(Node JavaDoc descendant, Node JavaDoc node)
170   {
171     for (;
172      descendant != node && descendant != null;
173      descendant = descendant.getParentNode()) {
174     }
175
176     return descendant != null;
177   }
178
179   public String JavaDoc toString()
180   {
181     return getPrefix() + "following::";
182   }
183 }
184
Popular Tags