| 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.dom; |
| 9 | |
| 10 | import net.sf.saxon.om.NodeInfo; |
| 11 | import net.sf.saxon.type.ComplexType; |
| 12 | import net.sf.saxon.type.SchemaType; |
| 13 | import net.sf.saxon.type.Type; |
| 14 | import net.sf.saxon.value.Whitespace; |
| 15 | import org.w3c.dom.Comment; |
| 16 | import org.w3c.dom.DOMException; |
| 17 | import org.w3c.dom.Text; |
| 18 | |
| 19 | /** |
| 20 | * This class is an implementation of the DOM Text and Comment interfaces that wraps a Saxon NodeInfo |
| 21 | * representation of a text or comment node. |
| 22 | */ |
| 23 | |
| 24 | public class TextOverNodeInfo extends NodeOverNodeInfo implements Text, Comment { |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Get the character data of a Text or Comment node. |
| 29 | * DOM method. |
| 30 | */ |
| 31 | |
| 32 | public String getData() { |
| 33 | return node.getStringValue(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Set the character data of a Text or Comment node. |
| 38 | * DOM method: always fails, Saxon tree is immutable. |
| 39 | */ |
| 40 | |
| 41 | public void setData(String data) throws DOMException { |
| 42 | disallowUpdate(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the length of a Text or Comment node. |
| 47 | * DOM method. |
| 48 | */ |
| 49 | |
| 50 | public int getLength() { |
| 51 | return node.getStringValue().length(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Extract a range of data from a Text or Comment node. DOM method. |
| 56 | * |
| 57 | * @param offset Start offset of substring to extract. |
| 58 | * @param count The number of 16-bit units to extract. |
| 59 | * @return The specified substring. If the sum of <code>offset</code> and |
| 60 | * <code>count</code> exceeds the <code>length</code> , then all 16-bit |
| 61 | * units to the end of the data are returned. |
| 62 | * @throws org.w3c.dom.DOMException INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is |
| 63 | * negative or greater than the number of 16-bit units in |
| 64 | * <code>data</code> , or if the specified <code>count</code> is |
| 65 | * negative. |
| 66 | */ |
| 67 | |
| 68 | public String substringData(int offset, int count) throws DOMException { |
| 69 | try { |
| 70 | return node.getStringValue().substring(offset, offset + count); |
| 71 | } catch (IndexOutOfBoundsException err2) { |
| 72 | throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR, |
| 73 | "substringData: index out of bounds"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Append the string to the end of the character data of the node. |
| 79 | * DOM method: always fails. |
| 80 | * |
| 81 | * @param arg The <code>DOMString</code> to append. |
| 82 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 83 | */ |
| 84 | |
| 85 | public void appendData(String arg) throws DOMException { |
| 86 | disallowUpdate(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Insert a string at the specified character offset. |
| 91 | * DOM method: always fails. |
| 92 | * |
| 93 | * @param offset The character offset at which to insert. |
| 94 | * @param arg The <code>DOMString</code> to insert. |
| 95 | * @throws org.w3c.dom.DOMException |
| 96 | */ |
| 97 | |
| 98 | public void insertData(int offset, String arg) throws DOMException { |
| 99 | disallowUpdate(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Remove a range of 16-bit units from the node. |
| 104 | * DOM method: always fails. |
| 105 | * |
| 106 | * @param offset The offset from which to start removing. |
| 107 | * @param count The number of 16-bit units to delete. |
| 108 | * @throws org.w3c.dom.DOMException |
| 109 | */ |
| 110 | |
| 111 | public void deleteData(int offset, int count) throws DOMException { |
| 112 | disallowUpdate(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Replace the characters starting at the specified 16-bit unit offset |
| 117 | * with the specified string. DOM method: always fails. |
| 118 | * |
| 119 | * @param offset The offset from which to start replacing. |
| 120 | * @param count The number of 16-bit units to replace. |
| 121 | * @param arg The <code>DOMString</code> with which the range must be |
| 122 | * replaced. |
| 123 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 124 | */ |
| 125 | |
| 126 | public void replaceData(int offset, |
| 127 | int count, |
| 128 | String arg) throws DOMException { |
| 129 | disallowUpdate(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Break this node into two nodes at the specified offset, |
| 135 | * keeping both in the tree as siblings. DOM method, always fails. |
| 136 | * |
| 137 | * @param offset The 16-bit unit offset at which to split, starting from 0. |
| 138 | * @return The new node, of the same type as this node. |
| 139 | * @throws org.w3c.dom.DOMException |
| 140 | */ |
| 141 | |
| 142 | public Text splitText(int offset) throws DOMException { |
| 143 | disallowUpdate(); |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Replaces the text of the current node and all logically-adjacent text |
| 149 | * nodes with the specified text. All logically-adjacent text nodes are |
| 150 | * removed including the current node unless it was the recipient of the |
| 151 | * replacement text. |
| 152 | * <br>This method returns the node which received the replacement text. |
| 153 | * The returned node is: |
| 154 | * <ul> |
| 155 | * <li><code>null</code>, when the replacement text is |
| 156 | * the empty string; |
| 157 | * </li> |
| 158 | * <li>the current node, except when the current node is |
| 159 | * read-only; |
| 160 | * </li> |
| 161 | * <li> a new <code>Text</code> node of the same type ( |
| 162 | * <code>Text</code> or <code>CDATASection</code>) as the current node |
| 163 | * inserted at the location of the replacement. |
| 164 | * </li> |
| 165 | * </ul> |
| 166 | * <br>For instance, in the above example calling |
| 167 | * <code>replaceWholeText</code> on the <code>Text</code> node that |
| 168 | * contains "bar" with "yo" in argument results in the following: |
| 169 | * <br>Where the nodes to be removed are read-only descendants of an |
| 170 | * <code>EntityReference</code>, the <code>EntityReference</code> must |
| 171 | * be removed instead of the read-only nodes. If any |
| 172 | * <code>EntityReference</code> to be removed has descendants that are |
| 173 | * not <code>EntityReference</code>, <code>Text</code>, or |
| 174 | * <code>CDATASection</code> nodes, the <code>replaceWholeText</code> |
| 175 | * method must fail before performing any modification of the document, |
| 176 | * raising a <code>DOMException</code> with the code |
| 177 | * <code>NO_MODIFICATION_ALLOWED_ERR</code>. |
| 178 | * <br>For instance, in the example below calling |
| 179 | * <code>replaceWholeText</code> on the <code>Text</code> node that |
| 180 | * contains "bar" fails, because the <code>EntityReference</code> node |
| 181 | * "ent" contains an <code>Element</code> node which cannot be removed. |
| 182 | * |
| 183 | * @param content The content of the replacing <code>Text</code> node. |
| 184 | * @return The <code>Text</code> node created with the specified content. |
| 185 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if one of the <code>Text</code> |
| 186 | * nodes being replaced is readonly. |
| 187 | * @since DOM Level 3 |
| 188 | */ |
| 189 | /*@Nullable*/ |
| 190 | public Text replaceWholeText(String content) throws DOMException { |
| 191 | disallowUpdate(); |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns whether this text node contains <a href='http://www.w3.org/TR/2004/REC-xml-infoset-20040204#infoitem.character'> |
| 197 | * element content whitespace</a>, often abusively called "ignorable whitespace". The text node is |
| 198 | * determined to contain whitespace in element content during the load |
| 199 | * of the document or if validation occurs while using |
| 200 | * <code>Document.normalizeDocument()</code>. |
| 201 | * |
| 202 | * @since DOM Level 3 |
| 203 | */ |
| 204 | public boolean isElementContentWhitespace() { |
| 205 | if (node.getNodeKind() != Type.TEXT) { |
| 206 | throw new UnsupportedOperationException("Method is defined only on text nodes"); |
| 207 | } |
| 208 | if (!Whitespace.isWhite(node.getStringValue())) { |
| 209 | return false; |
| 210 | } |
| 211 | NodeInfo parent = node.getParent(); |
| 212 | if (parent == null) { |
| 213 | return false; |
| 214 | } |
| 215 | SchemaType type = parent.getSchemaType(); |
| 216 | return type.isComplexType() && !((ComplexType) type).isMixedContent(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns all text of <code>Text</code> nodes logically-adjacent text |
| 221 | * nodes to this node, concatenated in document order. |
| 222 | * <br>For instance, in the example below <code>wholeText</code> on the |
| 223 | * <code>Text</code> node that contains "bar" returns "barfoo", while on |
| 224 | * the <code>Text</code> node that contains "foo" it returns "barfoo". |
| 225 | * |
| 226 | * @since DOM Level 3 |
| 227 | */ |
| 228 | public String getWholeText() { |
| 229 | if (node.getNodeKind() != Type.TEXT) { |
| 230 | throw new UnsupportedOperationException("Method is defined only on text nodes"); |
| 231 | } |
| 232 | return node.getStringValue(); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | } |
| 237 |