KickJava   Java API By Example, From Geeks To Geeks.

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


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 if we can find a following sibling matching the parent pattern.
39  */

40 public class FromPreviousSibling extends Axis {
41   public FromPreviousSibling(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 following 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.getNextSibling();
64      node != null;
65      node = node.getNextSibling()) {
66       if (_parent.match(node, env))
67     return true;
68     }
69     
70     return false;
71   }
72
73   /**
74    * preceding-sibling's iterator is in reverse document order.
75    */

76   public boolean isAscending()
77   {
78     return false;
79   }
80
81   /**
82    * Returns true if the pattern returns unique values.
83    */

84   public boolean isUnique()
85   {
86     if (_parent == null)
87       return true;
88     else
89       return _parent.isSingleSelect();
90   }
91
92   /**
93    * Returns the first node in the selection order.
94    *
95    * @param node the current node
96    *
97    * @return the first node
98    */

99   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
100   {
101     return node.getPreviousSibling();
102   }
103
104   /**
105    * Returns the next node in the selection order.
106    *
107    * @param node the current node
108    * @param lastNode the last node
109    *
110    * @return the next node
111    */

112   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc lastNOde)
113   {
114     return node.getPreviousSibling();
115   }
116
117   /**
118    * The count of nodes between the test-node and the axis.
119    *
120    * @param node the current node
121    * @param env the variable environment
122    */

123   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
124     throws XPathException
125   {
126     int index = env.getPositionIndex();
127
128     int count = 1;
129     while ((node = node.getNextSibling()) != null) {
130       if (_parent.match(node, env)) {
131         if (--index <= 0) {
132           // Test if there are more possible roots.
133
for (node = node.getNextSibling();
134                node != null;
135                node = node.getNextSibling()) {
136             if (_parent.match(node, env)) {
137               env.setMorePositions(true);
138               break;
139             }
140           }
141           return count;
142         }
143       }
144
145       if (pattern.match(node, env))
146     count++;
147     }
148
149     return count;
150   }
151
152   public String JavaDoc toString()
153   {
154     return getPrefix() + "preceding-sibling::";
155   }
156 }
157
158
Popular Tags