1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.NodeInfo; 3 import com.icl.saxon.om.DocumentInfo; 4 import com.icl.saxon.om.NamespaceException; 5 import com.icl.saxon.om.NamePool; 6 import com.icl.saxon.om.Namespace; 7 import com.icl.saxon.om.Name; 8 import com.icl.saxon.expr.NodeSetExtent; 9 import com.icl.saxon.output.Outputter; 10 import com.icl.saxon.pattern.NameTest; 11 import com.icl.saxon.pattern.NamespaceTest; 12 13 import java.util.Vector ; 14 import org.w3c.dom.*; 15 16 import javax.xml.transform.TransformerException ; 17 18 24 25 29 public class ElementImpl extends ParentNodeImpl 30 implements Element { 31 32 private static AttributeCollection emptyAtts = new AttributeCollection((NamePool)null); 33 34 protected int nameCode; 35 protected DocumentImpl root; 36 37 40 41 public ElementImpl() {} 42 43 46 47 public void setNameCode(int nameCode) { 48 this.nameCode = nameCode; 49 } 50 51 57 58 public void initialise(int nameCode, AttributeCollection atts, NodeInfo parent, 59 String baseURI, int lineNumber, int sequenceNumber) { 60 this.nameCode = nameCode; 61 this.parent = (ParentNodeImpl)parent; 62 this.sequence = sequenceNumber; 63 this.root = (DocumentImpl)parent.getDocumentRoot(); 64 root.setLineNumber(sequenceNumber, lineNumber); 65 root.setSystemId(sequenceNumber, baseURI); 66 } 67 68 73 74 public void setSystemId(String uri) { 75 root.setSystemId(sequence, uri); 76 } 77 78 81 82 public DocumentInfo getDocumentRoot() { 83 return root; 84 } 85 86 89 90 public final String getSystemId() { 91 return ((DocumentImpl)getDocumentRoot()).getSystemId(sequence); 92 } 93 94 98 99 public String getBaseURI() { 100 String xmlBase = getAttributeValue(Namespace.XML, "base"); 101 if (xmlBase!=null) { 102 return xmlBase; 103 } 104 String startSystemId = getSystemId(); 105 String parentSystemId = parent.getSystemId(); 106 if (startSystemId.equals(parentSystemId)) { 107 return parent.getBaseURI(); 108 } else { 109 return startSystemId; 110 } 111 } 112 113 116 117 public void setLineNumber(int line) { 118 ((DocumentImpl)getDocumentRoot()).setLineNumber(sequence, line); 119 } 120 121 122 125 126 public int getLineNumber() { 127 return ((DocumentImpl)getDocumentRoot()).getLineNumber(sequence); 128 } 129 130 131 134 135 public int getNameCode() { 136 return nameCode; 137 } 138 139 144 145 public String generateId() { 146 return "e" + sequence; 147 } 148 149 156 157 public short getURICodeForPrefix(String prefix) throws NamespaceException { 158 if (prefix.equals("xml")) return Namespace.XML_CODE; 161 if (parent.getNodeType()==NodeInfo.ROOT) { 162 if (prefix.equals("")) { 163 return Namespace.NULL_CODE; 164 } 165 throw new NamespaceException(prefix); 166 } else { 167 return ((ElementImpl)parent).getURICodeForPrefix(prefix); 168 } 169 } 170 171 178 179 public String getPrefixForURI(String uri) { 180 if (parent.getNodeType()==NodeInfo.ROOT) { 181 return null; 182 } else { 183 return ((ElementImpl)parent).getPrefixForURI(uri); 184 } 185 } 186 187 196 197 public final int makeNameCode(String qname, boolean useDefault) 198 throws NamespaceException { 199 200 NamePool namePool = getNamePool(); 201 String prefix = Name.getPrefix(qname); 202 if (prefix.equals("")) { 203 short uriCode = 0; 204 205 if (useDefault) { 206 uriCode = getURICodeForPrefix(prefix); 207 } 208 209 return namePool.allocate(prefix, uriCode, qname); 210 211 } else { 212 String localName = Name.getLocalName(qname); 213 short uriCode = getURICodeForPrefix(prefix); 214 return namePool.allocate(prefix, uriCode, localName); 215 } 216 217 } 218 219 230 231 public void addNamespaceNodes(ElementImpl owner, Vector list, boolean addXML) { 232 234 if (parent.getNodeType()!=NodeInfo.ROOT) { 235 ((ElementImpl)parent).addNamespaceNodes(owner, list, false); 236 } 237 if (addXML) { 238 int nsxml = (1<<16) + 1; 239 list.addElement( 240 new NamespaceImpl(this, nsxml, list.size()+1) 241 ); 242 } 243 } 244 245 249 250 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) throws TransformerException { 251 252 255 if (includeAncestors) { 256 if (!(parent instanceof DocumentInfo)) { 257 ((ElementImpl)parent).outputNamespaceNodes(out, true); 258 } 259 } 260 } 261 262 263 264 268 269 public final short getNodeType() { 270 return ELEMENT; 271 } 272 273 279 280 public AttributeCollection getAttributeList() { 281 return emptyAtts; 282 } 283 284 293 294 public String getAttributeValue( String name ) { 295 return null; 296 } 297 298 299 303 304 public void setAttribute(String name, String value ) throws DOMException { 305 disallowUpdate(); 306 } 307 308 311 312 public void copy(Outputter out) throws TransformerException { 313 copy(out, true); 314 } 315 316 321 322 public void copy(Outputter out, boolean allNamespaces) throws TransformerException { 323 int nc = getNameCode(); 324 out.writeStartTag(nc); 325 326 328 outputNamespaceNodes(out, allNamespaces); 329 330 332 NodeImpl next = (NodeImpl)getFirstChild(); 333 while (next!=null) { 334 if (next instanceof ElementImpl) { 335 ((ElementImpl)next).copy(out, false); 336 } else { 337 next.copy(out); 338 } 339 next = (NodeImpl)next.getNextSibling(); 340 } 341 342 out.writeEndTag(nc); 343 } 344 345 } 346 347 | Popular Tags |