| 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.om; |
| 9 | |
| 10 | import net.sf.saxon.event.Builder; |
| 11 | import net.sf.saxon.event.PipelineConfiguration; |
| 12 | import net.sf.saxon.tree.linked.LinkedTreeBuilder; |
| 13 | import net.sf.saxon.tree.tiny.TinyBuilder; |
| 14 | import net.sf.saxon.tree.tiny.TinyBuilderCondensed; |
| 15 | |
| 16 | /** |
| 17 | * A TreeModel represents an implementation of the Saxon NodeInfo interface, which itself |
| 18 | * is essentially an implementation of the XDM model defined in W3C specifications (except |
| 19 | * that Saxon's NodeInfo understands the 13 XPath axes, rather than merely supporting |
| 20 | * parent and child properties). |
| 21 | * <p>This class serves two purposes: it acts as a factory for obtaining a Builder which |
| 22 | * can be used to build trees using this tree model; and it provides static constants |
| 23 | * that can be used to identify the built-in tree models.</p> |
| 24 | */ |
| 25 | |
| 26 | public abstract class TreeModel { |
| 27 | |
| 28 | /** |
| 29 | * The TinyTree implementation. This is normally the default implementation |
| 30 | * of the tree model. |
| 31 | */ |
| 32 | public final static TreeModel TINY_TREE = new TinyTree(); |
| 33 | |
| 34 | /** |
| 35 | * The CondensedTinyTree implementation. This is a variant of the TinyTree that |
| 36 | * uses less memory but takes a little longer to build. Run-time performance |
| 37 | * is the same as the TinyTree. |
| 38 | */ |
| 39 | public final static TreeModel TINY_TREE_CONDENSED = new TinyTreeCondensed(); |
| 40 | |
| 41 | /** |
| 42 | * The LinkedTree. This takes more memory than the TinyTree. The main advantage |
| 43 | * of this model is that it is updateable: the nodes in a LinkedTree can be modified |
| 44 | * using XQuery Updates. |
| 45 | */ |
| 46 | public final static TreeModel LINKED_TREE = new LinkedTree(); |
| 47 | |
| 48 | /** |
| 49 | * Make a Builder, which can then be used to construct an instance of this tree model |
| 50 | * from a stream of events |
| 51 | * |
| 52 | * @param pipe A PipelineConfiguration, which can be constructed using the method |
| 53 | * {@link net.sf.saxon.Configuration#makePipelineConfiguration()}. |
| 54 | * @return a newly created Builder |
| 55 | */ |
| 56 | |
| 57 | public abstract Builder makeBuilder(PipelineConfiguration pipe); |
| 58 | |
| 59 | /** |
| 60 | * Get the integer constant used to identify this tree model in some legacy interfaces |
| 61 | * |
| 62 | * @return an integer constant used to identify the model, for example {@link Builder#TINY_TREE} |
| 63 | */ |
| 64 | |
| 65 | public int getSymbolicValue() { |
| 66 | return Builder.UNSPECIFIED_TREE_MODEL; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the tree model corresponding to a given integer constant |
| 71 | * |
| 72 | * @param symbolicValue one of the constants {@link Builder#TINY_TREE}, |
| 73 | * {@link Builder#TINY_TREE_CONDENSED}, or {@link Builder#LINKED_TREE} |
| 74 | * @return the corresponding TreeModel |
| 75 | */ |
| 76 | |
| 77 | public static TreeModel getTreeModel(int symbolicValue) { |
| 78 | switch (symbolicValue) { |
| 79 | case Builder.TINY_TREE: |
| 80 | return TreeModel.TINY_TREE; |
| 81 | case Builder.TINY_TREE_CONDENSED: |
| 82 | return TreeModel.TINY_TREE_CONDENSED; |
| 83 | case Builder.LINKED_TREE: |
| 84 | return TreeModel.LINKED_TREE; |
| 85 | default: |
| 86 | throw new IllegalArgumentException("tree model " + symbolicValue); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Ask whether this tree model supports updating (that is, whether the nodes |
| 92 | * in the constructed tree will implement {@link MutableNodeInfo}, which is necessary |
| 93 | * if they are to support XQuery Update. This method can be overridden in subclasses; |
| 94 | * the default implementation returns false. |
| 95 | * |
| 96 | * @return true if the tree model implementation supports updating, that is, if its |
| 97 | * nodes support the MutableNodeInfo interface. |
| 98 | */ |
| 99 | |
| 100 | public boolean isMutable() { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Ask whether this tree model supports the use of type annotations on element and attribute |
| 106 | * nodes. If false, all nodes in this tree are untyped (for elements) or untypedAtomic (for attributes). |
| 107 | * The default implementation returns false. |
| 108 | * |
| 109 | * @return true if type annotations other than xs:untyped and xs:untypedAtomic are supported. |
| 110 | */ |
| 111 | |
| 112 | public boolean isSchemaAware() { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get a name that identifies the tree model |
| 118 | * |
| 119 | * @return an identifying name for the tree model |
| 120 | */ |
| 121 | |
| 122 | public String getName() { |
| 123 | return toString(); |
| 124 | } |
| 125 | |
| 126 | private static class TinyTree extends TreeModel { |
| 127 | |
| 128 | public Builder makeBuilder(PipelineConfiguration pipe) { |
| 129 | TinyBuilder builder = new TinyBuilder(pipe); |
| 130 | builder.setStatistics(pipe.getConfiguration().getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 131 | return builder; |
| 132 | } |
| 133 | |
| 134 | public int getSymbolicValue() { |
| 135 | return Builder.TINY_TREE; |
| 136 | } |
| 137 | |
| 138 | public boolean isSchemaAware() { |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | public String getName() { |
| 143 | return "TinyTree"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private static class TinyTreeCondensed extends TreeModel { |
| 148 | public Builder makeBuilder(PipelineConfiguration pipe) { |
| 149 | TinyBuilderCondensed tbc = new TinyBuilderCondensed(pipe); |
| 150 | tbc.setStatistics(pipe.getConfiguration().getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 151 | return tbc; |
| 152 | } |
| 153 | |
| 154 | public int getSymbolicValue() { |
| 155 | return Builder.TINY_TREE_CONDENSED; |
| 156 | } |
| 157 | |
| 158 | public boolean isSchemaAware() { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | public String getName() { |
| 163 | return "TinyTreeCondensed"; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private static class LinkedTree extends TreeModel { |
| 168 | |
| 169 | /*@NotNull*/ |
| 170 | public Builder makeBuilder(PipelineConfiguration pipe) { |
| 171 | return new LinkedTreeBuilder(pipe); |
| 172 | } |
| 173 | |
| 174 | public int getSymbolicValue() { |
| 175 | return Builder.LINKED_TREE; |
| 176 | } |
| 177 | |
| 178 | public boolean isMutable() { |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | public boolean isSchemaAware() { |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | public String getName() { |
| 187 | return "LinkedTree"; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | } |
| 193 |