1 28 29 package com.caucho.xml2; 30 31 import com.caucho.util.CharBuffer; 32 33 import org.w3c.dom.*; 34 35 import java.io.IOException ; 36 import java.util.HashMap ; 37 import javax.xml.namespace.QName ; 38 39 42 public class QElement extends QAttributedNode implements CauchoElement { 43 private QName _name; 44 45 48 public QElement() 49 { 50 } 51 52 57 public QElement(String name) 58 { 59 _name = new QName (name); 60 } 61 62 67 public QElement(String name, String namespace) 68 { 69 _name = new QName (name, namespace); 70 } 71 72 77 public QElement(QName name) 78 { 79 _name = name; 80 } 81 82 protected QElement(QDocument owner, QName name) 83 { 84 _owner = owner; 85 _name = name; 86 } 87 88 94 QElement(QName name, HashMap attributes) 95 { 96 _name = name; 97 } 98 99 105 public void setName(QName name) 106 { 107 _name = name; 108 } 109 110 113 public QName getQName() 114 { 115 return _name; 116 } 117 118 121 public String getNodeName() 122 { 123 return _name.getLocalPart(); 124 } 125 126 129 public String getTagName() 130 { 131 return _name.getLocalPart(); 132 } 133 134 137 public String getLocalName() 138 { 139 return _name.getLocalPart(); 140 } 141 142 145 public String getPrefix() 146 { 147 return _name.getPrefix(); 148 } 149 150 153 public String getCanonicalName() 154 { 155 return _name.toString(); 156 } 157 158 161 public String getNamespaceURI() 162 { 163 return _name.getNamespaceURI(); 164 } 165 166 172 public String getNamespace(String prefix) 173 { 174 if (prefix == null) 175 return getNamespace("", "xmlns"); 176 else 177 return getNamespace(prefix, "xmlns:" + prefix); 178 } 179 180 private String getNamespace(String prefix, String xmlns) 181 { 182 Attr namespace = getAttributeNode(xmlns); 183 184 if (namespace != null) 185 return namespace.getNodeValue(); 186 187 if (_parent instanceof QElement) 188 return ((QElement) _parent).getNamespace(prefix, xmlns); 189 190 return _owner.getNamespace(prefix); 191 } 192 193 196 public short getNodeType() 197 { 198 return ELEMENT_NODE; 199 } 200 201 204 public TypeInfo getSchemaTypeInfo() 205 { 206 return null; 207 } 208 209 212 public NodeList getElementsByTagName(String tagName) 213 { 214 QAbstractNode child = (QAbstractNode) getFirstChild(); 215 216 if (child != null) 217 return new QDeepNodeList(this, child, new TagPredicate(tagName)); 218 else 219 return new QDeepNodeList(this, null, new TagPredicate(tagName)); 220 } 221 222 225 public NodeList getElementsByTagNameNS(String uri, String name) 226 { 227 QAbstractNode child = (QAbstractNode) getFirstChild(); 228 229 if (child != null) 230 return new QDeepNodeList(this, child, new NSTagPredicate(uri, name)); 231 else 232 return new QDeepNodeList(this, null, new NSTagPredicate(uri, name)); 233 } 234 235 241 public Node appendChild(Node child) 242 throws DOMException 243 { 244 Node result = super.appendChild(child); 245 246 if (child instanceof QElement) { 247 QElement elt = (QElement) child; 248 QName name = elt._name; 249 250 if (name.getNamespaceURI() != "") { 251 addNamespace(name); 252 } 253 254 for (QAttr attr = (QAttr) elt.getFirstAttribute(); 255 attr != null; 256 attr = (QAttr) attr.getNextSibling()) { 257 name = attr._name; 258 259 if (name.getNamespaceURI() != "") { 260 addNamespace(name); 261 } 262 } 263 } 264 265 return result; 266 } 267 268 271 void addNamespace(QName name) 272 { 273 _owner.addNamespace(name.getPrefix(), name.getNamespaceURI()); 274 } 275 276 279 public void normalize() 280 { 281 Node node = _firstChild; 282 283 while (node != null) { 284 if (node.getNodeType() == TEXT_NODE && 285 node.getNextSibling() != null && 286 node.getNextSibling().getNodeType() == TEXT_NODE) { 287 Text text = (Text) node; 288 Text next = (Text) node.getNextSibling(); 289 text.appendData(next.getData()); 290 removeChild(next); 291 } else if (node.getNodeType() == ELEMENT_NODE) { 292 Element elt = (Element) node; 293 elt.normalize(); 294 node = node.getNextSibling(); 295 } else 296 node = node.getNextSibling(); 297 } 298 } 299 300 public boolean hasContent() 301 { 302 return true; 303 } 304 305 public boolean equals(Object arg) 306 { 307 return this == arg; 308 } 309 310 public boolean equals(Node arg, boolean deep) 311 { 312 return this == arg; 313 } 314 315 319 public String getTextValue() 320 { 321 CharBuffer cb = CharBuffer.allocate(); 322 323 for (QAbstractNode node = _firstChild; node != null; node = node._next) { 324 cb.append(node.getTextValue()); 325 } 326 327 return cb.close(); 328 } 329 330 void print(XmlPrinter out) throws IOException  331 { 332 out.startElement(getNamespaceURI(), getLocalName(), getNodeName()); 333 for (QAbstractNode node = (QAbstractNode) getFirstAttribute(); 334 node != null; 335 node = (QAbstractNode) node.getNextSibling()) { 336 out.attribute(node.getNamespaceURI(), 337 node.getLocalName(), 338 node.getNodeName(), 339 node.getNodeValue()); 340 } 341 for (Node node = getFirstChild(); 342 node != null; 343 node = node.getNextSibling()) { 344 ((QAbstractNode) node).print(out); 345 } 346 out.endElement(getNamespaceURI(), getLocalName(), getNodeName()); 347 } 348 349 public String toString() 350 { 351 CharBuffer cb = CharBuffer.allocate(); 352 353 cb.append("Element[" + _name); 354 355 for (QAttr attr = (QAttr) getFirstAttribute(); 356 attr != null; 357 attr = (QAttr) attr.getNextSibling()) 358 cb.append(" " + attr); 359 cb.append("]"); 360 361 return cb.close(); 362 } 363 364 private Object writeReplace() 365 { 366 return new SerializedXml(this); 367 } 368 369 static class TagPredicate implements QNodePredicate { 370 String _name; 371 372 TagPredicate(String name) 373 { 374 if (name == null) 375 name = "*"; 376 377 _name = name; 378 } 379 380 public boolean isMatch(QAbstractNode node) 381 { 382 return (node.getNodeName().equals(_name) || 383 _name.equals("*") && node instanceof Element); 384 } 385 } 386 387 static class NSTagPredicate implements QNodePredicate { 388 String _uri; 389 String _local; 390 391 NSTagPredicate(String uri, String local) 392 { 393 _uri = uri; 394 _local = local; 395 } 396 397 public boolean isMatch(QAbstractNode node) 398 { 399 return (_local.equals(node.getLocalName()) && 400 _uri.equals(node.getNamespaceURI())); 401 } 402 } 403 } 404
| Popular Tags
|