| 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.type.SchemaType; |
| 12 | import net.sf.saxon.type.SimpleType; |
| 13 | |
| 14 | /** |
| 15 | * An extension of the NodeInfo interface defining a node that can be updated. The updating methods are |
| 16 | * closely modelled on the updating primitives defined in the XQuery Update specification, however, their |
| 17 | * semantics should not be assumed to be identical to those primitives. |
| 18 | * <p>These primitives may leave the tree in a state where the type annotations of nodes do not correctly |
| 19 | * describe the structure of the nodes. It is the caller's responsibility to maintain the integrity of |
| 20 | * type annotations.</p> |
| 21 | * <p>This interface was introduced in Saxon 9.1 and modified in Saxon 9.2. |
| 22 | * Some aspects of the semantics are not clearly specified, for example the effect of each |
| 23 | * operation on namespace bindings, and such details may change in the future. The primary purpose |
| 24 | * of this interface is as an internal interface for use by XQuery Update; applications use it (or |
| 25 | * implement it) at their own risk.</p> |
| 26 | */ |
| 27 | |
| 28 | public interface MutableNodeInfo extends NodeInfo { |
| 29 | |
| 30 | /** |
| 31 | * Set the type annotation on a node. This must only be called when the caller has verified (by validation) |
| 32 | * that the node is a valid instance of the specified type. The call is ignored if the node is not an element |
| 33 | * or attribute node. |
| 34 | * |
| 35 | * @param type the type annotation (possibly including high bits set to indicate the isID, isIDREF, and |
| 36 | * isNilled properties) |
| 37 | */ |
| 38 | |
| 39 | void setTypeAnnotation(SchemaType type); |
| 40 | |
| 41 | /** |
| 42 | * Insert a sequence of nodes as children of this node. |
| 43 | * <p>This method takes no action unless the target node is a document node or element node. It also |
| 44 | * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or |
| 45 | * processing instructions.</p> |
| 46 | * <p>The supplied nodes will form the new children. Adjacent text nodes will be merged, and |
| 47 | * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their |
| 48 | * parent property and to add namespace bindings, or they may be copied, at the discretion of |
| 49 | * the implementation.</p> |
| 50 | * |
| 51 | * @param source the nodes to be inserted. The implementation determines what implementation classes |
| 52 | * of node it will accept; all implementations must accept nodes constructed using the Builder supplied |
| 53 | * by the {@link #newBuilder} method on this object. The supplied nodes may be modified in situ, for example |
| 54 | * to change their parent property and to add namespace bindings, but this depends on the implementation. |
| 55 | * The argument array may be modified as a result of the call. |
| 56 | * @param atStart true if the new nodes are to be inserted before existing children; false if they are |
| 57 | * to be inserted after existing children |
| 58 | * @param inherit true if the inserted nodes are to inherit the namespaces of their new parent; false |
| 59 | * if such namespaces are to be undeclared |
| 60 | * @throws IllegalArgumentException if the supplied nodes use a node implementation that this |
| 61 | * implementation does not accept. |
| 62 | */ |
| 63 | |
| 64 | void insertChildren(NodeInfo[] source, boolean atStart, boolean inherit); |
| 65 | |
| 66 | /** |
| 67 | * Insert a sequence of nodes as siblings of this node. |
| 68 | * <p>This method takes no action unless the target node is an element, text node, comment, or |
| 69 | * processing instruction, and one that has a parent node. It also |
| 70 | * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or |
| 71 | * processing instructions.</p> |
| 72 | * <p>The supplied nodes will form new children of this node. Adjacent text nodes will be merged, and |
| 73 | * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their |
| 74 | * parent property and to add namespace bindings, or they may be copied, at the discretion of |
| 75 | * the implementation.</p> |
| 76 | * |
| 77 | * @param source the nodes to be inserted. The implementation determines what implementation classes |
| 78 | * of node it will accept; all implementations must accept nodes constructed using the Builder supplied |
| 79 | * by the {@link #newBuilder} method on this object. The supplied nodes may be modified in situ, for example |
| 80 | * to change their parent property and to add namespace bindings, but this depends on the implementation. |
| 81 | * The argument array may be modified as a result of the call. |
| 82 | * @param before true if the new nodes are to be inserted before the target node; false if they are |
| 83 | * to be inserted after the target node |
| 84 | * @param inherit true if the inserted nodes are to inherit the namespaces of their new parent; false |
| 85 | * if such namespaces are to be undeclared |
| 86 | * @throws IllegalArgumentException if any supplied node is not a text node, element, comment, or |
| 87 | * processing instruction or if any of the supplied nodes uses a node implementation that this |
| 88 | * implementation does not accept. |
| 89 | */ |
| 90 | |
| 91 | void insertSiblings(NodeInfo[] source, boolean before, boolean inherit); |
| 92 | |
| 93 | /** |
| 94 | * Remove an attribute from this element node |
| 95 | * <p>If the target node is not an element, or if the supplied node is not an attribute |
| 96 | * of this element, this method takes no action.</p> |
| 97 | * <p>The attribute object itself becomes unusable; any attempt to use this attribute object, |
| 98 | * or any other object representing the same node, is likely to result in an exception. |
| 99 | * Calling the {@link #isDeleted} method on the attribute object will return true.</p> |
| 100 | * |
| 101 | * @param attribute the attribute node to be removed |
| 102 | */ |
| 103 | |
| 104 | void removeAttribute(NodeInfo attribute); |
| 105 | |
| 106 | /** |
| 107 | * Add an attribute to this element node. |
| 108 | * <p>If this node is not an element, the method takes no action. |
| 109 | * If the element already has an attribute with this name, the method |
| 110 | * throws an exception.</p> |
| 111 | * <p>This method does not perform any namespace fixup. It is the caller's responsibility |
| 112 | * to ensure that any namespace prefix used in the name of the attribute (or in its value |
| 113 | * if it has a namespace-sensitive type) is declared on this element.</p> |
| 114 | * |
| 115 | * @param name the name of the new attribute |
| 116 | * @param attType the type annotation of the new attribute |
| 117 | * @param value the string value of the new attribute |
| 118 | * @param properties properties including IS_ID and IS_IDREF properties |
| 119 | * @throws IllegalStateException if the element already has an attribute with the given name. |
| 120 | */ |
| 121 | |
| 122 | void addAttribute(NodeName name, SimpleType attType, CharSequence value, int properties); |
| 123 | |
| 124 | /** |
| 125 | * Delete this node (that is, detach it from its parent). |
| 126 | * <p>Following this call, calling {@link #isDeleted} on the target node or on any of its |
| 127 | * descendants (or their attributes) will return true, and the node will in all other respects |
| 128 | * be unusable. The effect of other operations on the node is undefined.</p> |
| 129 | * <p>If the deleted node has preceding and following siblings that are both text nodes, |
| 130 | * the two text nodes will be joined into a single text node (the identity of this node |
| 131 | * with respect to the original text nodes is undefined).</p> |
| 132 | */ |
| 133 | |
| 134 | void delete(); |
| 135 | |
| 136 | /** |
| 137 | * Test whether this MutableNodeInfo object represents a node that has been deleted. |
| 138 | * Generally, such a node is unusable, and any attempt to use it will result in an exception |
| 139 | * being thrown. |
| 140 | * |
| 141 | * @return true if this node has been deleted |
| 142 | */ |
| 143 | |
| 144 | boolean isDeleted(); |
| 145 | |
| 146 | /** |
| 147 | * Replace this node with a given sequence of nodes. This node is effectively deleted, and the replacement |
| 148 | * nodes are attached to the parent of this node in its place. |
| 149 | * <p>The supplied nodes will become children of this node's parent. Adjacent text nodes will be merged, and |
| 150 | * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their |
| 151 | * parent property and to add namespace bindings, or they may be copied, at the discretion of |
| 152 | * the implementation.</p> |
| 153 | * |
| 154 | * @param replacement the replacement nodes. If this node is an attribute, the replacements |
| 155 | * must also be attributes; if this node is not an attribute, the replacements must not be attributes. |
| 156 | * The implementation determines what implementation classes |
| 157 | * of node it will accept; all implementations must accept nodes constructed using the Builder supplied |
| 158 | * by the {@link #newBuilder} method on this object. The supplied nodes may be modified in situ, for example |
| 159 | * to change their parent property and to add namespace bindings, but this depends on the implementation. |
| 160 | * The argument array may be modified as a result of the call. |
| 161 | * @param inherit true if the replacement nodes are to inherit the namespaces of their new parent; false |
| 162 | * if such namespaces are to be undeclared |
| 163 | * @throws IllegalArgumentException if any of the replacement nodes is of the wrong kind. When replacing |
| 164 | * a child node, the replacement nodes must all be elements, text, comment, or PI nodes; when replacing |
| 165 | * an attribute, the replacement nodes must all be attributes. |
| 166 | * @throws IllegalStateException if this node is deleted or if it has no parent node. |
| 167 | * @throws IllegalStateException if two replacement attributes have the same name. |
| 168 | */ |
| 169 | |
| 170 | void replace(NodeInfo[] replacement, boolean inherit); |
| 171 | |
| 172 | /** |
| 173 | * Replace the string-value of this node. If applied to an element or document node, this |
| 174 | * causes all existing children to be deleted, and replaced with a new text node |
| 175 | * whose string value is the value supplied. The caller is responsible for checking |
| 176 | * that the value is valid, for example that comments do not contain a double hyphen; the |
| 177 | * implementation is not required to check for such conditions. |
| 178 | * |
| 179 | * @param stringValue the new string value |
| 180 | */ |
| 181 | |
| 182 | void replaceStringValue(CharSequence stringValue); |
| 183 | |
| 184 | /** |
| 185 | * Rename this node. |
| 186 | * <p>This call has no effect if applied to a nameless node, such as a text node or comment.</p> |
| 187 | * <p>If necessary, a new namespace binding will be added to the target element, or to the element |
| 188 | * parent of the target attribute</p> |
| 189 | * |
| 190 | * @param newName the new name for the node |
| 191 | * @throws IllegalArgumentException if the new name code is not present in the name pool, or if |
| 192 | * it has a (prefix, uri) pair in which the |
| 193 | * prefix is the same as that of an existing in-scope namespace binding and the uri is different from that |
| 194 | * namespace binding. |
| 195 | */ |
| 196 | |
| 197 | void rename(NodeName newName); |
| 198 | |
| 199 | /** |
| 200 | * Add a namespace binding (that is, a namespace node) to this element. This call has no effect if applied |
| 201 | * to a node other than an element. |
| 202 | * |
| 203 | * @param nscode The namespace code representing the (prefix, uri) pair of the namespace binding to be |
| 204 | * added. If the target element already has a namespace binding with this (prefix, uri) pair, the call has |
| 205 | * no effect. If the target element currently has a namespace binding with this prefix and a different URI, an |
| 206 | * exception is raised. The new namespace binding will also be in scope for the subtree rooted |
| 207 | * at this node (that is, the namespace is implicitly inherited) |
| 208 | * @param unused Retained for backwards compatibility |
| 209 | * @throws IllegalArgumentException if the namespace code is not present in the namepool, or if the target |
| 210 | * element already has a namespace binding for this prefix |
| 211 | */ |
| 212 | |
| 213 | void addNamespace(NamespaceBinding nscode, boolean unused); |
| 214 | |
| 215 | /** |
| 216 | * Remove type information from this node (and its ancestors, recursively). |
| 217 | * This method implements the upd:removeType() primitive defined in the XQuery Update specification. |
| 218 | * (Note: the caller is responsible for updating the set of nodes marked for revalidation) |
| 219 | */ |
| 220 | |
| 221 | void removeTypeAnnotation(); |
| 222 | |
| 223 | /** |
| 224 | * Get a Builder suitable for building nodes that can be attached to this document. |
| 225 | * |
| 226 | * @return a new Builder that constructs nodes using the same object model implementation |
| 227 | * as this one, suitable for attachment to this tree |
| 228 | */ |
| 229 | |
| 230 | Builder newBuilder(); |
| 231 | |
| 232 | } |
| 233 |