KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuffer;
32 import com.caucho.xpath.Env;
33 import com.caucho.xpath.ExprEnvironment;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 /**
38  * Matches the node context.
39  */

40 public class FromContext extends Axis {
41   // ancestor count from the specified context where this virtual context is
42
int _ancestorCount;
43   
44   public FromContext()
45   {
46     super(null);
47   }
48
49   /**
50    * Creates a new context pattern. Using the ancestorCount,
51    * FromContext collapses parent patterns into a single FromContext
52    * pattern. For example, context()/../.. is collapsed to FromContext(2).
53    *
54    * @param ancestorCount number of parents where the real context starts
55    */

56   public FromContext(int ancestorCount)
57   {
58     super(null);
59
60     _ancestorCount = ancestorCount;
61   }
62
63   public int getCount()
64   {
65     return _ancestorCount;
66   }
67
68   /**
69    * Matches the node context
70    *
71    * @param node the current node
72    * @param env the variable environment
73    *
74    * @return true if the node is the context node.
75    */

76   public boolean match(Node JavaDoc node, ExprEnvironment env)
77   {
78     Node JavaDoc context = env.getContextNode();
79
80     for (int i = 0; i < _ancestorCount && context != null; i++)
81       context = context.getParentNode();
82     
83     return (node == context);
84   }
85
86   /**
87    * The context is strictly ascending.
88    */

89   public boolean isStrictlyAscending()
90   {
91     return true;
92   }
93   
94   /**
95    * Returns true if the pattern selects a single node
96    */

97   boolean isSingleSelect()
98   {
99     return true;
100   }
101   
102   /**
103    * There is only a single node in the context axis.
104    *
105    * @param node the current node
106    * @param env the variable environment
107    * @param pattern the position pattern
108    *
109    * @return 1
110    */

111   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
112   {
113     return 1;
114   }
115   
116   /**
117    * There is only a single node in the context axis.
118    *
119    * @param node the current node
120    * @param env the variable environment
121    * @param pattern the position pattern
122    *
123    * @return 1
124    */

125   public int count(Node JavaDoc node, Env env, AbstractPattern pattern)
126   {
127     return 1;
128   }
129
130   /**
131    * Returns the first node in the selection order.
132    *
133    * @param node the current node
134    * @param env the current environment
135    *
136    * @return the first node
137    */

138   public Node JavaDoc firstNode(Node JavaDoc node, ExprEnvironment env)
139   {
140     for (int i = 0; node != null && i < _ancestorCount; i++)
141       node = node.getParentNode();
142
143     return node;
144   }
145
146   /**
147    * Returns the next node in the selection order.
148    *
149    * @param node the current node
150    * @param lastNode the last node
151    *
152    * @return the next node
153    */

154   public Node JavaDoc nextNode(Node JavaDoc node, Node JavaDoc lastNode)
155   {
156     return null;
157   }
158
159   /**
160    * Returns true if the two patterns are equal.
161    */

162   public boolean equals(Object JavaDoc b)
163   {
164     return (b instanceof FromContext &&
165             _ancestorCount == ((FromContext) b)._ancestorCount);
166   }
167
168   /**
169    * Prints the pattern representing the context.
170    */

171   public String JavaDoc toString()
172   {
173     if (_ancestorCount == 0)
174       return "context()";
175     
176     CharBuffer cb = CharBuffer.allocate();
177
178     cb.append("..");
179
180     for (int i = 1; i < _ancestorCount; i++) {
181       cb.append("/..");
182     }
183
184     return cb.close();
185   }
186 }
187
Popular Tags