| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.event.*; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.om.TreeModel; |
| 13 | import net.sf.saxon.serialize.SerializationProperties; |
| 14 | |
| 15 | import java.net.URI; |
| 16 | |
| 17 | /** |
| 18 | * An <code>XdmDestination</code> is a {@link Destination} in which an {@link XdmNode} |
| 19 | * is constructed to hold the output of a query or transformation: |
| 20 | * that is, a tree using Saxon's implementation of the XDM data model |
| 21 | * <p>No data needs to be supplied to the <code>XdmDestination</code> object. The query or transformation |
| 22 | * populates an <code>XdmNode</code>, which may then be retrieved using the <code>getXdmNode</code> |
| 23 | * method.</p> |
| 24 | * <p>An <code>XdmDestination</code> is designed to hold a single tree rooted at a document node. |
| 25 | * If the result of a query is an arbitrary sequence, it will be normalized according to the rules |
| 26 | * of the "sequence normalization" operation described in the W3C XSLT/XQuery Serialization |
| 27 | * Recommendation, which results in either a single document node, or an error.</p> |
| 28 | * <p>An XdmDestination can be reused to hold the results of a second query or transformation only |
| 29 | * if the {@link #reset} method is first called to reset its state.</p> |
| 30 | * <p>If an XDM tree is to be built from a lexical XML document, or programmatically from the application |
| 31 | * by writing a sequence of events, the recommended mechanism is to use a {@link DocumentBuilder} rather |
| 32 | * than this class.</p> |
| 33 | * @since 9.1. Changed in 9.9 to perform sequence normalization, so the result is always a single |
| 34 | * document node. To get the raw results of a query or stylesheet without sequence normalization, |
| 35 | * use a {@link RawDestination}. |
| 36 | */ |
| 37 | |
| 38 | public class XdmDestination extends AbstractDestination { |
| 39 | |
| 40 | TreeModel treeModel = TreeModel.TINY_TREE; |
| 41 | Builder builder; |
| 42 | |
| 43 | public XdmDestination() { } |
| 44 | |
| 45 | /** |
| 46 | * Set the base URI for the document node that will be created when the XdmDestination is written to. |
| 47 | * This method must be called before writing to the destination; it has no effect on an XdmNode that |
| 48 | * has already been constructed. |
| 49 | * |
| 50 | * @param baseURI the base URI for the node that will be constructed when the XdmDestination is written to. |
| 51 | * This must be an absolute URI |
| 52 | * @throws IllegalArgumentException if the baseURI supplied is not an absolute URI |
| 53 | * @since 9.1 |
| 54 | */ |
| 55 | |
| 56 | public void setBaseURI(URI baseURI) { |
| 57 | if (!baseURI.isAbsolute()) { |
| 58 | throw new IllegalArgumentException("Supplied base URI must be absolute"); |
| 59 | } |
| 60 | setDestinationBaseURI(baseURI); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the base URI that will be used for the document node when the XdmDestination is written to. |
| 65 | * |
| 66 | * @return the base URI that will be used for the node that is constructed when the XdmDestination is written to. |
| 67 | * @since 9.1 |
| 68 | */ |
| 69 | |
| 70 | public URI getBaseURI() { |
| 71 | return getDestinationBaseURI(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Set the tree model to be used for documents constructed using this XdmDestination. |
| 76 | * By default, the TinyTree is used. |
| 77 | * |
| 78 | * @param model typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE}, |
| 79 | * {@link TreeModel#TINY_TREE_CONDENSED}, or {@link TreeModel#LINKED_TREE}. However, in principle |
| 80 | * a user-defined tree model can be used. |
| 81 | * @since 9.2 |
| 82 | */ |
| 83 | |
| 84 | public void setTreeModel(TreeModel model) { |
| 85 | this.treeModel = model; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the tree model to be used for documents constructed using this XdmDestination. |
| 90 | * By default, the TinyTree is used. |
| 91 | * |
| 92 | * @return the tree model in use: typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE}, |
| 93 | * {@link net.sf.saxon.om.TreeModel#TINY_TREE_CONDENSED}, or {@link TreeModel#LINKED_TREE}. However, in principle |
| 94 | * a user-defined tree model can be used. |
| 95 | * @since 9.2 |
| 96 | */ |
| 97 | |
| 98 | public TreeModel getTreeModel() { |
| 99 | return treeModel; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Return a Receiver. Saxon calls this method to obtain a Receiver, to which it then sends |
| 104 | * a sequence of events representing the content of an XML document. |
| 105 | * |
| 106 | * @param pipe The Saxon configuration. This is supplied so that the destination can |
| 107 | * use information from the configuration (for example, a reference to the name pool) |
| 108 | * to construct or configure the returned Receiver. |
| 109 | * @param params The serialization properties requested. Largely irrelevant for this {@code Destination}, |
| 110 | * except perhaps for {@code item-separator}. |
| 111 | * @return the Receiver to which events are to be sent. |
| 112 | */ |
| 113 | |
| 114 | public Receiver getReceiver(PipelineConfiguration pipe, SerializationProperties params) { |
| 115 | TreeModel model = treeModel; |
| 116 | if (model == null) { |
| 117 | int m = pipe.getParseOptions().getTreeModel(); |
| 118 | if (m != Builder.UNSPECIFIED_TREE_MODEL) { |
| 119 | model = TreeModel.getTreeModel(m); |
| 120 | } |
| 121 | if (model == null) { |
| 122 | model = TreeModel.TINY_TREE; |
| 123 | } |
| 124 | } |
| 125 | builder = model.makeBuilder(pipe); |
| 126 | String systemId = getBaseURI() == null ? null : getBaseURI().toASCIIString(); |
| 127 | if (systemId != null) { |
| 128 | builder.setUseEventLocation(false); |
| 129 | builder.setBaseURI(systemId); |
| 130 | } |
| 131 | SequenceNormalizer sn = params.makeSequenceNormalizer(builder); |
| 132 | sn.setSystemId(systemId); |
| 133 | sn.onClose(helper.getListeners()); |
| 134 | return sn; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Close the destination, allowing resources to be released. Saxon calls this method when |
| 139 | * it has finished writing to the destination. |
| 140 | */ |
| 141 | |
| 142 | public void close() { |
| 143 | // no action |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return the node at the root of the tree, after it has been constructed. |
| 148 | * <p>This method should not be called while the tree is under construction.</p> |
| 149 | * |
| 150 | * @return the root node of the tree (always a document or element node); or null if |
| 151 | * nothing is written to the tree (for example, the result of a query that returns the |
| 152 | * empty sequence) |
| 153 | * @throws IllegalStateException if called during the execution of the process that |
| 154 | * is writing the tree. |
| 155 | */ |
| 156 | |
| 157 | public XdmNode getXdmNode() { |
| 158 | if (builder == null) { |
| 159 | throw new IllegalStateException("The document has not yet been built"); |
| 160 | } |
| 161 | NodeInfo node = builder.getCurrentRoot(); |
| 162 | return node == null ? null : (XdmNode) XdmValue.wrap(node); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Allow the <code>XdmDestination</code> to be reused, without resetting other properties |
| 167 | * of the destination. |
| 168 | */ |
| 169 | |
| 170 | public void reset() { |
| 171 | builder = null; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | } |
| 176 |