KickJava   Java API By Example, From Geeks To Geeks.

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


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 child nodes.
39  */

40 public class FromChildren extends Axis {
41   public FromChildren(AbstractPattern parent)
42   {
43     super(parent);
44   }
45
46   /**
47    * Matches all nodes except attributes and tests the parent pattern
48    * with the parent node.
49    *
50    * @param node the current node
51    * @param env the variable environment
52    *
53    * @return true if the pattern matches
54    */

55   public boolean match(Node JavaDoc node, ExprEnvironment env)
56     throws XPathException
57   {
58     if (node == null)
59       return false;
60
61     return _parent == null || _parent.match(node.getParentNode(), env);
62   }
63   
64   /**
65    * The position of the child is the count of previous siblings
66    * matching the pattern.
67    *
68    * @param node the current node
69    * @param env the variable environment
70    * @param pattern the position pattern
71    *
72    * @return the node's position.
73    */

74   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
75     throws XPathException
76   {
77     int count = 1;
78     
79     for (node = node.getPreviousSibling();
80      node != null;
81      node = node.getPreviousSibling()) {
82       if (pattern == null || pattern.match(node, env))
83     count++;
84     }
85
86     return count;
87   }
88   
89   /**
90    * Counts all siblings matching the pattern.
91    *
92    * @param node the current node
93    * @param env the variable environment
94    * @param pattern the position pattern
95    *
96    * @return the count of the node list.
97    */

98   public int count(Node JavaDoc node, Env env, AbstractPattern pattern)
99     throws XPathException
100   {
101     int count = 0;
102
103     for (node = node.getParentNode().getFirstChild();
104      node != null;
105      node = node.getNextSibling()) {
106       if (pattern.match(node, env))
107     count++;
108     }
109
110     return count;
111   }
112
113   /**
114    * Returns the first node in the selection order.
115    *
116    * @param node the current node
117    *
118    * @return the first node
119    */

120   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
121   {
122     return node.getFirstChild();
123   }
124
125   /**
126    * Returns the next node in the selection order.
127    *
128    * @param node the current node
129    * @param node the last node
130    *
131    * @return the next node
132    */

133   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc lastNode)
134   {
135     return node.getNextSibling();
136   }
137   
138   /**
139    * Returns true if the pattern nodes on a single level.
140    */

141   boolean isSingleLevel()
142   {
143     return _parent == null || _parent.isSingleLevel();
144   }
145
146   /**
147    * Returns true if the pattern is strictly ascending.
148    */

149   public boolean isStrictlyAscending()
150   {
151     if (_parent == null)
152       return true;
153     else {
154       return _parent.isStrictlyAscending();
155     }
156   }
157
158   /**
159    * Returns true if the two patterns are equal.
160    */

161   public boolean equals(Object JavaDoc b)
162   {
163     if (! (b instanceof FromChildren))
164       return false;
165
166     FromChildren bPattern = (FromChildren) b;
167     
168     return (_parent == bPattern._parent ||
169             (_parent != null && _parent.equals(bPattern._parent)));
170   }
171
172   public String JavaDoc toString()
173   {
174     return getPrefix() + "child::";
175   }
176 }
177
Popular Tags