001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.xpath.Env;
033: import com.caucho.xpath.ExprEnvironment;
034:
035: import org.w3c.dom.Node;
036:
037: /**
038: * Matches the node context.
039: */
040: public class FromContext extends Axis {
041: // ancestor count from the specified context where this virtual context is
042: int _ancestorCount;
043:
044: public FromContext() {
045: super (null);
046: }
047:
048: /**
049: * Creates a new context pattern. Using the ancestorCount,
050: * FromContext collapses parent patterns into a single FromContext
051: * pattern. For example, context()/../.. is collapsed to FromContext(2).
052: *
053: * @param ancestorCount number of parents where the real context starts
054: */
055: public FromContext(int ancestorCount) {
056: super (null);
057:
058: _ancestorCount = ancestorCount;
059: }
060:
061: public int getCount() {
062: return _ancestorCount;
063: }
064:
065: /**
066: * Matches the node context
067: *
068: * @param node the current node
069: * @param env the variable environment
070: *
071: * @return true if the node is the context node.
072: */
073: public boolean match(Node node, ExprEnvironment env) {
074: Node context = env.getContextNode();
075:
076: for (int i = 0; i < _ancestorCount && context != null; i++)
077: context = context.getParentNode();
078:
079: return (node == context);
080: }
081:
082: /**
083: * The context is strictly ascending.
084: */
085: public boolean isStrictlyAscending() {
086: return true;
087: }
088:
089: /**
090: * Returns true if the pattern selects a single node
091: */
092: boolean isSingleSelect() {
093: return true;
094: }
095:
096: /**
097: * There is only a single node in the context axis.
098: *
099: * @param node the current node
100: * @param env the variable environment
101: * @param pattern the position pattern
102: *
103: * @return 1
104: */
105: public int position(Node node, Env env, AbstractPattern pattern) {
106: return 1;
107: }
108:
109: /**
110: * There is only a single node in the context axis.
111: *
112: * @param node the current node
113: * @param env the variable environment
114: * @param pattern the position pattern
115: *
116: * @return 1
117: */
118: public int count(Node node, Env env, AbstractPattern pattern) {
119: return 1;
120: }
121:
122: /**
123: * Returns the first node in the selection order.
124: *
125: * @param node the current node
126: * @param env the current environment
127: *
128: * @return the first node
129: */
130: public Node firstNode(Node node, ExprEnvironment env) {
131: for (int i = 0; node != null && i < _ancestorCount; i++)
132: node = node.getParentNode();
133:
134: return node;
135: }
136:
137: /**
138: * Returns the next node in the selection order.
139: *
140: * @param node the current node
141: * @param lastNode the last node
142: *
143: * @return the next node
144: */
145: public Node nextNode(Node node, Node lastNode) {
146: return null;
147: }
148:
149: /**
150: * Returns true if the two patterns are equal.
151: */
152: public boolean equals(Object b) {
153: return (b instanceof FromContext && _ancestorCount == ((FromContext) b)._ancestorCount);
154: }
155:
156: /**
157: * Prints the pattern representing the context.
158: */
159: public String toString() {
160: if (_ancestorCount == 0)
161: return "context()";
162:
163: CharBuffer cb = CharBuffer.allocate();
164:
165: cb.append("..");
166:
167: for (int i = 1; i < _ancestorCount; i++) {
168: cb.append("/..");
169: }
170:
171: return cb.close();
172: }
173: }
|