| 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.Configuration; |
| 11 | import net.sf.saxon.tree.util.DocumentNumberAllocator; |
| 12 | |
| 13 | import java.util.*; |
| 14 | |
| 15 | /** |
| 16 | * A generic (model-independent) implementation of the TreeInfo interface, suitable for use with all |
| 17 | * tree models where the object representing the document node does not itself act as the TreeInfo |
| 18 | * implementation |
| 19 | */ |
| 20 | |
| 21 | public class GenericTreeInfo implements TreeInfo { |
| 22 | |
| 23 | private Configuration config; |
| 24 | private NodeInfo root; |
| 25 | private String systemId; |
| 26 | private Map<String, Object> userData; |
| 27 | private long documentNumber = -1; |
| 28 | private SpaceStrippingRule spaceStrippingRule = NoElementsSpaceStrippingRule.getInstance(); |
| 29 | |
| 30 | /** |
| 31 | * Create the TreeInfo |
| 32 | * @param config the Saxon Configuration |
| 33 | */ |
| 34 | |
| 35 | public GenericTreeInfo(Configuration config) { |
| 36 | this.config = config; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create the TreeInfo |
| 41 | * @param config the Saxon Configuration |
| 42 | * @param root the root node |
| 43 | * @throws java.lang.IllegalArgumentException if the supplied node is not parentless |
| 44 | */ |
| 45 | |
| 46 | public GenericTreeInfo(Configuration config, NodeInfo root) { |
| 47 | this.config = config; |
| 48 | setRootNode(root); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the configuration (containing the name pool used for all names in this tree) |
| 53 | * |
| 54 | * @param config the configuration |
| 55 | */ |
| 56 | |
| 57 | public void setConfiguration(Configuration config) { |
| 58 | this.config = config; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the Configuration to which this tree belongs |
| 63 | * |
| 64 | * @return the configuration |
| 65 | */ |
| 66 | public Configuration getConfiguration() { |
| 67 | return config; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the root node of the tree |
| 72 | * @param root the root node (not necessarily a document node) |
| 73 | * @throws java.lang.IllegalArgumentException if the supplied node is not parentless |
| 74 | */ |
| 75 | |
| 76 | public void setRootNode(NodeInfo root) { |
| 77 | if (root.getParent() != null) { |
| 78 | throw new IllegalArgumentException("The root node of a tree must be parentless"); |
| 79 | } |
| 80 | this.root = root; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the NodeInfo object representing the root of the tree (not necessarily a document node) |
| 85 | * |
| 86 | * @return the root node |
| 87 | */ |
| 88 | public NodeInfo getRootNode() { |
| 89 | return root; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the systemId of the document node (for most implementations, this is likely to be the systemId |
| 94 | * of the entire tree) |
| 95 | * @param systemId the system ID |
| 96 | */ |
| 97 | |
| 98 | public void setSystemId(String systemId) { |
| 99 | this.systemId = systemId; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the systemId of the document node |
| 104 | * @return the system ID |
| 105 | */ |
| 106 | |
| 107 | public String getSystemId() { |
| 108 | return systemId; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the Public ID of the entity containing the node. |
| 113 | * |
| 114 | * @return null (always) |
| 115 | * @since 9.7 |
| 116 | */ |
| 117 | public String getPublicId() { |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the document number, which identifies this tree uniquely within a Configuration |
| 123 | * @return the document number |
| 124 | */ |
| 125 | |
| 126 | public long getDocumentNumber() { |
| 127 | if (documentNumber == -1) { |
| 128 | DocumentNumberAllocator dna = config.getDocumentNumberAllocator(); |
| 129 | synchronized (this) { |
| 130 | if (documentNumber == -1) { |
| 131 | documentNumber = dna.allocateDocumentNumber(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return documentNumber; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set the document number, which identifies this tree uniquely within a Configuration |
| 140 | * @param documentNumber the document number allocated to this tree |
| 141 | */ |
| 142 | |
| 143 | public synchronized void setDocumentNumber(long documentNumber) { |
| 144 | this.documentNumber = documentNumber; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Ask whether the tree contains any nodes whose type annotation is anything other than |
| 150 | * UNTYPED |
| 151 | * |
| 152 | * @return true if the document contains elements or attributes whose type is other than UNTYPED |
| 153 | */ |
| 154 | public boolean isTyped() { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the element with a given ID, if any |
| 160 | * |
| 161 | * @param id the required ID value |
| 162 | * @param getParent true if running the element-with-id() function rather than the id() |
| 163 | * function; the difference is that in the case of an element of type xs:ID, the parent of |
| 164 | * the element should be returned, not the element itself. |
| 165 | * @return the element with the given ID, or null if there is no such ID |
| 166 | * present (or if the parser has not notified attributes as being of |
| 167 | * type ID) |
| 168 | * @since 8.4. Second argument added in 9.2. |
| 169 | */ |
| 170 | public NodeInfo selectID(String id, boolean getParent) { |
| 171 | return null; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the list of unparsed entities defined in this document |
| 176 | * |
| 177 | * @return an Iterator, whose items are of type String, containing the names of all |
| 178 | * unparsed entities defined in this document. If there are no unparsed entities or if the |
| 179 | * information is not available then an empty iterator is returned |
| 180 | * @since 9.1 |
| 181 | */ |
| 182 | public Iterator<String> getUnparsedEntityNames() { |
| 183 | List<String> e = Collections.emptyList(); |
| 184 | return e.iterator(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the unparsed entity with a given name |
| 189 | * |
| 190 | * @param name the name of the entity |
| 191 | * @return if the entity exists, return an array of two Strings, the first |
| 192 | * holding the system ID of the entity (as an absolute URI if possible), |
| 193 | * the second holding the public ID if there is one, or null if not. |
| 194 | * If the entity does not exist, the method returns null. |
| 195 | * Applications should be written on the assumption that this array may |
| 196 | * be extended in the future to provide additional information. |
| 197 | * @since 8.4 |
| 198 | */ |
| 199 | public String[] getUnparsedEntity(String name) { |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Set details of space stripping action that was applied to this document during |
| 205 | * construction. This ensures that space stripping is not applied twice to the same |
| 206 | * document. |
| 207 | * |
| 208 | * @param rule details of the space stripping rules that have been applied to this |
| 209 | * document during its construction. |
| 210 | * @since 9.9 |
| 211 | */ |
| 212 | public void setSpaceStrippingRule(SpaceStrippingRule rule) { |
| 213 | this.spaceStrippingRule = rule; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get details of space stripping action that was applied to this document during |
| 218 | * construction. This ensures that space stripping is not applied twice to the same |
| 219 | * document. |
| 220 | * |
| 221 | * @return details of the space stripping rules that have been applied to this |
| 222 | * document during its construction. By default, returns {@link NoElementsSpaceStrippingRule}, |
| 223 | * indicating that no space stripping has been applied |
| 224 | * @since 9.9 |
| 225 | */ |
| 226 | public SpaceStrippingRule getSpaceStrippingRule() { |
| 227 | return spaceStrippingRule; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Set user data on the tree. The user data can be retrieved subsequently |
| 232 | * using {@link #getUserData} |
| 233 | * |
| 234 | * @param key A string giving the name of the property to be set. Clients are responsible |
| 235 | * for choosing a key that is likely to be unique. Must not be null. Keys used internally |
| 236 | * by Saxon are prefixed "saxon:". |
| 237 | * @param value The value to be set for the property. May be null, which effectively |
| 238 | */ |
| 239 | public void setUserData(String key, Object value) { |
| 240 | if (userData == null) { |
| 241 | userData = new HashMap<String, Object>(); |
| 242 | } |
| 243 | userData.put(key, value); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get user data held in the tree. This retrieves properties previously set using |
| 248 | * {@link #setUserData} |
| 249 | * |
| 250 | * @param key A string giving the name of the property to be retrieved. |
| 251 | * @return the value of the property, or null if the property has not been defined. |
| 252 | */ |
| 253 | public Object getUserData(String key) { |
| 254 | if (userData == null) { |
| 255 | return userData; |
| 256 | } else { |
| 257 | return userData.get(key); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | public boolean isStreamed() { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | } |
| 267 |