| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.event; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.NamePool; |
| 13 | import net.sf.saxon.om.NodeInfo; |
| 14 | import net.sf.saxon.om.NodeName; |
| 15 | import net.sf.saxon.trans.CommandLineOptions; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.tree.tiny.TinyDocumentImpl; |
| 18 | import net.sf.saxon.type.SchemaType; |
| 19 | |
| 20 | /** |
| 21 | * The abstract Builder class is responsible for taking a stream of SAX events |
| 22 | * and constructing a Document tree. There is one concrete subclass for each |
| 23 | * tree implementation. |
| 24 | * |
| 25 | * @author Michael H. Kay |
| 26 | */ |
| 27 | |
| 28 | public abstract class Builder implements Receiver { |
| 29 | /** |
| 30 | * Constant denoting a request for the default tree model |
| 31 | */ |
| 32 | public static final int UNSPECIFIED_TREE_MODEL = -1; |
| 33 | /** |
| 34 | * Constant denoting the "linked tree" in which each node is represented as an object |
| 35 | */ |
| 36 | public static final int LINKED_TREE = 0; |
| 37 | /** |
| 38 | * Alternative constant denoting the "linked tree" in which each node is represented as an object |
| 39 | * Retained for backwards compatibility |
| 40 | */ |
| 41 | public static final int STANDARD_TREE = 0; |
| 42 | /** |
| 43 | * Constant denoting the "tiny tree" in which the tree is represented internally using arrays of integers |
| 44 | */ |
| 45 | public static final int TINY_TREE = 1; |
| 46 | /** |
| 47 | * Constant denoting the "tiny tree condensed", a variant of the tiny tree in which text and attribute nodes |
| 48 | * sharing the same string value use shared storage for the value. |
| 49 | */ |
| 50 | public static final int TINY_TREE_CONDENSED = 2; |
| 51 | |
| 52 | public static final int JDOM_TREE = 3; |
| 53 | public static final int JDOM2_TREE = 4; |
| 54 | public static final int AXIOM_TREE = 5; |
| 55 | public static final int DOMINO_TREE = 6; |
| 56 | public static final int K_TREE = 7; |
| 57 | |
| 58 | protected PipelineConfiguration pipe; |
| 59 | protected Configuration config; |
| 60 | protected NamePool namePool; |
| 61 | protected String systemId; |
| 62 | protected String baseURI; |
| 63 | protected NodeInfo currentRoot; |
| 64 | protected boolean lineNumbering = false; |
| 65 | protected boolean useEventLocation = true; |
| 66 | |
| 67 | protected boolean started = false; |
| 68 | protected boolean timing = false; |
| 69 | protected boolean open = false; |
| 70 | |
| 71 | private long startTime; |
| 72 | |
| 73 | /** |
| 74 | * Create a Builder and initialise variables |
| 75 | */ |
| 76 | |
| 77 | public Builder() { |
| 78 | } |
| 79 | |
| 80 | public Builder(PipelineConfiguration pipe) { |
| 81 | this.pipe = pipe; |
| 82 | config = pipe.getConfiguration(); |
| 83 | lineNumbering = config.isLineNumbering(); |
| 84 | namePool = config.getNamePool(); |
| 85 | } |
| 86 | |
| 87 | public void setPipelineConfiguration(/*@NotNull*/ PipelineConfiguration pipe) { |
| 88 | this.pipe = pipe; |
| 89 | config = pipe.getConfiguration(); |
| 90 | lineNumbering = lineNumbering || config.isLineNumbering(); |
| 91 | namePool = config.getNamePool(); |
| 92 | } |
| 93 | |
| 94 | /*@NotNull*/ |
| 95 | public PipelineConfiguration getPipelineConfiguration() { |
| 96 | return pipe; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the Configuration |
| 101 | * |
| 102 | * @return the Saxon configuration |
| 103 | */ |
| 104 | |
| 105 | /*@NotNull*/ |
| 106 | public Configuration getConfiguration() { |
| 107 | return config; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get a builder monitor for this builder. This must be called immediately after opening the builder, |
| 112 | * and all events to the builder must thenceforth be sent via the BuilderMonitor. |
| 113 | * |
| 114 | * @return a new BuilderMonitor appropriate to this kind of Builder; or null if the Builder does |
| 115 | * not provide this service. The default implementation returns null. |
| 116 | */ |
| 117 | |
| 118 | /*@Nullable*/ |
| 119 | public BuilderMonitor getBuilderMonitor() { |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Say that the system IDs of constructed nodes (especially element nodes) are to be |
| 125 | * taken from the system ID property of the {@link Location} object passed as a parameter to |
| 126 | * the {@link #startElement(NodeName, SchemaType, Location, int)} event. This property |
| 127 | * should be set to true when building a document from events originating with an XML parser, |
| 128 | * because the {@link Location} object in this case will have a system ID that changes |
| 129 | * as external entities are processed. The base URI of a node in this case is determined |
| 130 | * by the system ID, modified by any <code>xml:base</code> attributes. If the property |
| 131 | * is set to false, all nodes in the tree have the same system ID, this being supplied |
| 132 | * as the systemID property of the builder. This will typically be the static base URI |
| 133 | * of the instruction in the query or stylesheet that was used to construct the root node; |
| 134 | * in the case of <code>xsl:result-document</code>, it will be the absolutized value of the |
| 135 | * <code>href</code> attribute of the instruction. |
| 136 | * @param useEventLocation true if the system ID is to be taken from the Location parameter of |
| 137 | * each event. The default value is true. |
| 138 | */ |
| 139 | |
| 140 | public void setUseEventLocation(boolean useEventLocation) { |
| 141 | this.useEventLocation = useEventLocation; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Ask whether the system IDs of constructed nodes (especially element nodes) are to be |
| 146 | * taken from the system ID property of the {@link Location} object passed as a parameter to |
| 147 | * the {@link #startElement(NodeName, SchemaType, Location, int)} event. |
| 148 | * @return true if the system ID is to be taken from the Location parameter of |
| 149 | * each event. The default value is true. |
| 150 | */ |
| 151 | |
| 152 | public boolean isUseEventLocation() { |
| 153 | return useEventLocation; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * The SystemId is equivalent to the document-uri property defined in the XDM data model. |
| 158 | * It should be set only in the case of a document that is potentially retrievable via this URI. |
| 159 | * This means it should not be set in the case of a temporary tree constructed in the course of |
| 160 | * executing a query or transformation. |
| 161 | * |
| 162 | * @param systemId the SystemId, that is, the document-uri. |
| 163 | */ |
| 164 | |
| 165 | public void setSystemId(/*@Nullable*/ String systemId) { |
| 166 | this.systemId = systemId; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * The SystemId is equivalent to the document-uri property defined in the XDM data model. |
| 171 | * It should be set only in the case of a document that is potentially retrievable via this URI. |
| 172 | * This means the value will be null in the case of a temporary tree constructed in the course of |
| 173 | * executing a query or transformation. |
| 174 | * |
| 175 | * @return the SystemId, that is, the document-uri. |
| 176 | */ |
| 177 | |
| 178 | /*@Nullable*/ |
| 179 | public String getSystemId() { |
| 180 | return systemId; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Set the base URI of the document node of the tree being constructed by this builder |
| 185 | * |
| 186 | * @param baseURI the base URI |
| 187 | */ |
| 188 | |
| 189 | public void setBaseURI(/*@Nullable*/ String baseURI) { |
| 190 | this.baseURI = baseURI; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get the base URI of the document node of the tree being constructed by this builder |
| 195 | * |
| 196 | * @return the base URI |
| 197 | */ |
| 198 | |
| 199 | /*@Nullable*/ |
| 200 | public String getBaseURI() { |
| 201 | return baseURI; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | ///////////////////////////////////////////////////////////////////////// |
| 206 | // Methods setting and getting options for building the tree |
| 207 | ///////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | /** |
| 210 | * Set line numbering on or off |
| 211 | * |
| 212 | * @param lineNumbering set to true if line numbers are to be maintained for nodes in the tree being |
| 213 | * constructed. |
| 214 | */ |
| 215 | |
| 216 | public void setLineNumbering(boolean lineNumbering) { |
| 217 | this.lineNumbering = lineNumbering; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Set timing option on or off |
| 222 | * |
| 223 | * @param on set to true to turn timing on. This causes the builder to display statistical information |
| 224 | * about the tree that is constructed. It corresponds to the command line -t option |
| 225 | */ |
| 226 | |
| 227 | public void setTiming(boolean on) { |
| 228 | timing = on; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get timing option |
| 233 | * |
| 234 | * @return true if timing information has been requested |
| 235 | */ |
| 236 | |
| 237 | public boolean isTiming() { |
| 238 | return timing; |
| 239 | } |
| 240 | |
| 241 | public void open() { |
| 242 | if (timing && !open) { |
| 243 | getConfiguration().getStandardErrorOutput().println( |
| 244 | "Building tree for " + getSystemId() + " using " + getClass()); |
| 245 | startTime = System.nanoTime(); |
| 246 | } |
| 247 | open = true; |
| 248 | } |
| 249 | |
| 250 | public void close() throws XPathException { |
| 251 | if (timing && open) { |
| 252 | long endTime = System.nanoTime(); |
| 253 | getConfiguration().getStandardErrorOutput().println( |
| 254 | "Tree built in " + CommandLineOptions.showExecutionTimeNano(endTime - startTime)); |
| 255 | if (currentRoot instanceof TinyDocumentImpl) { |
| 256 | ((TinyDocumentImpl) currentRoot).showSize(); |
| 257 | } |
| 258 | startTime = endTime; |
| 259 | } |
| 260 | open = false; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations |
| 265 | * supplied on element and attribute events |
| 266 | * |
| 267 | * @return true if the Receiver makes any use of this information. If false, the caller |
| 268 | * may supply untyped nodes instead of supplying the type annotation |
| 269 | */ |
| 270 | |
| 271 | public boolean usesTypeAnnotations() { |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get the current root node. This will normally be a document node, but if the root of the tree |
| 277 | * is an element node, it can be an element. |
| 278 | * |
| 279 | * @return the root of the tree that is currently being built, or that has been most recently built |
| 280 | * using this builder |
| 281 | */ |
| 282 | |
| 283 | /*@Nullable*/ |
| 284 | public NodeInfo getCurrentRoot() { |
| 285 | return currentRoot; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Reset the builder to its initial state. The most important effect of calling this |
| 290 | * method (implemented in subclasses) is to release any links to the constructed document |
| 291 | * tree, allowing the memory occupied by the tree to released by the garbage collector even |
| 292 | * if the Builder is still in memory. This can happen because the Builder is referenced from a |
| 293 | * parser in the Configuration's parser pool. |
| 294 | */ |
| 295 | |
| 296 | public void reset() { |
| 297 | systemId = null; |
| 298 | baseURI = null; |
| 299 | currentRoot = null; |
| 300 | lineNumbering = false; |
| 301 | started = false; |
| 302 | timing = false; |
| 303 | open = false; |
| 304 | } |
| 305 | |
| 306 | } |
| 307 |