| 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 | |
| 12 | import javax.xml.transform.Source; |
| 13 | import java.util.Iterator; |
| 14 | |
| 15 | /** |
| 16 | * This interface represents information about a tree as a whole. The tree may or may not be rooted |
| 17 | * at a document node. In some tree models, the interface is implemented by the object representing the |
| 18 | * root node of the tree (typically but not necessarily the document node). In other tree models, |
| 19 | * it is a free-standing object. The TreeInfo holds information that is common to all the nodes in |
| 20 | * a tree, such as the document number and a reference to the Configuration. |
| 21 | * |
| 22 | * <p>Java object identity for TreeInfo objects equates to XPath node identity for the root |
| 23 | * nodes of the relevant trees: that is, two root nodes are "the same node" if and only if |
| 24 | * their TreeInfo objects are the same Java object. However, when sorting into document order, |
| 25 | * the order of trees is based on their "document number", a unique number allocated by the |
| 26 | * document number allocator for the Configuration.</p> |
| 27 | * |
| 28 | * @author Michael H. Kay |
| 29 | * @since 9.7. Replaces the DocumentInfo interface which represented both a document as a whole, and |
| 30 | * the document node at the root of a document, but which did not apply to trees rooted at a node other |
| 31 | * than a document node. |
| 32 | */ |
| 33 | |
| 34 | public interface TreeInfo extends Source { |
| 35 | |
| 36 | /** |
| 37 | * Get the NodeInfo object representing the node at the root of the tree, which will often |
| 38 | * but not invariably be a document node |
| 39 | * @return the node at the root of the tree |
| 40 | */ |
| 41 | |
| 42 | NodeInfo getRootNode(); |
| 43 | |
| 44 | /** |
| 45 | * Get the Configuration to which the nodes in this tree belong |
| 46 | * @return the configuration |
| 47 | */ |
| 48 | |
| 49 | Configuration getConfiguration(); |
| 50 | |
| 51 | /** |
| 52 | * Get the document number, which identifies this tree uniquely within a Configuration. |
| 53 | * Document numbers are used when sorting nodes from different trees into document order. |
| 54 | * @return the document number |
| 55 | */ |
| 56 | |
| 57 | long getDocumentNumber(); |
| 58 | |
| 59 | /** |
| 60 | * Ask whether the tree contains any nodes whose type annotation is anything other than |
| 61 | * UNTYPED or UNTYPED_ATOMIC |
| 62 | * |
| 63 | * @return true if the tree contains elements or attributes whose type is other than |
| 64 | * UNTYPED or UNTYPED_ATOMIC. This is typically the case only if the document has been |
| 65 | * schema-validated (one can imagine an external tree model in which the type annotations |
| 66 | * arise some other way, but they must always be consistent with the typed value of the |
| 67 | * node). |
| 68 | */ |
| 69 | |
| 70 | boolean isTyped(); |
| 71 | |
| 72 | /** |
| 73 | * Get the element with a given ID, if any |
| 74 | * |
| 75 | * @param id the required ID value |
| 76 | * @param getParent true if running the element-with-id() function rather than the id() |
| 77 | * function; the difference is that in the case of an element of type xs:ID, the parent of |
| 78 | * the element should be returned, not the element itself. |
| 79 | * @return the element with the given ID, or null if there is no such ID |
| 80 | * present (or if the parser has not notified attributes as being of |
| 81 | * type ID) |
| 82 | * @since 8.4. Second argument added in 9.2. |
| 83 | */ |
| 84 | |
| 85 | NodeInfo selectID(String id, boolean getParent); |
| 86 | |
| 87 | /** |
| 88 | * Get the list of unparsed entities defined in this document |
| 89 | * |
| 90 | * @return an Iterator, whose items are of type String, containing the names of all |
| 91 | * unparsed entities defined in this document. If there are no unparsed entities or if the |
| 92 | * information is not available then an empty iterator is returned |
| 93 | * @since 9.1 |
| 94 | */ |
| 95 | |
| 96 | Iterator<String> getUnparsedEntityNames(); |
| 97 | |
| 98 | /** |
| 99 | * Get the unparsed entity with a given name |
| 100 | * |
| 101 | * @param name the name of the entity |
| 102 | * @return if the entity exists, return an array of two Strings, the first |
| 103 | * holding the system ID of the entity (as an absolute URI if possible), |
| 104 | * the second holding the public ID if there is one, or null if not. |
| 105 | * If the entity does not exist, the method returns null. |
| 106 | * Applications should be written on the assumption that this array may |
| 107 | * be extended in the future to provide additional information. |
| 108 | * @since 8.4 |
| 109 | */ |
| 110 | |
| 111 | String[] getUnparsedEntity(String name); |
| 112 | |
| 113 | /** |
| 114 | * Set details of space stripping action that was applied to this document during |
| 115 | * construction. This ensures that space stripping is not applied twice to the same |
| 116 | * document. |
| 117 | * @param rule details of the space stripping rules that have been applied to this |
| 118 | * document during its construction. |
| 119 | * @since 9.9 |
| 120 | */ |
| 121 | |
| 122 | void setSpaceStrippingRule(SpaceStrippingRule rule); |
| 123 | |
| 124 | /** |
| 125 | * Get details of space stripping action that was applied to this document during |
| 126 | * construction. This ensures that space stripping is not applied twice to the same |
| 127 | * document. |
| 128 | * |
| 129 | * @return details of the space stripping rules that have been applied to this |
| 130 | * document during its construction. By default, returns {@link NoElementsSpaceStrippingRule}, |
| 131 | * indicating that no space stripping has been applied |
| 132 | * @since 9.9 |
| 133 | */ |
| 134 | |
| 135 | SpaceStrippingRule getSpaceStrippingRule(); |
| 136 | |
| 137 | /** |
| 138 | * Set user data that applies to this tree. The user data can be retrieved subsequently |
| 139 | * using {@link #getUserData} |
| 140 | * |
| 141 | * @param key A string giving the name of the property to be set. Clients are responsible |
| 142 | * for choosing a key that is likely to be unique. Must not be null. Keys used internally |
| 143 | * by Saxon are prefixed "saxon:". |
| 144 | * <p>The key <code>saxon:document-uri</code> may be used to set the document-uri |
| 145 | * property of the document node. If this is set to a non-empty string, the result |
| 146 | * of the document-uri() function on the document node will be that string, as an |
| 147 | * instance of xs:anyURI. If it is set to a zero-length string, the result of |
| 148 | * document-uri() will be the empty sequence, ().</p> |
| 149 | * @param value The value to be set for the property. May be null, which effectively |
| 150 | * removes the existing value for the property. |
| 151 | */ |
| 152 | |
| 153 | void setUserData(String key, Object value); |
| 154 | |
| 155 | /** |
| 156 | * Get user data that applies to this tree. This retrieves properties previously set using |
| 157 | * {@link #setUserData} |
| 158 | * |
| 159 | * @param key A string giving the name of the property to be retrieved. |
| 160 | * @return the value of the property, or null if the property has not been defined. |
| 161 | */ |
| 162 | |
| 163 | Object getUserData(String key); |
| 164 | |
| 165 | } |
| 166 |