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.xpath.Env;
032: import com.caucho.xpath.ExprEnvironment;
033: import com.caucho.xpath.XPathException;
034:
035: import org.w3c.dom.Node;
036:
037: /**
038: * matches if we can find a following sibling matching the parent pattern.
039: */
040: public class FromPreviousSibling extends Axis {
041: public FromPreviousSibling(AbstractPattern parent) {
042: super (parent);
043:
044: if (parent == null)
045: throw new RuntimeException();
046: }
047:
048: /**
049: * matches if we can find a following sibling that matches the parent.
050: *
051: * @param node the current node
052: * @param env the variable environment
053: *
054: * @return true if the pattern matches
055: */
056: public boolean match(Node node, ExprEnvironment env)
057: throws XPathException {
058: if (node == null)
059: return false;
060:
061: for (node = node.getNextSibling(); node != null; node = node
062: .getNextSibling()) {
063: if (_parent.match(node, env))
064: return true;
065: }
066:
067: return false;
068: }
069:
070: /**
071: * preceding-sibling's iterator is in reverse document order.
072: */
073: public boolean isAscending() {
074: return false;
075: }
076:
077: /**
078: * Returns true if the pattern returns unique values.
079: */
080: public boolean isUnique() {
081: if (_parent == null)
082: return true;
083: else
084: return _parent.isSingleSelect();
085: }
086:
087: /**
088: * Returns the first node in the selection order.
089: *
090: * @param node the current node
091: *
092: * @return the first node
093: */
094: public Node firstNode(Node node, ExprEnvironment env) {
095: return node.getPreviousSibling();
096: }
097:
098: /**
099: * Returns the next node in the selection order.
100: *
101: * @param node the current node
102: * @param lastNode the last node
103: *
104: * @return the next node
105: */
106: public Node nextNode(Node node, Node lastNOde) {
107: return node.getPreviousSibling();
108: }
109:
110: /**
111: * The count of nodes between the test-node and the axis.
112: *
113: * @param node the current node
114: * @param env the variable environment
115: */
116: public int position(Node node, Env env, AbstractPattern pattern)
117: throws XPathException {
118: int index = env.getPositionIndex();
119:
120: int count = 1;
121: while ((node = node.getNextSibling()) != null) {
122: if (_parent.match(node, env)) {
123: if (--index <= 0) {
124: // Test if there are more possible roots.
125: for (node = node.getNextSibling(); node != null; node = node
126: .getNextSibling()) {
127: if (_parent.match(node, env)) {
128: env.setMorePositions(true);
129: break;
130: }
131: }
132: return count;
133: }
134: }
135:
136: if (pattern.match(node, env))
137: count++;
138: }
139:
140: return count;
141: }
142:
143: public String toString() {
144: return getPrefix() + "preceding-sibling::";
145: }
146: }
|