KickJava   Java API By Example, From Geeks To Geeks.

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


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  * matches following siblings
39  */

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

57   public boolean match(Node JavaDoc node, ExprEnvironment env)
58     throws XPathException
59   {
60     if (node == null)
61       return false;
62
63     for (node = node.getPreviousSibling();
64      node != null;
65      node = node.getPreviousSibling()) {
66       if (_parent.match(node, env))
67     return true;
68     }
69
70     return false;
71   }
72
73   /**
74    * The count of nodes between the test-node and the axis.
75    *
76    * @param node the current node
77    * @param env the variable environment
78    */

79   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
80     throws XPathException
81   {
82     int index = env.getPositionIndex();
83
84     int count = 1;
85     while ((node = node.getPreviousSibling()) != null) {
86       if (_parent.match(node, env)) {
87         if (--index <= 0) {
88           // Test if there are more possible roots.
89
for (node = node.getPreviousSibling();
90                node != null;
91                node = node.getPreviousSibling()) {
92             if (_parent.match(node, env)) {
93               env.setMorePositions(true);
94               break;
95             }
96           }
97           return count;
98         }
99       }
100
101       if (pattern.match(node, env))
102     count++;
103     }
104
105     return count;
106   }
107
108   /**
109    * Returns true if the pattern is strictly ascending.
110    */

111   public boolean isStrictlyAscending()
112   {
113     if (_parent == null)
114       return true;
115     else
116       return _parent.isSingleSelect();
117   }
118
119   /**
120    * Returns the first node in the selection order.
121    *
122    * @param node the current node
123    *
124    * @return the first node
125    */

126   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
127   {
128     return node.getNextSibling();
129   }
130
131   /**
132    * Returns the next node in the selection order.
133    *
134    * @param node the current node
135    * @param lastNode the last node
136    *
137    * @return the next node
138    */

139   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc lastNode)
140   {
141     return node.getNextSibling();
142   }
143
144   public String JavaDoc toString()
145   {
146     return getPrefix() + "following-sibling::";
147   }
148 }
149
Popular Tags