1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.Namespace; 5 import com.icl.saxon.om.NamePool; 6 import org.xml.sax.Attributes ; 7 import java.io.*; 8 import java.util.*; 9 import javax.xml.transform.TransformerException ; 10 11 16 17 public class NamespaceEmitter extends ProxyEmitter 18 { 19 protected NamePool namePool; 20 protected int nscodeXML; 21 protected int nscodeNull; 22 23 29 private int[] namespaces = new int[30]; private int namespacesSize = 0; private int[] namespaceStack = new int[100]; 32 private int nsStackTop = 0; 33 34 35 38 39 public void setNamePool(NamePool pool) { 40 namePool = pool; 42 nscodeXML = pool.getNamespaceCode("xml", Namespace.XML); 43 nscodeNull = pool.getNamespaceCode("", ""); 44 super.setNamePool(pool); 45 } 46 47 48 52 53 public void startElement(int nameCode, Attributes attList, 54 int[] namespaceCodes, int nrOfCodes) throws TransformerException { 55 56 61 63 int[] uniqueNamespaces = new int[namespaceCodes.length+1]; 64 int uniqueCount = 0; 65 66 68 int elementNamespace = namePool.allocateNamespaceCode(nameCode); 69 if (isNeeded(elementNamespace)) { 70 addToStack(elementNamespace); 71 uniqueNamespaces[uniqueCount++] = elementNamespace; 72 } 73 74 76 for (int n=0; n<nrOfCodes; n++) { 77 int nscode = namespaceCodes[n]; 78 if (isNeeded(nscode)) { 79 addToStack(nscode); 80 uniqueNamespaces[uniqueCount++] = nscode; 81 } 82 } 83 84 86 if (nsStackTop>=namespaceStack.length) { 87 int[] newstack = new int[nsStackTop*2]; 88 System.arraycopy(namespaceStack, 0, newstack, 0, nsStackTop); 89 namespaceStack = newstack; 90 } 91 92 namespaceStack[nsStackTop++] = uniqueCount; 93 94 super.startElement(nameCode, attList, uniqueNamespaces, uniqueCount); 96 } 97 98 101 102 private boolean isNeeded(int nscode) { 103 if (nscode==nscodeXML) { 104 return false; 106 } 107 108 for (int i=namespacesSize-1; i>=0; i--) { 109 if (namespaces[i]==nscode) { 110 return false; 112 } 113 if ((namespaces[i]>>16) == (nscode>>16)) { 114 return true; 116 } 117 } 118 return (nscode != nscodeNull); 119 } 120 121 124 125 private void addToStack(int nscode) { 126 if (namespacesSize+1 >= namespaces.length) { 128 int[] newlist = new int[namespacesSize*2]; 129 System.arraycopy(namespaces, 0, newlist, 0, namespacesSize); 130 namespaces = newlist; 131 } 132 namespaces[namespacesSize++] = nscode; 133 } 134 135 136 139 140 141 public void endElement (int nameCode) throws TransformerException 142 { 143 if (nsStackTop-- == 0) { 144 throw new TransformerException ("Attempt to output end tag with no matching start tag"); 145 } 146 147 int nscount = namespaceStack[nsStackTop]; 148 namespacesSize -= nscount; 149 150 super.endElement(nameCode); 151 152 } 153 } 154 155 | Popular Tags |