1 package com.icl.saxon.tinytree; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.tree.DOMExceptionImpl; 4 import com.icl.saxon.expr.NodeSetExtent; 5 import com.icl.saxon.expr.XPathException; 6 import com.icl.saxon.om.Axis; 7 import com.icl.saxon.output.Outputter; 8 import com.icl.saxon.pattern.NameTest; 9 import com.icl.saxon.pattern.NamespaceTest; 10 import com.icl.saxon.pattern.AnyNodeTest; 11 12 import javax.xml.transform.TransformerException ; 13 14 import org.w3c.dom.Element ; 15 import org.w3c.dom.Attr ; 16 import org.w3c.dom.Node ; 17 import org.w3c.dom.NodeList ; 18 import org.w3c.dom.DOMException ; 19 20 26 27 final class TinyElementImpl extends TinyParentNodeImpl 28 implements Element { 29 30 33 34 public TinyElementImpl(TinyDocumentImpl doc, int nodeNr) { 35 this.document = doc; 36 this.nodeNr = nodeNr; 37 } 38 39 43 44 public final short getNodeType() { 45 return ELEMENT; 46 } 47 48 52 53 public String getBaseURI() { 54 String xmlBase = getAttributeValue(Namespace.XML, "base"); 55 if (xmlBase!=null) { 56 return xmlBase; 57 } 58 String startSystemId = getSystemId(); 59 NodeInfo parent = getParent(); 60 String parentSystemId = parent.getSystemId(); 61 if (startSystemId.equals(parentSystemId)) { 62 return parent.getBaseURI(); 63 } else { 64 return startSystemId; 65 } 66 } 67 68 75 76 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) 77 throws TransformerException { 78 79 int ns = document.length[nodeNr]; if (ns>0 ) { 81 while (ns < document.numberOfNamespaces && 82 document.namespaceParent[ns] == nodeNr ) { 83 int nscode = document.namespaceCode[ns]; 84 out.writeNamespaceDeclaration(nscode); 85 ns++; 86 } 87 } 88 89 92 if (includeAncestors && document.isUsingNamespaces()) { 93 getParent().outputNamespaceNodes(out, true); 94 } 96 } 97 98 104 105 public boolean hasAttributes() { 106 return document.offset[nodeNr] >= 0; 107 } 108 109 117 118 public String getAttributeValue( String uri, String localName ) { 119 int f = document.getNamePool().getFingerprint(uri, localName); 120 return getAttributeValue(f); 121 } 122 123 128 129 public String getAttributeValue(int fingerprint) { 130 int a = document.offset[nodeNr]; 131 if (a<0) return null; 132 while (a < document.numberOfAttributes && document.attParent[a] == nodeNr) { 133 if ((document.attCode[a] & 0xfffff) == fingerprint ) { 134 return document.attValue[a]; 135 } 136 a++; 137 } 138 return null; 139 } 140 141 146 147 public TinyAttributeImpl makeAttributeNode(int index) { 148 int a = document.offset[nodeNr]; 149 if (a<0) return null; 150 return document.getAttributeNode(a+index); 151 } 152 153 160 161 public void setAttribute(String name, String value ) throws DOMException { 162 throw new DOMExceptionImpl((short)9999, "Saxon DOM is not updateable"); 163 } 164 165 168 169 public void copy(Outputter out) throws TransformerException { 170 copy(out, true); 171 } 172 173 178 179 public void copy(Outputter out, boolean allNamespaces) throws TransformerException { 180 181 185 int nc = getNameCode(); 186 out.writeStartTag(nc); 187 188 190 outputNamespaceNodes(out, allNamespaces); 191 192 194 int a = document.offset[nodeNr]; 195 if (a >= 0) { 196 while (a < document.numberOfAttributes && document.attParent[a] == nodeNr) { 197 document.getAttributeNode(a).copy(out); 198 a++; 199 } 200 } 201 202 204 AxisEnumeration children = 205 getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()); 206 207 while (children.hasMoreElements()) { 208 NodeInfo next = children.nextElement(); 209 if (next instanceof TinyElementImpl) { 210 ((TinyElementImpl)next).copy(out, false); 211 } else { 213 next.copy(out); 214 } 215 } 216 out.writeEndTag(nc); 217 } 218 219 } 220 221 | Popular Tags |