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