001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.type.ComplexType;
004: import net.sf.saxon.type.SchemaType;
005: import net.sf.saxon.type.Type;
006: import net.sf.saxon.value.Whitespace;
007: import org.w3c.dom.Comment;
008: import org.w3c.dom.DOMException;
009: import org.w3c.dom.Text;
010:
011: /**
012: * This class is an implementation of the DOM Text and Comment interfaces that wraps a Saxon NodeInfo
013: * representation of a text or comment node.
014: */
015:
016: public class TextOverNodeInfo extends NodeOverNodeInfo implements Text,
017: Comment {
018:
019: /**
020: * Get the character data of a Text or Comment node.
021: * DOM method.
022: */
023:
024: public String getData() {
025: return node.getStringValue();
026: }
027:
028: /**
029: * Set the character data of a Text or Comment node.
030: * DOM method: always fails, Saxon tree is immutable.
031: */
032:
033: public void setData(String data) throws DOMException {
034: disallowUpdate();
035: }
036:
037: /**
038: * Get the length of a Text or Comment node.
039: * DOM method.
040: */
041:
042: public int getLength() {
043: return node.getStringValue().length();
044: }
045:
046: /**
047: * Extract a range of data from a Text or Comment node. DOM method.
048: * @param offset Start offset of substring to extract.
049: * @param count The number of 16-bit units to extract.
050: * @return The specified substring. If the sum of <code>offset</code> and
051: * <code>count</code> exceeds the <code>length</code> , then all 16-bit
052: * units to the end of the data are returned.
053: * @exception org.w3c.dom.DOMException
054: * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
055: * negative or greater than the number of 16-bit units in
056: * <code>data</code> , or if the specified <code>count</code> is
057: * negative.
058: */
059:
060: public String substringData(int offset, int count)
061: throws DOMException {
062: try {
063: return node.getStringValue().substring(offset,
064: offset + count);
065: } catch (IndexOutOfBoundsException err2) {
066: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
067: "substringData: index out of bounds");
068: }
069: }
070:
071: /**
072: * Append the string to the end of the character data of the node.
073: * DOM method: always fails.
074: * @param arg The <code>DOMString</code> to append.
075: * @exception org.w3c.dom.DOMException
076: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
077: */
078:
079: public void appendData(String arg) throws DOMException {
080: disallowUpdate();
081: }
082:
083: /**
084: * Insert a string at the specified character offset.
085: * DOM method: always fails.
086: * @param offset The character offset at which to insert.
087: * @param arg The <code>DOMString</code> to insert.
088: * @exception org.w3c.dom.DOMException
089: */
090:
091: public void insertData(int offset, String arg) throws DOMException {
092: disallowUpdate();
093: }
094:
095: /**
096: * Remove a range of 16-bit units from the node.
097: * DOM method: always fails.
098: * @param offset The offset from which to start removing.
099: * @param count The number of 16-bit units to delete.
100: * @exception org.w3c.dom.DOMException
101: */
102:
103: public void deleteData(int offset, int count) throws DOMException {
104: disallowUpdate();
105: }
106:
107: /**
108: * Replace the characters starting at the specified 16-bit unit offset
109: * with the specified string. DOM method: always fails.
110: * @param offset The offset from which to start replacing.
111: * @param count The number of 16-bit units to replace.
112: * @param arg The <code>DOMString</code> with which the range must be
113: * replaced.
114: * @exception org.w3c.dom.DOMException
115: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
116: */
117:
118: public void replaceData(int offset, int count, String arg)
119: throws DOMException {
120: disallowUpdate();
121: }
122:
123: /**
124: * Break this node into two nodes at the specified offset,
125: * keeping both in the tree as siblings. DOM method, always fails.
126: * @param offset The 16-bit unit offset at which to split, starting from 0.
127: * @return The new node, of the same type as this node.
128: * @exception org.w3c.dom.DOMException
129: */
130:
131: public Text splitText(int offset) throws DOMException {
132: disallowUpdate();
133: return null;
134: }
135:
136: /**
137: * Replaces the text of the current node and all logically-adjacent text
138: * nodes with the specified text. All logically-adjacent text nodes are
139: * removed including the current node unless it was the recipient of the
140: * replacement text.
141: * <br>This method returns the node which received the replacement text.
142: * The returned node is:
143: * <ul>
144: * <li><code>null</code>, when the replacement text is
145: * the empty string;
146: * </li>
147: * <li>the current node, except when the current node is
148: * read-only;
149: * </li>
150: * <li> a new <code>Text</code> node of the same type (
151: * <code>Text</code> or <code>CDATASection</code>) as the current node
152: * inserted at the location of the replacement.
153: * </li>
154: * </ul>
155: * <br>For instance, in the above example calling
156: * <code>replaceWholeText</code> on the <code>Text</code> node that
157: * contains "bar" with "yo" in argument results in the following:
158: * <br>Where the nodes to be removed are read-only descendants of an
159: * <code>EntityReference</code>, the <code>EntityReference</code> must
160: * be removed instead of the read-only nodes. If any
161: * <code>EntityReference</code> to be removed has descendants that are
162: * not <code>EntityReference</code>, <code>Text</code>, or
163: * <code>CDATASection</code> nodes, the <code>replaceWholeText</code>
164: * method must fail before performing any modification of the document,
165: * raising a <code>DOMException</code> with the code
166: * <code>NO_MODIFICATION_ALLOWED_ERR</code>.
167: * <br>For instance, in the example below calling
168: * <code>replaceWholeText</code> on the <code>Text</code> node that
169: * contains "bar" fails, because the <code>EntityReference</code> node
170: * "ent" contains an <code>Element</code> node which cannot be removed.
171: *
172: * @param content The content of the replacing <code>Text</code> node.
173: * @return The <code>Text</code> node created with the specified content.
174: * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if one of the <code>Text</code>
175: * nodes being replaced is readonly.
176: * @since DOM Level 3
177: */
178: public Text replaceWholeText(String content) throws DOMException {
179: disallowUpdate();
180: return null;
181: }
182:
183: /**
184: * Returns whether this text node contains <a href='http://www.w3.org/TR/2004/REC-xml-infoset-20040204#infoitem.character'>
185: * element content whitespace</a>, often abusively called "ignorable whitespace". The text node is
186: * determined to contain whitespace in element content during the load
187: * of the document or if validation occurs while using
188: * <code>Document.normalizeDocument()</code>.
189: *
190: * @since DOM Level 3
191: */
192: public boolean isElementContentWhitespace() {
193: if (node.getNodeKind() != Type.TEXT) {
194: throw new UnsupportedOperationException(
195: "Method is defined only on text nodes");
196: }
197: int annotation = node.getParent().getTypeAnnotation();
198: if (annotation == -1) {
199: return false;
200: }
201: if (!Whitespace.isWhite(node.getStringValue())) {
202: return false;
203: }
204: SchemaType type = node.getConfiguration().getSchemaType(
205: annotation);
206: if (!type.isComplexType()) {
207: return false;
208: }
209: if (((ComplexType) type).isMixedContent()) {
210: return false;
211: }
212: return true;
213: }
214:
215: /**
216: * Returns all text of <code>Text</code> nodes logically-adjacent text
217: * nodes to this node, concatenated in document order.
218: * <br>For instance, in the example below <code>wholeText</code> on the
219: * <code>Text</code> node that contains "bar" returns "barfoo", while on
220: * the <code>Text</code> node that contains "foo" it returns "barfoo".
221: *
222: * @since DOM Level 3
223: */
224: public String getWholeText() {
225: if (node.getNodeKind() != Type.TEXT) {
226: throw new UnsupportedOperationException(
227: "Method is defined only on text nodes");
228: }
229: return node.getStringValue();
230: }
231:
232: }
233:
234: //
235: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
236: // you may not use this file except in compliance with the License. You may obtain a copy of the
237: // License at http://www.mozilla.org/MPL/
238: //
239: // Software distributed under the License is distributed on an "AS IS" basis,
240: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
241: // See the License for the specific language governing rights and limitations under the License.
242: //
243: // The Original Code is: all this file.
244: //
245: // The Initial Developer of the Original Code is Michael H. Kay.
246: //
247: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
248: //
249: // Contributor(s): none.
250: //
|