1 package net.sf.saxon.event; 2 import net.sf.saxon.om.AttributeCollectionImpl; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.NamespaceConstant; 5 import net.sf.saxon.om.NamespaceResolver; 6 import net.sf.saxon.trans.XPathException; 7 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 18 19 public class StartTagBuffer extends ProxyReceiver implements NamespaceResolver 20 { 21 22 24 int elementNameCode; 25 int elementTypeCode; 26 int elementLocationId; 27 int elementProperties; 28 29 31 AttributeCollectionImpl bufferedAttributes; 32 33 39 private int[] namespaces = new int[50]; private int namespacesSize = 0; private int[] countStack = new int[50]; 42 private int depth = 0; 43 44 46 public void setPipelineConfiguration(PipelineConfiguration config) { 47 super.setPipelineConfiguration(config); 48 bufferedAttributes = new AttributeCollectionImpl(getNamePool()); 49 } 50 51 54 55 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 56 57 elementNameCode = nameCode; 58 elementTypeCode = typeCode; 59 elementLocationId = locationId; 60 elementProperties = properties; 61 62 bufferedAttributes.clear(); 63 64 66 countStack[depth] = 0; 67 if (++depth >= countStack.length) { 68 int[] newstack = new int[depth*2]; 69 System.arraycopy(countStack, 0, newstack, 0, depth); 70 countStack = newstack; 71 } 72 } 73 74 public void namespace(int namespaceCode, int properties) throws XPathException { 75 addToStack(namespaceCode); 76 countStack[depth - 1]++; 77 } 78 79 91 92 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) throws XPathException { 93 bufferedAttributes.addAttribute(nameCode, typeCode, value.toString(), locationId, properties); 94 } 95 96 99 100 private void addToStack(int nscode) { 101 if (namespacesSize+1 >= namespaces.length) { 103 int[] newlist = new int[namespacesSize*2]; 104 System.arraycopy(namespaces, 0, newlist, 0, namespacesSize); 105 namespaces = newlist; 106 } 107 namespaces[namespacesSize++] = nscode; 108 } 109 110 114 115 public void startContent() throws XPathException { 116 super.startElement(elementNameCode, elementTypeCode, elementLocationId, elementProperties); 117 declareNamespacesForStartElement(); 118 119 for (int i=0; i<bufferedAttributes.getLength(); i++) { 120 super.attribute(bufferedAttributes.getNameCode(i), 121 bufferedAttributes.getTypeAnnotation(i), 122 bufferedAttributes.getValue(i), 123 bufferedAttributes.getLocationId(i), 124 bufferedAttributes.getProperties(i)); 125 } 126 super.startContent(); 127 } 128 129 protected void declareNamespacesForStartElement() throws XPathException { 130 for (int i=namespacesSize - countStack[depth-1]; i<namespacesSize; i++) { 131 super.namespace(namespaces[i], 0); 132 } 133 } 134 135 protected void declareAllNamespaces() throws XPathException { 136 for (int i=0; i<namespacesSize; i++) { 137 super.namespace(namespaces[i], 0); 138 } 139 } 140 141 144 145 public void endElement () throws XPathException { 146 super.endElement(); 147 undeclareNamespacesForElement(); 148 } 149 150 protected void undeclareNamespacesForElement() { 151 int nscount = countStack[--depth]; 152 namespacesSize -= nscount; 153 } 154 155 158 159 public int getElementNameCode() { 160 return elementNameCode; 161 } 162 163 167 168 public String getAttribute(int nameCode) { 169 return bufferedAttributes.getValueByFingerprint(nameCode & 0xfffff); 170 } 171 172 178 179 protected short getURICode(short prefixCode) { 180 for (int i=namespacesSize-1; i>=0; i--) { 181 if ((namespaces[i]>>16) == (prefixCode)) { 182 return (short)(namespaces[i]&0xffff); 183 } 184 } 185 if (prefixCode == 0) { 186 return 0; } else { 188 return -1; 189 } 190 } 191 192 201 202 public String getURIForPrefix(String prefix, boolean useDefault) { 203 NamePool pool = getNamePool(); 204 if ("".equals(prefix) && !useDefault) { 205 return ""; 206 } else if ("xml".equals(prefix)) { 207 return NamespaceConstant.XML; 208 } else { 209 short prefixCode = pool.getCodeForPrefix(prefix); 210 short uriCode = getURICode(prefixCode); 211 if (uriCode == -1) { 212 return null; 213 } 214 return pool.getURIFromURICode(uriCode); 215 } 216 } 217 218 222 223 public Iterator iteratePrefixes() { 224 NamePool pool = getNamePool(); 225 List prefixes = new ArrayList (namespacesSize); 226 for (int i=namespacesSize-1; i>=0; i--) { 227 String prefix = pool.getPrefixFromNamespaceCode(namespaces[i]); 228 if (!prefixes.contains(prefix)) { 229 prefixes.add(prefix); 230 } 231 } 232 prefixes.add("xml"); 233 return prefixes.iterator(); 234 } 235 } 236 237 | Popular Tags |