1 28 29 package com.caucho.xpath.expr; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.xml.QDocumentType; 33 import com.caucho.xml.XmlChar; 34 import com.caucho.xml.XmlUtil; 35 import com.caucho.xpath.Expr; 36 import com.caucho.xpath.ExprEnvironment; 37 import com.caucho.xpath.XPathException; 38 import com.caucho.xpath.pattern.NodeIterator; 39 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 48 public class IdExpr extends Expr { 49 private Expr _expr; 50 51 private ExprEnvironment _lastEnv; 52 private int _lastUseCount; 53 private Node _lastContext; 54 private ArrayList _lastList; 55 56 public IdExpr(ArrayList <Expr> args) 57 { 58 if (args.size() > 0) 59 _expr = args.get(0); 60 } 61 62 public boolean isNodeSet() 63 { 64 return true; 65 } 66 67 75 public double evalNumber(Node node, ExprEnvironment env) 76 throws XPathException 77 { 78 String string = evalString(node, env); 79 80 return stringToNumber(string); 81 } 82 83 91 public boolean evalBoolean(Node node, ExprEnvironment env) 92 throws XPathException 93 { 94 return id(node, env).size() > 0; 95 } 96 97 106 public String evalString(Node node, ExprEnvironment env) 107 throws XPathException 108 { 109 NodeIterator iter = evalNodeSet(node, env); 110 111 if (! iter.hasNext()) 112 return ""; 113 114 Node qNode = (Node ) iter.next(); 115 return XmlUtil.textValue(qNode); 116 } 117 118 126 public Object evalObject(Node node, ExprEnvironment env) 127 throws XPathException 128 { 129 ArrayList <Element > list = id(node, env); 130 131 return list; 132 } 133 134 137 private ArrayList <Element > id(Node context, ExprEnvironment env) 138 throws XPathException 139 { 140 ArrayList idList = getIdList(context, env); 141 ArrayList <Element > list = new ArrayList <Element >(); 142 143 if (idList == null || idList.size() == 0) 144 return list; 145 146 Node ptr; 147 148 if (context instanceof Document ) 149 ptr = context; 150 else 151 ptr = context.getOwnerDocument(); 152 153 while ((ptr = XmlUtil.getNext(ptr)) != null) { 154 if (ptr instanceof Element ) { 155 Element elt = (Element ) ptr; 156 157 QDocumentType dtd; 158 dtd = (QDocumentType) elt.getOwnerDocument().getDoctype(); 159 String id = null; 160 if (dtd != null) 161 id = (String ) dtd.getElementId(elt.getNodeName()); 162 163 if (id != null) { 164 String idValue = elt.getAttribute(id); 165 if (idList.contains(idValue) && ! list.contains(elt)) 166 list.add(elt); 167 } 168 } 169 } 170 171 return list; 172 } 173 174 182 private ArrayList <String > getIdList(Node node, ExprEnvironment env) 183 throws XPathException 184 { 185 ArrayList <String > idList = new ArrayList <String >(); 186 187 Object obj = _expr.evalObject(node, env); 188 if (obj instanceof NodeList ) { 189 NodeList list = (NodeList ) obj; 190 191 int length = list.getLength(); 192 for (int i = 0; i < length; i++) { 193 Node value = list.item(i); 194 195 addText(idList, XmlUtil.textValue(value)); 196 } 197 } 198 else if (obj instanceof ArrayList ) { 199 ArrayList list = (ArrayList ) obj; 200 201 for (int i = 0; i < list.size(); i++) { 202 Node value = (Node ) list.get(i); 203 204 addText(idList, XmlUtil.textValue(value)); 205 } 206 } 207 else if (obj instanceof Iterator) { 208 Iterator iter = (Iterator) obj; 209 210 while (iter.hasNext()) { 211 Node value = (Node ) iter.next(); 212 213 addText(idList, XmlUtil.textValue(value)); 214 } 215 } 216 else 217 addText(idList, toString(obj)); 218 219 return idList; 220 } 221 222 private void addText(ArrayList <String > idList, String text) 223 { 224 int len = text.length(); 225 CharBuffer cb = new CharBuffer(); 226 int i = 0; 227 int ch = 0; 228 for (; i < len && XmlChar.isWhitespace(text.charAt(i)); i++) { 229 } 230 231 if (i == len) 232 return; 233 234 while (i < len) { 235 cb.clear(); 236 for (; i < len && ! XmlChar.isWhitespace(text.charAt(i)); i++) 237 cb.append(text.charAt(i)); 238 239 idList.add(cb.toString()); 240 241 for (; i < len && XmlChar.isWhitespace(text.charAt(i)); i++) { 242 } 243 } 244 } 245 246 public String toString() 247 { 248 if (_expr != null) 249 return "id(" + _expr + ")"; 250 else 251 return "id()"; 252 } 253 } 254 | Popular Tags |