1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xpath.Env; 32 import com.caucho.xpath.ExprEnvironment; 33 import com.caucho.xpath.XPathException; 34 35 import org.w3c.dom.Node ; 36 37 40 public class FromPreviousSibling extends Axis { 41 public FromPreviousSibling(AbstractPattern parent) 42 { 43 super(parent); 44 45 if (parent == null) 46 throw new RuntimeException (); 47 } 48 49 57 public boolean match(Node node, ExprEnvironment env) 58 throws XPathException 59 { 60 if (node == null) 61 return false; 62 63 for (node = node.getNextSibling(); 64 node != null; 65 node = node.getNextSibling()) { 66 if (_parent.match(node, env)) 67 return true; 68 } 69 70 return false; 71 } 72 73 76 public boolean isAscending() 77 { 78 return false; 79 } 80 81 84 public boolean isUnique() 85 { 86 if (_parent == null) 87 return true; 88 else 89 return _parent.isSingleSelect(); 90 } 91 92 99 public Node firstNode(Node node, ExprEnvironment env) 100 { 101 return node.getPreviousSibling(); 102 } 103 104 112 public Node nextNode(Node node, Node lastNOde) 113 { 114 return node.getPreviousSibling(); 115 } 116 117 123 public int position(Node node, Env env, AbstractPattern pattern) 124 throws XPathException 125 { 126 int index = env.getPositionIndex(); 127 128 int count = 1; 129 while ((node = node.getNextSibling()) != null) { 130 if (_parent.match(node, env)) { 131 if (--index <= 0) { 132 for (node = node.getNextSibling(); 134 node != null; 135 node = node.getNextSibling()) { 136 if (_parent.match(node, env)) { 137 env.setMorePositions(true); 138 break; 139 } 140 } 141 return count; 142 } 143 } 144 145 if (pattern.match(node, env)) 146 count++; 147 } 148 149 return count; 150 } 151 152 public String toString() 153 { 154 return getPrefix() + "preceding-sibling::"; 155 } 156 } 157 158 | Popular Tags |