1 package net.sf.saxon.event; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.Controller; 5 import net.sf.saxon.om.DocumentInfo; 6 import net.sf.saxon.om.NamePool; 7 import net.sf.saxon.om.NodeInfo; 8 import net.sf.saxon.om.StrippedDocument; 9 import net.sf.saxon.tinytree.TinyBuilder; 10 import net.sf.saxon.tinytree.TinyDocumentImpl; 11 import net.sf.saxon.trans.DynamicError; 12 import net.sf.saxon.trans.XPathException; 13 import net.sf.saxon.tree.TreeBuilder; 14 15 import javax.xml.transform.Source ; 16 import javax.xml.transform.dom.DOMSource ; 17 import java.util.Date ; 18 19 25 26 public abstract class Builder implements Receiver { 27 public static final int STANDARD_TREE = 0; 28 public static final int TINY_TREE = 1; 29 30 protected PipelineConfiguration pipe; 31 protected Configuration config; 32 protected NamePool namePool; 33 protected String systemId; 34 protected NodeInfo currentRoot; 35 protected boolean lineNumbering = false; 36 37 protected boolean started = false; 38 protected boolean timing = false; 39 40 private long startTime; 41 42 45 46 public Builder() { 47 } 48 49 public void setPipelineConfiguration(PipelineConfiguration pipe) { 50 this.pipe = pipe; 51 this.config = pipe.getConfiguration(); 52 this.namePool = config.getNamePool(); 53 this.lineNumbering = (lineNumbering || config.isLineNumbering()); 54 } 55 56 public PipelineConfiguration getPipelineConfiguration () { 57 return pipe; 58 } 59 60 public Configuration getConfiguration() { 61 return config; 62 } 63 64 public void setSystemId(String systemId) { 65 this.systemId = systemId; 66 } 67 68 public String getSystemId() { 69 return systemId; 70 } 71 72 public void setLineNumbering(boolean is) { 73 lineNumbering = is; 74 } 75 76 80 86 87 91 92 95 96 public void setTiming(boolean on) { 97 timing = on; 98 } 99 100 103 104 public boolean isTiming() { 105 return timing; 106 } 107 108 public void open() throws XPathException { 109 if (timing) { 110 System.err.println("Building tree for " + getSystemId() + " using " + getClass()); 111 startTime = (new Date ()).getTime(); 112 } 113 } 114 115 public void close() throws XPathException { 116 if (timing) { 117 long endTime = (new Date ()).getTime(); 118 System.err.println("Tree built in " + (endTime - startTime) + " milliseconds"); 119 if (currentRoot instanceof TinyDocumentImpl) { 120 ((TinyDocumentImpl)currentRoot).showSize(); 121 } 122 startTime = endTime; 123 } 124 } 125 126 130 131 public void startDocument(int properties) throws XPathException { } 132 133 136 137 public void endDocument() throws XPathException { } 138 139 145 146 public NodeInfo getCurrentRoot() { 147 return currentRoot; 148 } 149 150 159 160 public static NodeInfo build(Source source, Stripper stripper, Configuration config) throws XPathException { 161 if (source == null) { 162 throw new NullPointerException ("Source supplied to builder cannot be null"); 163 } 164 165 NodeInfo start; 166 if (source instanceof DOMSource || source instanceof NodeInfo) { 167 start = Controller.unravel(source, config); 168 if (stripper != null) { 169 DocumentInfo docInfo = start.getDocumentRoot(); 170 StrippedDocument strippedDoc = new StrippedDocument(docInfo, stripper); 171 start = strippedDoc.wrap(start); 172 } 173 174 } else { 175 Builder b; 177 if (config.getTreeModel() == Builder.TINY_TREE) { 178 b = new TinyBuilder(); 179 } else { 180 b = new TreeBuilder(); 181 } 182 PipelineConfiguration pipe = config.makePipelineConfiguration(); 183 pipe.setConfiguration(config); 184 b.setPipelineConfiguration(pipe); 185 b.setLineNumbering(config.isLineNumbering()); 186 Receiver receiver = b; 187 if (stripper != null) { 188 stripper.setUnderlyingReceiver(b); 189 receiver = stripper; 190 } 191 try { 192 new Sender(pipe).send(source, receiver); 193 } catch (XPathException err) { 194 throw new DynamicError(err); 195 } 196 start = b.getCurrentRoot(); 197 } 198 return start; 199 } 200 201 } 202 203 | Popular Tags |