1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.NamePool; 3 import com.icl.saxon.om.NodeInfo; 4 import com.icl.saxon.om.NamespaceException; 5 import com.icl.saxon.om.Namespace; 6 import com.icl.saxon.om.DocumentInfo; 7 import com.icl.saxon.output.Outputter; 9 10 import java.util.Vector ; 11 import org.w3c.dom.Node ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Attr ; 14 import org.w3c.dom.NamedNodeMap ; 15 import org.w3c.dom.DOMException ; 16 import javax.xml.transform.TransformerException ; 17 18 24 25 29 public class ElementWithAttributes extends ElementImpl 30 implements Element , NamedNodeMap { 31 32 protected AttributeCollection attributeList; protected int[] namespaceList = null; 37 38 44 45 public void initialise(int nameCode, AttributeCollection atts, NodeInfo parent, 46 String baseURI, int lineNumber, int sequenceNumber) { 47 this.nameCode = nameCode; 48 this.attributeList = atts; 49 this.parent = (ParentNodeImpl)parent; 50 this.sequence = sequenceNumber; 51 this.root = (DocumentImpl)parent.getDocumentRoot(); 52 root.setLineNumber(sequenceNumber, lineNumber); 53 root.setSystemId(sequenceNumber, baseURI); 54 } 55 56 59 60 public void setNamespaceDeclarations(int[] namespaces, int namespacesUsed) { 61 namespaceList = new int[namespacesUsed]; 62 System.arraycopy(namespaces, 0, namespaceList, 0, namespacesUsed); 63 } 64 65 66 73 74 public short getURICodeForPrefix(String prefix) throws NamespaceException { 75 if (prefix.equals("xml")) return Namespace.XML_CODE; 76 77 NamePool pool = getNamePool(); 78 int prefixCode = pool.getCodeForPrefix(prefix); 79 if (prefixCode==-1) { 80 throw new NamespaceException(prefix); 81 } 82 return getURICodeForPrefixCode(prefixCode); 83 } 84 85 private short getURICodeForPrefixCode(int prefixCode) throws NamespaceException { 86 if (namespaceList!=null) { 87 for (int i=0; i<namespaceList.length; i++) { 88 if ((namespaceList[i]>>16) == prefixCode) { 89 return (short)(namespaceList[i] & 0xffff); 90 } 91 } 92 } 93 NodeImpl next = parent; 94 while (true) { 95 if (next.getNodeType()==NodeInfo.ROOT) { 96 if (prefixCode==0) return Namespace.NULL_CODE; 98 throw new NamespaceException(getNamePool().getPrefixFromNamespaceCode(prefixCode<<16)); 99 } else if (next instanceof ElementWithAttributes) { 100 return ((ElementWithAttributes)next).getURICodeForPrefixCode(prefixCode); 101 } else { 102 next = (NodeImpl)next.getParentNode(); 103 } 104 } 105 } 106 107 114 115 public String getPrefixForURI(String uri) { 116 if (uri.equals(Namespace.XML)) return "xml"; 117 118 NamePool pool = getNamePool(); 119 int uriCode = pool.getCodeForURI(uri); 120 if (uriCode<0) return null; 121 return getPrefixForURICode(uriCode); 122 } 123 124 private String getPrefixForURICode(int code) { 125 if (namespaceList!=null) { 126 for (int i=0; i<namespaceList.length; i++) { 127 if ((namespaceList[i] & 0xffff) == code) { 128 return getNamePool().getPrefixFromNamespaceCode(namespaceList[i]); 129 } 130 } 131 } 132 NodeImpl next = parent; 133 while (true) { 134 if (next instanceof DocumentInfo) { 135 return null; 136 } else if (next instanceof ElementWithAttributes) { 137 return ((ElementWithAttributes)next).getPrefixForURICode(code); 138 } else { 139 next = (NodeImpl)next.getParentNode(); 140 } 141 } 142 } 143 144 155 156 public void addNamespaceNodes(ElementImpl owner, Vector list, boolean addXML) { 157 if (namespaceList!=null) { 158 int max = list.size(); 159 for (int i=0; i<namespaceList.length; i++) { 160 int nscode = namespaceList[i]; 161 int prefixCode = nscode>>16; 162 163 boolean found = false; 164 165 for (int j=0; j<max; ) { 167 NamespaceImpl ns = (NamespaceImpl)list.elementAt(j++); 168 if (ns.getNamespaceCode() == prefixCode) { 169 found=true; 170 break; 171 } 172 } 173 if (!found) { 174 list.addElement( 175 new NamespaceImpl( 176 (ElementImpl)owner, nscode, list.size()+1)); 177 } 178 } 179 } 180 181 183 if (parent.getNodeType()!=NodeInfo.ROOT) { 184 ((ElementImpl)parent).addNamespaceNodes(owner, list, false); 185 } 186 187 if (addXML) { 188 int nsxml = (1<<16) + 1; 189 list.addElement( 190 new NamespaceImpl(this, nsxml, list.size()+1) 191 ); 192 } 193 } 194 195 199 200 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) throws TransformerException { 201 202 if (namespaceList!=null) { 203 for (int i=0; i<namespaceList.length; i++) { 204 out.writeNamespaceDeclaration(namespaceList[i]); 205 } 206 } 207 208 211 if (includeAncestors) { 212 if (parent.getNodeType()!=NodeInfo.ROOT) { 213 ((ElementImpl)parent).outputNamespaceNodes(out, true); 214 } 215 } 216 } 217 218 222 223 protected int[] getNamespaceCodes() { 224 Vector namespaceNodes = new Vector (); 225 addNamespaceNodes(this, namespaceNodes, true); 226 227 int[] namespaceCodes = new int[namespaceNodes.size()]; 229 for (int i=0; i<namespaceNodes.size(); i++) { 230 NamespaceImpl nsi = (NamespaceImpl)namespaceNodes.elementAt(i); 231 namespaceCodes[i] = nsi.getNamespaceCode(); 232 } 233 return namespaceCodes; 234 } 235 236 242 243 public AttributeCollection getAttributeList() { 244 return attributeList; 245 } 246 247 253 254 public boolean hasAttributes() { 255 return attributeList.getLength() > 0; 256 } 257 258 266 267 public String getAttributeValue( String uri, String localName ) { 268 return attributeList.getValue(uri, localName); 269 } 270 271 280 281 public String getAttributeValue( String name ) { 282 return attributeList.getValue(name); 283 } 284 285 290 291 public String getAttributeValue(int fingerprint) { 292 return attributeList.getValueByFingerprint(fingerprint); 293 } 294 295 303 304 public void setAttribute(String name, String value ) throws DOMException { 305 disallowUpdate(); 306 } 307 308 313 314 public void copy(Outputter out, boolean allNamespaces) throws TransformerException { 315 int nc = getNameCode(); 316 out.writeStartTag(nc); 317 318 320 outputNamespaceNodes(out, allNamespaces); 321 322 324 for (int i=0; i<attributeList.getLength(); i++) { 325 out.writeAttribute(attributeList.getNameCode(i), 326 attributeList.getValue(i)); 327 } 328 329 331 NodeImpl next = (NodeImpl)getFirstChild(); 332 while (next!=null) { 333 if (next instanceof ElementImpl) { 334 ((ElementImpl)next).copy(out, false); 335 } else { 336 next.copy(out); 337 } 338 next = (NodeImpl)next.getNextSibling(); 339 } 340 341 out.writeEndTag(nc); 342 } 343 344 348 349 358 359 public String getAttribute(String name) { 360 int index = attributeList.getIndex(name); 361 if (index<0) return ""; 362 return attributeList.getValue(index); 363 } 364 365 369 370 public NamedNodeMap getAttributes() { 371 return this; 372 } 373 374 378 379 public void removeAttribute(String name) { 380 setAttribute(name, null); 381 } 382 383 394 395 public Attr getAttributeNode(String name) { 396 int index = getAttributeList().getIndex(name); 397 if (index<0) { 398 return null; 399 } 400 return new AttributeImpl(this, index); 401 } 402 403 408 409 public Attr setAttributeNode(Attr newAttr) throws DOMException { 410 disallowUpdate(); 411 return null; 412 } 413 414 419 420 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 421 disallowUpdate(); 422 return null; 423 } 424 425 434 435 public String getAttributeNS(String namespaceURI, String localName) { 436 String value = getAttributeValue(namespaceURI, localName); 437 return (value==null ? "" : value); 438 } 439 440 450 451 public void setAttributeNS(String namespaceURI, 452 String qualifiedName, 453 String value) 454 throws DOMException { 455 disallowUpdate(); 456 } 457 458 464 465 public void removeAttributeNS(String namespaceURI, 466 String localName) 467 throws DOMException { 468 disallowUpdate(); 469 } 470 471 481 482 public Attr getAttributeNodeNS(String namespaceURI, String localName) { 483 int index = attributeList.getIndex(namespaceURI, localName); 484 if (index<0) return null; 485 return new AttributeImpl(this, index); 486 } 487 488 499 500 public Attr setAttributeNodeNS(Attr newAttr) 501 throws DOMException { 502 disallowUpdate(); 503 return null; 504 } 505 506 516 517 public boolean hasAttribute(String name) { 518 return attributeList.getIndex(name) >= 0; 519 } 520 521 533 534 public boolean hasAttributeNS(String namespaceURI, String localName) { 535 return (getAttributeValue(namespaceURI, localName) != null); 536 } 537 538 542 546 547 public Node getNamedItem(String name) { 548 return getAttributeNode(name); 549 } 550 551 554 555 public Node setNamedItem(Node arg) throws DOMException { 556 disallowUpdate(); 557 return null; 558 } 559 560 563 564 public Node removeNamedItem(String name) throws DOMException { 565 disallowUpdate(); 566 return null; 567 } 568 569 573 574 public Node item(int index) { 575 if (index<0 || index>=attributeList.getLength()) { 576 return null; 577 } 578 return new AttributeImpl(this, index); 579 } 580 581 585 586 public int getLength() { 587 return attributeList.getLength(); 588 } 589 590 594 595 public Node getNamedItemNS(String uri, String localName) { 596 return getAttributeNodeNS(uri, localName); 597 } 598 599 602 603 public Node setNamedItemNS(Node arg) throws DOMException { 604 disallowUpdate(); 605 return null; 606 } 607 608 611 612 public Node removeNamedItemNS(String uri, String localName) throws DOMException { 613 disallowUpdate(); 614 return null; 615 } 616 617 } 618 619 | Popular Tags |