1 56 57 package org.jdom.contrib.helpers; 58 59 60 import java.util.List ; 61 import java.util.Iterator ; 62 63 import org.jdom.*; 64 import org.jdom.filter.ContentFilter; 65 66 67 78 public class XPathHelper { 79 80 92 public static String getPathString(Element to) throws JDOMException { 93 return getPathString(null, to); 94 } 95 96 113 public static String getPathString(Object from, Element to) 114 throws JDOMException { 115 checkPathStringArguments(from, to); 116 117 if (to == from) { 118 return "node()"; 119 } 120 else { 121 return getElementPath(from, to, true).toString(); 122 } 123 } 124 125 137 public static String getPathString(Attribute to) throws JDOMException { 138 return getPathString(null, to); 139 } 140 141 158 public static String getPathString(Object from, Attribute to) 159 throws JDOMException { 160 checkPathStringArguments(from, to); 161 162 if (to == from) { 163 return "node()"; 164 } 165 else { 166 StringBuffer path = getElementPath(from, to.getParent(), false); 167 path.append('@').append(to.getName()).toString(); 168 return path.toString(); 169 } 170 } 171 172 184 public static String getPathString(Text to) throws JDOMException { 185 return getPathString(null, to); 186 } 187 188 205 public static String getPathString(Object from, Text to) 206 throws JDOMException { 207 checkPathStringArguments(from, to); 208 209 if (to == from) { 210 return "node()"; 211 } 212 else { 213 Element parent = to.getParentElement(); 214 List siblings = null; 215 int nodeType = ContentFilter.TEXT; 216 StringBuffer path = getElementPath(from, parent, false); 217 218 if (parent != null) { 219 siblings = parent.getContent(new ContentFilter(nodeType)); 220 } 221 else { 222 Document doc = to.getDocument(); 223 if (doc != null) { 224 siblings = doc.getContent(new ContentFilter(nodeType)); 225 } 226 } 227 return getPositionPath(to, siblings, "text()", path).toString(); 228 } 229 } 230 231 243 public static String getPathString(Comment to) throws JDOMException { 244 return getPathString(null, to); 245 } 246 247 264 public static String getPathString(Object from, Comment to) 265 throws JDOMException { 266 checkPathStringArguments(from, to); 267 268 if (to == from) { 269 return "node()"; 270 } 271 else { 272 Element parent = to.getParentElement(); 273 List siblings = null; 274 int nodeType = ContentFilter.COMMENT; 275 StringBuffer path = getElementPath(from, parent, false); 276 277 if (parent != null) { 278 siblings = parent.getContent(new ContentFilter(nodeType)); 279 } 280 else { 281 Document doc = to.getDocument(); 282 if (doc != null) { 283 siblings = doc.getContent(new ContentFilter(nodeType)); 284 } 285 } 286 return getPositionPath(to, siblings, "comment()", path).toString(); 287 } 288 } 289 290 303 public static String getPathString(ProcessingInstruction to) 304 throws JDOMException { 305 return getPathString(null, to); 306 } 307 308 326 public static String getPathString(Object from, ProcessingInstruction to) 327 throws JDOMException { 328 checkPathStringArguments(from, to); 329 330 if (to == from) { 331 return "node()"; 332 } 333 else { 334 Element parent = to.getParentElement(); 335 List siblings = null; 336 int nodeType = ContentFilter.PI; 337 StringBuffer path = getElementPath(from, parent, false); 338 339 if (parent != null) { 340 siblings = parent.getContent(new ContentFilter(nodeType)); 341 } 342 else { 343 Document doc = to.getDocument(); 344 if (doc != null) { 345 siblings = doc.getContent(new ContentFilter(nodeType)); 346 } 347 } 348 return getPositionPath(to, siblings, 349 "processing-instruction()", path).toString(); 350 } 351 } 352 353 367 public static String getPathString(Object to) throws JDOMException { 368 return getPathString(null, to); 369 } 370 371 390 public static String getPathString(Object from, Object to) 391 throws JDOMException { 392 if (to instanceof Element) { 393 return getPathString(from, (Element) to); 394 } 395 else if (to instanceof Attribute) { 396 return getPathString(from, (Attribute) to); 397 } 398 else if (to instanceof Text) { 399 return getPathString(from, (Text) to); 400 } 401 else if (to instanceof Comment) { 402 return getPathString(from, (Comment) to); 403 } 404 else if (to instanceof ProcessingInstruction) { 405 return getPathString(from, (ProcessingInstruction) to); 406 } 407 else { 408 throw new IllegalArgumentException ( 409 "\"to \" shall be an Element, Attribute," + 410 " Text, Comment or ProcessingInstruction node"); 411 } 412 } 413 414 415 425 private static void checkPathStringArguments(Object from, Object to) { 426 if (!((from == null) || (from instanceof Element) 427 || (from instanceof Document))) { 428 throw new IllegalArgumentException ("from"); 429 } 430 if (to == null) { 431 throw new IllegalArgumentException ("to"); 432 } 433 } 434 435 449 private static StringBuffer getElementPath(Object from, 450 Element to, boolean leaf) 451 throws JDOMException { 452 if (from instanceof Document) { 453 from = null; 454 } 455 return getElementPath((Element) from, to, leaf, new StringBuffer ()); 456 } 457 458 473 private static StringBuffer getElementPath(Element from, Element to, 474 boolean leaf, StringBuffer path) 475 throws JDOMException { 476 if (to != from) { 477 boolean isRoot = false; 478 List siblings = null; 479 480 Element parent = to.getParentElement(); 481 if (parent == null) { 482 if (parent != from) { 484 throw new JDOMException( 486 "The \"from\" node is not an ancestor of the \"to\" node"); 487 } 488 if (to.isRootElement()) { 489 isRoot = true; 490 path.append('/'); 491 } 492 } 493 else { 494 siblings = parent.getChildren(to.getName(), null); 495 } 496 497 if (parent != from) { 498 path = getElementPath(from, parent, false, path); 499 } 500 501 Namespace ns = to.getNamespace(); 502 if (ns == Namespace.NO_NAMESPACE) { 503 path = getPositionPath(to, siblings, to.getName(), path); 505 } 506 else { 507 String prefix = to.getNamespacePrefix(); 509 if ("".equals(prefix)) { 510 path.append("*[local-name()='"). 513 append(to.getName()).append("']"); 514 515 path = getPositionPath(to, siblings, null, path); 516 } 517 else { 518 path.append(to.getNamespacePrefix()).append(':'); 520 521 path = getPositionPath(to, siblings, to.getName(), path); 522 } 523 } 524 525 if ((!leaf) && (path.length() != 0)) { 526 path.append('/'); 527 } 528 } 529 return (path); 530 } 531 532 547 private static StringBuffer getPositionPath(Object node, List siblings, 548 String pathToken, 549 StringBuffer buffer) { 550 if (buffer == null) { 551 buffer = new StringBuffer (); 552 } 553 if (pathToken != null) { 554 buffer.append(pathToken); 555 } 556 557 if ((siblings != null) && (siblings.size() != 1)) { 558 int position = 0; 559 for (Iterator i = siblings.iterator(); i.hasNext();) { 560 position++; 561 if (i.next() == node) break; 562 } 563 buffer.append('[').append(position).append(']'); 564 } 565 return buffer; 566 } 567 } 568 569 | Popular Tags |