1 22 23 package org.xquark.xpath; 24 25 26 33 public class XNode implements Cloneable 34 { 35 private static final String RCSRevision = "$Revision: 1.1 $"; 36 private static final String RCSName = "$Name: $"; 37 38 41 private String namespace = null; 42 private String localName = null; 44 45 46 protected byte type = NodeKind.NODE; 47 48 51 public XNode() 52 {} 53 54 60 public XNode(String namespace, String localName) 61 { 62 this(namespace, localName, NodeKind.NODE); 63 } 64 65 69 public XNode(byte type) 70 { 71 this(null, null, type); 72 } 73 74 78 public XNode(String target) 79 { 80 this(target, null, NodeKind.PI); 81 } 82 83 89 public XNode(String namespace, String localName, byte type) 90 { 91 set(namespace, localName, type); 92 } 93 94 100 public void set(String namespace, String localName, byte type) 101 { 102 if ((namespace == null) || (namespace.length() == 0)) 103 this.namespace = null; 104 else 105 this.namespace = namespace; 106 this.localName = localName; 107 set(type); 108 } 109 110 114 public void set(byte type) 115 { 116 this.type = type; 117 } 118 119 124 public void set(String exp) 125 { 126 int i, j = 0; 127 128 i = 0; 130 if (exp.charAt(i) == '@') 131 { 132 i++; 133 type = NodeKind.ATTRIBUTE; 134 } 135 else 136 type = NodeKind.ELEMENT; 137 if (exp.charAt(i) == '{') 138 { 139 j = exp.indexOf('}'); 140 namespace = exp.substring(i + 1, j); 141 i = j + 1; 142 } 143 else 144 namespace = null; 145 146 localName = exp.substring(i); 147 } 148 149 153 public String getNamespace() 154 { 155 return namespace; 156 } 157 158 162 public final String getLocalName() 163 { 164 return localName; 165 } 166 167 174 public final String getExpandedName() 175 { 176 if ((namespace == null) || namespace.equals("")) 177 return localName; 178 else 179 return '{' + namespace + '}' + localName; 180 } 181 182 187 public byte getType() 188 { 189 return type; 190 } 191 192 197 public final boolean equals(Object o) 198 { 199 XNode node = null; 200 if (o instanceof XNode) 201 node = (XNode) o; 202 else 203 return false; 204 return ( 205 ((getNamespace() == null) && (node.getNamespace() == null)) 206 || ((getNamespace() != null) 207 && getNamespace().equals(node.getNamespace()))) 208 && (((getLocalName() == null) && (node.getLocalName() == null)) 209 || ((getLocalName() != null) 210 && getLocalName().equals(node.getLocalName()))) 211 && (type == node.getType()); 212 } 213 214 220 public int hashCode() 221 { 222 if (localName != null) 224 return localName.hashCode(); 225 else 226 return toString().hashCode(); 227 } 228 229 234 public String toString() 235 { 236 return getAbbreviatedSyntax(); 237 } 238 239 244 public final String getAbbreviatedSyntax() 245 { 246 StringBuffer sb = new StringBuffer (); 247 248 switch (type) 249 { 250 case NodeKind.NODE : 251 sb.append("node()"); 252 break; 253 254 case NodeKind.COMMENT : 255 sb.append("comment()"); 256 break; 257 258 case NodeKind.TEXT : 259 sb.append("text()"); 260 break; 261 262 case NodeKind.PI : 263 if (localName == null) 264 sb.append("processing-instruction()"); 265 else 266 sb.append("processing-instruction('" + localName + "')"); 267 break; 268 269 case NodeKind.ATTRIBUTE : 270 sb.append("@" + getExpandedName()); 271 break; 272 273 case NodeKind.ELEMENT : 274 sb.append(getExpandedName()); 275 break; 276 } 277 return sb.toString(); 278 } 279 280 286 boolean match(StepExpr step) 287 { 288 return step.match(this); 289 } 290 291 295 public Object clone() 296 { 297 try 298 { 299 return super.clone(); 300 } 301 catch (CloneNotSupportedException e) 302 { 303 return null; 305 } 306 } 307 308 } 309 | Popular Tags |