1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.util.StringTokenizer ; 23 24 import org.w3c.dom.Node ; 25 26 public class XPath { 27 String xpath = null; 28 String [] parts = null; 29 30 33 public XPath(String xpath) { 34 this.xpath = xpath; 35 36 StringTokenizer st = new StringTokenizer (xpath, "/"); 37 int length = st.countTokens(); 38 parts = new String [length]; 39 40 for (int i = 0; i < length; i++) { 41 parts[i] = st.nextToken(); 42 } 43 } 44 45 48 public XPath getParent() { 49 String parentXPath = ""; 50 51 for (int i = 0; i < (parts.length - 1); i++) { 52 parentXPath = parentXPath + "/" + parts[i]; 53 } 54 55 return new XPath(parentXPath); 56 } 57 58 61 public short getType() { 62 if (parts[parts.length - 1].indexOf("@") == 0) { 63 return Node.ATTRIBUTE_NODE; 64 } 65 66 return Node.ELEMENT_NODE; 67 } 68 69 72 public String toString() { 73 return xpath; 74 } 75 76 79 public String getName() { 80 if (getType() == Node.ATTRIBUTE_NODE) { 81 return parts[parts.length - 1].substring(1); 82 } 83 84 return parts[parts.length - 1]; 85 } 86 87 92 public String getElementName() { 93 if (getType() == Node.ATTRIBUTE_NODE) { 94 return parts[parts.length - 2]; 95 } 96 97 return parts[parts.length - 1]; 98 } 99 100 103 public String getNameWithoutPredicates() { 104 return removePredicates(getName()); 105 } 106 107 110 public String removePredicates(String s) { 111 int index = s.indexOf("["); 112 113 if (index >= 0) { 114 return s.substring(0, index); 115 } 116 117 return s; 118 } 119 } 120 | Popular Tags |