1 package net.sf.saxon.tree; 2 import net.sf.saxon.event.LocationCopier; 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.om.*; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.Type; 7 import net.sf.saxon.style.StandardNames; 8 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 13 19 20 24 public class ElementWithAttributes extends ElementImpl { 25 26 protected AttributeCollection attributeList; protected int[] namespaceList = null; 31 32 38 39 public void initialise(int nameCode, AttributeCollectionImpl atts, NodeInfo parent, 40 String baseURI, int lineNumber, int sequenceNumber) { 41 this.nameCode = nameCode; 42 this.attributeList = atts; 43 this.parent = (ParentNodeImpl)parent; 44 this.sequence = sequenceNumber; 45 this.root = (DocumentImpl)parent.getDocumentRoot(); 46 root.setLineNumber(sequenceNumber, lineNumber); 47 root.setSystemId(sequenceNumber, baseURI); 48 } 49 50 53 54 public void setNamespaceDeclarations(int[] namespaces, int namespacesUsed) { 55 namespaceList = new int[namespacesUsed]; 56 System.arraycopy(namespaces, 0, namespaceList, 0, namespacesUsed); 57 } 58 59 71 72 public String getURIForPrefix(String prefix, boolean useDefault) { 73 if (prefix.equals("xml")) { 74 return NamespaceConstant.XML; 75 } 76 if (prefix.equals("") && !useDefault) { 77 return ""; 78 } 79 80 NamePool pool = getNamePool(); 81 int prefixCode = pool.getCodeForPrefix(prefix); 82 if (prefixCode==-1) { 83 return null; 84 } 85 try { 86 short uriCode = getURICodeForPrefixCode(prefixCode); 87 return pool.getURIFromURICode(uriCode); 88 } catch (NamespaceException e) { 89 return null; 90 } 91 } 92 93 97 98 public Iterator iteratePrefixes() { 99 Set inScope = new HashSet (10); 100 Set outOfScope = new HashSet (10); 101 inScope.add(""); 102 inScope.add("xml"); 103 gatherNamespacePrefixes(getNamePool(), inScope, outOfScope); 104 return inScope.iterator(); 105 } 106 107 114 115 public short getURICodeForPrefix(String prefix) throws NamespaceException { 116 if (prefix.equals("xml")) return NamespaceConstant.XML_CODE; 117 118 NamePool pool = getNamePool(); 119 int prefixCode = pool.getCodeForPrefix(prefix); 120 if (prefixCode==-1) { 121 throw new NamespaceException(prefix); 122 } 123 return getURICodeForPrefixCode(prefixCode); 124 } 125 126 private short getURICodeForPrefixCode(int prefixCode) throws NamespaceException { 127 if (namespaceList!=null) { 128 for (int i=0; i<namespaceList.length; i++) { 129 if ((namespaceList[i]>>16) == prefixCode) { 130 return (short)(namespaceList[i] & 0xffff); 131 } 132 } 133 } 134 NodeInfo next = parent; 135 while (true) { 136 if (next.getNodeKind()==Type.DOCUMENT) { 137 if (prefixCode==0) return NamespaceConstant.NULL_CODE; 139 throw new NamespaceException(getNamePool().getPrefixFromNamespaceCode(prefixCode<<16)); 140 } else if (next instanceof ElementWithAttributes) { 141 return ((ElementWithAttributes)next).getURICodeForPrefixCode(prefixCode); 142 } else { 143 next = next.getParent(); 144 } 145 } 146 } 147 148 155 156 public String getPrefixForURI(String uri) { 157 if (uri.equals(NamespaceConstant.XML)) return "xml"; 158 159 NamePool pool = getNamePool(); 160 int uriCode = pool.getCodeForURI(uri); 161 if (uriCode<0) return null; 162 return getPrefixForURICode(uriCode); 163 } 164 165 private String getPrefixForURICode(int code) { 166 if (namespaceList!=null) { 167 for (int i=0; i<namespaceList.length; i++) { 168 if ((namespaceList[i] & 0xffff) == code) { 169 return getNamePool().getPrefixFromNamespaceCode(namespaceList[i]); 170 } 171 } 172 } 173 NodeInfo next = parent; 174 while (true) { 175 if (next instanceof DocumentInfo) { 176 return null; 177 } else if (next instanceof ElementWithAttributes) { 178 return ((ElementWithAttributes)next).getPrefixForURICode(code); 179 } else { 180 next = next.getParent(); 181 } 182 } 183 } 184 185 private void gatherNamespacePrefixes(NamePool pool, Set inScope, Set outOfScope) { 186 if (namespaceList!=null) { 187 for (int i=0; i<namespaceList.length; i++) { 188 int nscode = namespaceList[i]; 189 String prefix = pool.getPrefixFromNamespaceCode(nscode); 190 if ((nscode & 0xffff) == 0) { 191 outOfScope.add(prefix); 193 } else if (!outOfScope.contains(prefix)) { 194 inScope.add(prefix); 195 outOfScope.add(prefix); 196 } 197 } 198 } 199 200 202 NodeInfo parent = getParent(); 203 while (parent != null) { 204 if (parent instanceof ElementWithAttributes) { 205 ((ElementWithAttributes)parent).gatherNamespacePrefixes(pool, inScope, outOfScope); 206 } 207 } 208 } 209 210 211 215 216 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) throws XPathException { 217 218 if (namespaceList!=null) { 219 for (int i=0; i<namespaceList.length; i++) { 220 out.namespace(namespaceList[i], 0); 221 } 222 } 223 224 227 if (includeAncestors) { 228 if (parent.getNodeKind()!=Type.DOCUMENT) { 229 parent.sendNamespaceDeclarations(out, true); 230 } 231 } 232 } 233 234 249 250 public int[] getDeclaredNamespaces(int[] buffer) { 251 return namespaceList; 252 } 253 254 258 259 public int[] getInScopeNamespaceCodes() { 260 return new NamespaceIterator(this, null).getInScopeNamespaceCodes(); 261 } 262 263 269 270 public AttributeCollection getAttributeList() { 271 return attributeList; 272 } 273 274 279 280 public String getAttributeValue(int fingerprint) { 281 return attributeList.getValueByFingerprint(fingerprint); 282 } 283 284 290 291 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException { 292 293 int typeCode = (copyAnnotations ? getTypeAnnotation() : -1); 294 if (locationId == 0 && out instanceof LocationCopier) { 295 out.setSystemId(getBaseURI()); 296 ((LocationCopier)out).setLineNumber(getLineNumber()); 297 } 298 out.startElement(getNameCode(), typeCode, locationId, 0); 299 300 302 if (whichNamespaces != NO_NAMESPACES) { 303 sendNamespaceDeclarations(out, whichNamespaces==ALL_NAMESPACES); 304 } 305 306 308 for (int i=0; i<attributeList.getLength(); i++) { 309 out.attribute(attributeList.getNameCode(i), StandardNames.XDT_UNTYPED_ATOMIC, 310 attributeList.getValue(i), 0, 0); 311 } 312 313 out.startContent(); 314 315 317 int childNamespaces = (whichNamespaces==NO_NAMESPACES ? NO_NAMESPACES : LOCAL_NAMESPACES); 318 NodeImpl next = (NodeImpl)getFirstChild(); 319 while (next!=null) { 320 next.copy(out, childNamespaces, copyAnnotations, locationId); 321 next = (NodeImpl)next.getNextSibling(); 322 } 323 324 out.endElement(); 325 } 326 327 } 328 329 | Popular Tags |