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.xml.XmlUtil;
032: import com.caucho.xpath.Env;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035:
036: import org.w3c.dom.Node;
037:
038: /**
039: * Matches nodes following the current node in the current document.
040: */
041: public class FromNext extends Axis {
042: public FromNext(AbstractPattern parent) {
043: super (parent);
044:
045: if (parent == null)
046: throw new RuntimeException();
047: }
048:
049: /**
050: * Matches if there is a previous node matching the parent pattern.
051: *
052: * @param node the current node
053: * @param env the variable environment
054: *
055: * @return true if the pattern matches
056: */
057: public boolean match(Node node, ExprEnvironment env)
058: throws XPathException {
059: if (node == null)
060: return false;
061:
062: return getAxisContext(node, env) != null;
063: }
064:
065: /**
066: * Returns true if the pattern is strictly ascending.
067: */
068: public boolean isStrictlyAscending() {
069: if (_parent == null)
070: return true;
071: else
072: return _parent.isSingleSelect();
073: }
074:
075: /**
076: * Returns the first node in the selection order.
077: *
078: * @param node the current node
079: *
080: * @return the first node
081: */
082: public Node firstNode(Node node, ExprEnvironment env) {
083: for (; node != null; node = node.getParentNode())
084: if (node.getNextSibling() != null)
085: return node.getNextSibling();
086:
087: return null;
088: }
089:
090: /**
091: * Returns the next node in the selection order.
092: *
093: * @param node the current node
094: * @param lastNode the last node
095: *
096: * @return the next node
097: */
098: public Node nextNode(Node node, Node lastNode) {
099: return XmlUtil.getNext(node);
100: }
101:
102: /**
103: * Calculates position by counting previous nodes matching the pattern.
104: *
105: * The axis is a previous node matching the parent pattern.
106: */
107: public int position(Node node, Env env, AbstractPattern pattern)
108: throws XPathException {
109: int index = env.getPositionIndex();
110:
111: int count = 1;
112: for (Node ptr = XmlUtil.getPrevious(node); ptr != null; ptr = XmlUtil
113: .getPrevious(ptr)) {
114: if (_parent.match(ptr, env)) {
115: boolean isParent = false;
116:
117: for (Node n = node; n != null; n = n.getParentNode()) {
118: if (n == ptr) {
119: isParent = true;
120: break;
121: }
122: }
123: if (!isParent && --index < 0) {
124: for (; ptr != null; ptr = XmlUtil.getPrevious(ptr)) {
125: if (_parent.match(ptr, env)) {
126: env.setMorePositions(true);
127: break;
128: }
129: }
130: return count;
131: }
132: }
133: if (pattern.match(ptr, env))
134: count++;
135: }
136:
137: return count;
138: }
139:
140: /**
141: * An axis context is a previous node matching the parent pattern.
142: */
143: private Node getAxisContext(Node node, ExprEnvironment env)
144: throws XPathException {
145: if (node == null)
146: return null;
147:
148: for (Node prev = node; prev != null; prev = XmlUtil
149: .getPrevious(prev)) {
150: if (!isDescendant(node, prev) && _parent.match(prev, env))
151: return prev;
152: }
153:
154: return null;
155: }
156:
157: /**
158: * Returns true if descendant is a descendant of node.
159: */
160: private boolean isDescendant(Node descendant, Node node) {
161: for (; descendant != node && descendant != null; descendant = descendant
162: .getParentNode()) {
163: }
164:
165: return descendant != null;
166: }
167:
168: public String toString() {
169: return getPrefix() + "following::";
170: }
171: }
|