1 package com.icl.saxon.tinytree; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.Controller; 4 import com.icl.saxon.Context; 5 import com.icl.saxon.PreviewManager; 6 import com.icl.saxon.ExtendedInputSource; 7 import com.icl.saxon.expr.SingletonNodeSet; 8 9 import org.xml.sax.Attributes ; 10 import javax.xml.transform.TransformerException ; 11 12 13 19 20 public class TinyBuilder extends Builder 21 22 { 23 24 private int currentDepth = 0; 25 private int nodeNr = 0; private int attributeNodeNr = 0; 27 private int namespaceNodeNr = 0; 28 private boolean ended = false; 29 30 private int[] prevAtDepth = new int[100]; 31 32 public void createDocument () { 33 currentDocument = new TinyDocumentImpl(); 34 if (locator==null) { 35 locator = this; 36 } 37 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 38 doc.setSystemId(locator.getSystemId()); 39 doc.setNamePool(namePool); 40 } 41 42 45 46 public void startDocument () throws TransformerException 47 { 48 failed = false; 50 if (started) { 51 return; 53 } 54 started = true; 55 56 if (currentDocument==null) { 57 createDocument(); 59 } else { 60 if (!(currentDocument instanceof TinyDocumentImpl)) { 62 throw new TransformerException ("Root node supplied is of wrong type"); 63 } 64 if (currentDocument.hasChildNodes()) { 65 throw new TransformerException ("Supplied document is not empty"); 66 } 67 currentDocument.setNamePool(namePool); 68 } 69 70 currentDepth = 0; 72 nodeNr = 0; 73 74 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 75 if (lineNumbering) { 76 doc.setLineNumbering(); 77 } 78 79 doc.addNode(NodeInfo.ROOT, 0, 0, 0, -1); 80 prevAtDepth[0] = 0; 81 doc.next[0] = -1; 82 83 currentDepth++; 84 nodeNr++; 85 86 } 87 88 91 92 public void endDocument () throws TransformerException 93 { 94 96 if (ended) return; ended = true; 98 99 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 100 int prev = prevAtDepth[currentDepth]; 101 if (prev > 0) { 102 doc.next[prev] = -1; 103 } 104 prevAtDepth[currentDepth] = -1; 105 106 108 } 109 110 113 114 public void startElement ( 115 int nameCode, Attributes attributes, int[] namespaces, int namespacesUsed) throws TransformerException 116 { 117 119 121 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 122 123 125 int firstNS = (namespacesUsed==0 ? -1 : doc.numberOfNamespaces); 126 for (int n=0; n<namespacesUsed; n++) { 127 doc.addNamespace( nodeNr, 128 namespaces[n] ); 129 } 130 namespacesUsed = 0; 131 132 134 int numAtts = attributes.getLength(); 135 int firstAtt = (numAtts==0 ? -1 : doc.numberOfAttributes); 136 137 doc.addNode(NodeInfo.ELEMENT, currentDepth, firstAtt, firstNS, nameCode); 138 139 for (int i=0; i<numAtts; i++) { 140 int anamecode = namePool.allocate( 141 Name.getPrefix(attributes.getQName(i)), 142 attributes.getURI(i), 143 attributes.getLocalName(i)); 144 doc.addAttribute( nodeNr, 145 anamecode, 146 attributes.getType(i), 147 attributes.getValue(i) ); 148 } 149 150 151 152 int prev = prevAtDepth[currentDepth]; 153 if (prev > 0) { 154 doc.next[prev] = nodeNr; 155 } 156 prevAtDepth[currentDepth] = nodeNr; 157 currentDepth++; 158 159 if (currentDepth == prevAtDepth.length) { 160 int[] p2 = new int[currentDepth*2]; 161 System.arraycopy(prevAtDepth, 0, p2, 0, currentDepth); 162 prevAtDepth = p2; 163 } 164 prevAtDepth[currentDepth] = -1; 165 166 if (locator!=null) { 167 doc.setSystemId(nodeNr, locator.getSystemId()); 168 if (lineNumbering) { 169 doc.setLineNumber(nodeNr, locator.getLineNumber()); 170 } 171 } 172 nodeNr++; 173 } 174 175 178 179 public void endElement (int nameCode) throws TransformerException 180 { 181 183 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 184 185 if (previewManager != null) { 187 188 int elementFP = nameCode & 0xfffff; 189 if (previewManager.isPreviewElement(elementFP)) { 190 NodeInfo currentNode = doc.getNode(prevAtDepth[currentDepth-1]); 191 Context context = controller.makeContext(currentNode); 193 controller.applyTemplates( 194 context, 195 new SingletonNodeSet(currentNode), 196 controller.getRuleManager().getMode(previewManager.getPreviewMode()), 197 null); 198 199 nodeNr = prevAtDepth[currentDepth-1] + 1; 201 doc.truncate(nodeNr); 202 } 204 } 205 206 int prev = prevAtDepth[currentDepth]; 207 if (prev > 0) { 208 doc.next[prev] = -1; 209 } 210 prevAtDepth[currentDepth] = -1; 211 212 currentDepth--; 213 } 214 215 218 219 public void characters (char ch[], int start, int len) throws TransformerException 220 { 221 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 223 if (len>0) { 224 int bufferStart = doc.charBufferLength; 225 doc.appendChars(ch, start, len); 226 doc.addNode(NodeInfo.TEXT, currentDepth, bufferStart, len, -1); 227 228 int prev = prevAtDepth[currentDepth]; 229 if (prev > 0) { 230 doc.next[prev] = nodeNr; 231 } 232 prevAtDepth[currentDepth] = nodeNr; 233 234 nodeNr++; 235 236 } 237 238 239 } 240 241 242 247 248 public void processingInstruction (String piname, String remainder) throws TransformerException 249 { 250 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 252 if (!discardComments) { 253 int s = doc.commentBuffer.length(); 254 doc.commentBuffer.append(remainder); 255 int nameCode = namePool.allocate("", "", piname); 256 doc.addNode(NodeInfo.PI, currentDepth, s, remainder.length(), 257 nameCode); 258 259 int prev = prevAtDepth[currentDepth]; 260 if (prev > 0) { 261 doc.next[prev] = nodeNr; 262 } 263 prevAtDepth[currentDepth] = nodeNr; 264 265 nodeNr++; 266 267 } 272 } 273 274 277 278 public void comment (char ch[], int start, int length) throws TransformerException 279 { 280 addComment(new String (ch, start, length)); 281 } 282 283 private void addComment(String comment) throws TransformerException { 284 TinyDocumentImpl doc = (TinyDocumentImpl)currentDocument; 285 if (!discardComments && !inDTD) { 286 int s = doc.commentBuffer.length(); 287 doc.commentBuffer.append(comment); 288 doc.addNode(NodeInfo.COMMENT, currentDepth, s, comment.length(), -1); 289 290 int prev = prevAtDepth[currentDepth]; 291 if (prev > 0) { 292 doc.next[prev] = nodeNr; 293 } 294 prevAtDepth[currentDepth] = nodeNr; 295 296 nodeNr++; 297 } 298 299 } 300 301 304 305 public void setUnparsedEntity(String name, String uri) { 306 ((TinyDocumentImpl)currentDocument).setUnparsedEntity(name, uri); 307 } 308 309 310 311 } 313 | Popular Tags |