KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > TextOverNodeInfo


1 package net.sf.saxon.dom;
2
3 import net.sf.saxon.type.ComplexType;
4 import net.sf.saxon.type.SchemaType;
5 import net.sf.saxon.type.Type;
6 import net.sf.saxon.value.Whitespace;
7 import org.w3c.dom.Comment JavaDoc;
8 import org.w3c.dom.DOMException JavaDoc;
9 import org.w3c.dom.Text JavaDoc;
10
11 /**
12  * This class is an implementation of the DOM Text and Comment interfaces that wraps a Saxon NodeInfo
13  * representation of a text or comment node.
14  */

15
16 public class TextOverNodeInfo extends NodeOverNodeInfo implements Text JavaDoc, Comment JavaDoc {
17
18
19     /**
20     * Get the character data of a Text or Comment node.
21     * DOM method.
22     */

23
24     public String JavaDoc getData() {
25         return node.getStringValue();
26     }
27
28     /**
29     * Set the character data of a Text or Comment node.
30     * DOM method: always fails, Saxon tree is immutable.
31     */

32
33     public void setData(String JavaDoc data) throws DOMException JavaDoc {
34         disallowUpdate();
35     }
36
37     /**
38     * Get the length of a Text or Comment node.
39     * DOM method.
40     */

41
42     public int getLength() {
43         return node.getStringValue().length();
44     }
45
46     /**
47      * Extract a range of data from a Text or Comment node. DOM method.
48      * @param offset Start offset of substring to extract.
49      * @param count The number of 16-bit units to extract.
50      * @return The specified substring. If the sum of <code>offset</code> and
51      * <code>count</code> exceeds the <code>length</code> , then all 16-bit
52      * units to the end of the data are returned.
53      * @exception org.w3c.dom.DOMException
54      * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
55      * negative or greater than the number of 16-bit units in
56      * <code>data</code> , or if the specified <code>count</code> is
57      * negative.
58      */

59
60     public String JavaDoc substringData(int offset, int count) throws DOMException JavaDoc {
61         try {
62             return node.getStringValue().substring(offset, offset+count);
63         } catch (IndexOutOfBoundsException JavaDoc err2) {
64             throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
65                              "substringData: index out of bounds");
66         }
67     }
68
69     /**
70      * Append the string to the end of the character data of the node.
71      * DOM method: always fails.
72      * @param arg The <code>DOMString</code> to append.
73      * @exception org.w3c.dom.DOMException
74      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
75      */

76
77     public void appendData(String JavaDoc arg) throws DOMException JavaDoc {
78         disallowUpdate();
79     }
80
81     /**
82      * Insert a string at the specified character offset.
83      * DOM method: always fails.
84      * @param offset The character offset at which to insert.
85      * @param arg The <code>DOMString</code> to insert.
86      * @exception org.w3c.dom.DOMException
87      */

88
89     public void insertData(int offset, String JavaDoc arg) throws DOMException JavaDoc {
90         disallowUpdate();
91     }
92
93     /**
94      * Remove a range of 16-bit units from the node.
95      * DOM method: always fails.
96      * @param offset The offset from which to start removing.
97      * @param count The number of 16-bit units to delete.
98      * @exception org.w3c.dom.DOMException
99      */

100
101     public void deleteData(int offset, int count) throws DOMException JavaDoc {
102         disallowUpdate();
103     }
104
105     /**
106      * Replace the characters starting at the specified 16-bit unit offset
107      * with the specified string. DOM method: always fails.
108      * @param offset The offset from which to start replacing.
109      * @param count The number of 16-bit units to replace.
110      * @param arg The <code>DOMString</code> with which the range must be
111      * replaced.
112      * @exception org.w3c.dom.DOMException
113      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
114      */

115
116     public void replaceData(int offset,
117                             int count,
118                             String JavaDoc arg) throws DOMException JavaDoc {
119         disallowUpdate();
120     }
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 JavaDoc splitText(int offset) throws DOMException JavaDoc {
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 JavaDoc replaceWholeText(String JavaDoc content) throws DOMException JavaDoc {
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 JavaDoc("Method is defined only on text nodes");
195         }
196         int annotation = node.getParent().getTypeAnnotation();
197         if (annotation == -1) {
198             return false;
199         }
200         if (!Whitespace.isWhite(node.getStringValue())) {
201             return false;
202         }
203         SchemaType type = node.getConfiguration().getSchemaType(annotation);
204         if (!type.isComplexType()) {
205             return false;
206         }
207         if (((ComplexType)type).isMixedContent()) {
208             return false;
209         }
210         return true;
211     }
212
213     /**
214      * Returns all text of <code>Text</code> nodes logically-adjacent text
215      * nodes to this node, concatenated in document order.
216      * <br>For instance, in the example below <code>wholeText</code> on the
217      * <code>Text</code> node that contains "bar" returns "barfoo", while on
218      * the <code>Text</code> node that contains "foo" it returns "barfoo".
219      *
220      * @since DOM Level 3
221      */

222     public String JavaDoc getWholeText() {
223         if (node.getNodeKind() != Type.TEXT) {
224             throw new UnsupportedOperationException JavaDoc("Method is defined only on text nodes");
225         }
226         return node.getStringValue();
227     }
228
229
230 }
231
232 //
233
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
234
// you may not use this file except in compliance with the License. You may obtain a copy of the
235
// License at http://www.mozilla.org/MPL/
236
//
237
// Software distributed under the License is distributed on an "AS IS" basis,
238
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
239
// See the License for the specific language governing rights and limitations under the License.
240
//
241
// The Original Code is: all this file.
242
//
243
// The Initial Developer of the Original Code is Michael H. Kay.
244
//
245
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
246
//
247
// Contributor(s): none.
248
//
Popular Tags