1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xml.XmlUtil; 32 import com.caucho.xpath.Env; 33 import com.caucho.xpath.ExprEnvironment; 34 import com.caucho.xpath.XPathException; 35 36 import org.w3c.dom.Node ; 37 38 41 public class FromNext extends Axis { 42 public FromNext(AbstractPattern parent) 43 { 44 super(parent); 45 46 if (parent == null) 47 throw new RuntimeException (); 48 } 49 50 58 public boolean match(Node node, ExprEnvironment env) 59 throws XPathException 60 { 61 if (node == null) 62 return false; 63 64 return getAxisContext(node, env) != null; 65 } 66 67 70 public boolean isStrictlyAscending() 71 { 72 if (_parent == null) 73 return true; 74 else 75 return _parent.isSingleSelect(); 76 } 77 78 85 public Node firstNode(Node node, ExprEnvironment env) 86 { 87 for (; node != null; node = node.getParentNode()) 88 if (node.getNextSibling() != null) 89 return node.getNextSibling(); 90 91 return null; 92 } 93 94 102 public Node nextNode(Node node, Node lastNode) 103 { 104 return XmlUtil.getNext(node); 105 } 106 107 112 public int position(Node node, Env env, AbstractPattern pattern) 113 throws XPathException 114 { 115 int index = env.getPositionIndex(); 116 117 int count = 1; 118 for (Node ptr = XmlUtil.getPrevious(node); 119 ptr != null; 120 ptr = XmlUtil.getPrevious(ptr)) { 121 if (_parent.match(ptr, env)) { 122 boolean isParent = false; 123 124 for (Node n = node; n != null; n = n.getParentNode()) { 125 if (n == ptr) { 126 isParent = true; 127 break; 128 } 129 } 130 if (! isParent && --index < 0) { 131 for (; ptr != null; ptr = XmlUtil.getPrevious(ptr)) { 132 if (_parent.match(ptr, env)) { 133 env.setMorePositions(true); 134 break; 135 } 136 } 137 return count; 138 } 139 } 140 if (pattern.match(ptr, env)) 141 count++; 142 } 143 144 return count; 145 } 146 147 150 private Node getAxisContext(Node node, ExprEnvironment env) 151 throws XPathException 152 { 153 if (node == null) 154 return null; 155 156 for (Node prev = node; 157 prev != null; 158 prev = XmlUtil.getPrevious(prev)) { 159 if (! isDescendant(node, prev) && _parent.match(prev, env)) 160 return prev; 161 } 162 163 return null; 164 } 165 166 169 private boolean isDescendant(Node descendant, Node node) 170 { 171 for (; 172 descendant != node && descendant != null; 173 descendant = descendant.getParentNode()) { 174 } 175 176 return descendant != null; 177 } 178 179 public String toString() 180 { 181 return getPrefix() + "following::"; 182 } 183 } 184 | Popular Tags |