KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > Text


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.nodes;
21 import java.util.List JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23 import org.netbeans.modules.xml.xdm.visitor.XMLNodeVisitor;
24
25 /**
26  *
27  * @author Ajit
28  */

29 public class Text extends NodeImpl implements Node, org.w3c.dom.Text JavaDoc {
30     
31     public void accept(XMLNodeVisitor visitor) {
32         visitor.visit(this);
33     }
34     
35     Text() {
36         super();
37     }
38
39     Text(String JavaDoc text) {
40         this();
41         setText(text);
42     }
43
44     @Override JavaDoc
45     public String JavaDoc getNodeValue() {
46         return getText();
47     }
48
49     public short getNodeType() {
50         return Node.TEXT_NODE;
51     }
52
53     public String JavaDoc getNodeName() {
54         return "#text"; //NOI18N
55
}
56
57     @Override JavaDoc
58     public String JavaDoc getNamespaceURI() {
59         return null;
60     }
61
62     public String JavaDoc getText() {
63         StringBuilder JavaDoc txtBuf = new StringBuilder JavaDoc();
64         for (Token token: getTokens()) {
65             if(token.getType()==TokenType.TOKEN_CHARACTER_DATA)
66                 txtBuf.append(token.getValue());
67         }
68         return removeEntityReference(txtBuf.toString());
69     }
70     
71     public void setText(String JavaDoc text) {
72         checkNotInTree();
73         List JavaDoc<Token> tokens = getTokensForWrite();
74         if(!tokens.isEmpty())tokens.clear();
75         // no null text allowed
76
if(text==null) text = "";
77         text = insertEntityReference(text);
78         tokens.add(Token.create(text, TokenType.TOKEN_CHARACTER_DATA));
79     }
80     
81     /**
82      * Returns whether this text node contains <a HREF='http://www.w3.org/TR/2004/REC-xml-infoset-20040204#infoitem.character'>
83      * element content whitespace</a>, often abusively called "ignorable whitespace". The text node is
84      * determined to contain whitespace in element content during the load
85      * of the document or if validation occurs while using
86      * <code>Document.normalizeDocument()</code>.
87      * @since DOM Level 3
88      */

89     public boolean isElementContentWhitespace() {
90         //TODO Implement later
91
return false;
92     }
93
94     /**
95      * The number of 16-bit units that are available through <code>data</code>
96      * and the <code>substringData</code> method below. This may have the
97      * value zero, i.e., <code>CharacterData</code> nodes may be empty.
98      */

99     public int getLength() {
100         //TODO Implement later
101
return -1;
102     }
103
104     /**
105      * Replaces the text of the current node and all logically-adjacent text
106      * nodes with the specified text. All logically-adjacent text nodes are
107      * removed including the current node unless it was the recipient of the
108      * replacement text.
109      * <br>This method returns the node which received the replacement text.
110      * The returned node is:
111      * <ul>
112      * <li><code>null</code>, when the replacement text is
113      * the empty string;
114      * </li>
115      * <li>the current node, except when the current node is
116      * read-only;
117      * </li>
118      * <li> a new <code>Text</code> node of the same type (
119      * <code>Text</code> or <code>CDATASection</code>) as the current node
120      * inserted at the location of the replacement.
121      * </li>
122      * </ul>
123      * <br>For instance, in the above example calling
124      * <code>replaceWholeText</code> on the <code>Text</code> node that
125      * contains "bar" with "yo" in argument results in the following:
126      * <br>Where the nodes to be removed are read-only descendants of an
127      * <code>EntityReference</code>, the <code>EntityReference</code> must
128      * be removed instead of the read-only nodes. If any
129      * <code>EntityReference</code> to be removed has descendants that are
130      * not <code>EntityReference</code>, <code>Text</code>, or
131      * <code>CDATASection</code> nodes, the <code>replaceWholeText</code>
132      * method must fail before performing any modification of the document,
133      * raising a <code>DOMException</code> with the code
134      * <code>NO_MODIFICATION_ALLOWED_ERR</code>.
135      * <br>For instance, in the example below calling
136      * <code>replaceWholeText</code> on the <code>Text</code> node that
137      * contains "bar" fails, because the <code>EntityReference</code> node
138      * "ent" contains an <code>Element</code> node which cannot be removed.
139      * @param content The content of the replacing <code>Text</code> node.
140      * @return The <code>Text</code> node created with the specified content.
141      * @exception DOMException
142      * NO_MODIFICATION_ALLOWED_ERR: Raised if one of the <code>Text</code>
143      * nodes being replaced is readonly.
144      * @since DOM Level 3
145      */

146     public org.w3c.dom.Text JavaDoc replaceWholeText(String JavaDoc content) {
147         //TODO Implement later
148
return null;
149     }
150
151     /**
152      * Breaks this node into two nodes at the specified <code>offset</code>,
153      * keeping both in the tree as siblings. After being split, this node
154      * will contain all the content up to the <code>offset</code> point. A
155      * new node of the same type, which contains all the content at and
156      * after the <code>offset</code> point, is returned. If the original
157      * node had a parent node, the new node is inserted as the next sibling
158      * of the original node. When the <code>offset</code> is equal to the
159      * length of this node, the new node has no data.
160      * @param offset The 16-bit unit offset at which to split, starting from
161      * <code>0</code>.
162      * @return The new node, of the same type as this node.
163      * @exception DOMException
164      * INDEX_SIZE_ERR: Raised if the specified offset is negative or greater
165      * than the number of 16-bit units in <code>data</code>.
166      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
167      */

168     public org.w3c.dom.Text JavaDoc splitText(int offset) {
169         //TODO Implement later
170
return null;
171     }
172
173     /**
174      * Returns all text of <code>Text</code> nodes logically-adjacent text
175      * nodes to this node, concatenated in document order.
176      * <br>For instance, in the example below <code>wholeText</code> on the
177      * <code>Text</code> node that contains "bar" returns "barfoo", while on
178      * the <code>Text</code> node that contains "foo" it returns "barfoo".
179      * @since DOM Level 3
180      */

181     public String JavaDoc getWholeText() {
182         //TODO Implement later
183
return null;
184     }
185
186     /**
187      * The character data of the node that implements this interface. The DOM
188      * implementation may not put arbitrary limits on the amount of data
189      * that may be stored in a <code>CharacterData</code> node. However,
190      * implementation limits may mean that the entirety of a node's data may
191      * not fit into a single <code>DOMString</code>. In such cases, the user
192      * may call <code>substringData</code> to retrieve the data in
193      * appropriately sized pieces.
194      * @exception DOMException
195      * NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
196      */

197     public void setData(String JavaDoc text) {
198         checkNotInTree();
199         List JavaDoc<Token> tokens = getTokensForWrite();
200         if(!tokens.isEmpty())tokens.clear();
201         // no null text allowed
202
if(text==null) text = "";
203         tokens.add(Token.create(text, TokenType.TOKEN_CHARACTER_DATA));
204     }
205
206     /**
207      * Append the string to the end of the character data of the node. Upon
208      * success, <code>data</code> provides access to the concatenation of
209      * <code>data</code> and the <code>DOMString</code> specified.
210      * @param arg The <code>DOMString</code> to append.
211      * @exception DOMException
212      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
213      */

214     public void appendData(String JavaDoc arg) {
215         //TODO Implement later
216
}
217
218     /**
219      * Replace the characters starting at the specified 16-bit unit offset
220      * with the specified string.
221      * @param offset The offset from which to start replacing.
222      * @param count The number of 16-bit units to replace. If the sum of
223      * <code>offset</code> and <code>count</code> exceeds
224      * <code>length</code>, then all 16-bit units to the end of the data
225      * are replaced; (i.e., the effect is the same as a <code>remove</code>
226      * method call with the same range, followed by an <code>append</code>
227      * method invocation).
228      * @param arg The <code>DOMString</code> with which the range must be
229      * replaced.
230      * @exception DOMException
231      * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
232      * negative or greater than the number of 16-bit units in
233      * <code>data</code>, or if the specified <code>count</code> is
234      * negative.
235      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
236      */

237     public void replaceData(int offset, int count, String JavaDoc arg) {
238         //TODO Implement later
239
}
240
241     /**
242      * Insert a string at the specified 16-bit unit offset.
243      * @param offset The character offset at which to insert.
244      * @param arg The <code>DOMString</code> to insert.
245      * @exception DOMException
246      * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
247      * negative or greater than the number of 16-bit units in
248      * <code>data</code>.
249      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
250      */

251     public void insertData(int offset, String JavaDoc arg) {
252         //TODO Implement later
253
}
254
255     /**
256      * Extracts a range of data from the node.
257      * @param offset Start offset of substring to extract.
258      * @param count The number of 16-bit units to extract.
259      * @return The specified substring. If the sum of <code>offset</code> and
260      * <code>count</code> exceeds the <code>length</code>, then all 16-bit
261      * units to the end of the data are returned.
262      * @exception DOMException
263      * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
264      * negative or greater than the number of 16-bit units in
265      * <code>data</code>, or if the specified <code>count</code> is
266      * negative.
267      * <br>DOMSTRING_SIZE_ERR: Raised if the specified range of text does
268      * not fit into a <code>DOMString</code>.
269      */

270     public String JavaDoc substringData(int offset, int count) {
271         //TODO Implement later
272
return null;
273     }
274
275     /**
276      * The character data of the node that implements this interface. The DOM
277      * implementation may not put arbitrary limits on the amount of data
278      * that may be stored in a <code>CharacterData</code> node. However,
279      * implementation limits may mean that the entirety of a node's data may
280      * not fit into a single <code>DOMString</code>. In such cases, the user
281      * may call <code>substringData</code> to retrieve the data in
282      * appropriately sized pieces.
283      * @exception DOMException
284      * DOMSTRING_SIZE_ERR: Raised when it would return more characters than
285      * fit in a <code>DOMString</code> variable on the implementation
286      * platform.
287      */

288     public String JavaDoc getData() {
289         StringBuilder JavaDoc txtBuf = new StringBuilder JavaDoc();
290         for (Token token: getTokens()) {
291             if(token.getType()==TokenType.TOKEN_CHARACTER_DATA)
292                 txtBuf.append(token.getValue());
293         }
294         return txtBuf.toString();
295     }
296
297     /**
298      * Remove a range of 16-bit units from the node. Upon success,
299      * <code>data</code> and <code>length</code> reflect the change.
300      * @param offset The offset from which to start removing.
301      * @param count The number of 16-bit units to delete. If the sum of
302      * <code>offset</code> and <code>count</code> exceeds
303      * <code>length</code> then all 16-bit units from <code>offset</code>
304      * to the end of the data are deleted.
305      * @exception DOMException
306      * INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
307      * negative or greater than the number of 16-bit units in
308      * <code>data</code>, or if the specified <code>count</code> is
309      * negative.
310      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
311      */

312     public void deleteData(int offset, int count) {
313         //TODO Implement later
314
}
315     
316     private String JavaDoc insertEntityReference(String JavaDoc text) {
317         // just make sure we replace & with &amp; and not &amp; with &&amp;amp; and so on
318
String JavaDoc result = removeEntityReference(text);
319         result = result.replaceAll("&","&amp;"); //replace &
320
result = result.replaceAll("<","&lt;"); //replace <
321
result = result.replaceAll(">","&gt;"); //replace >
322
// result = result.replaceAll("'","&apos;"); //replace '
323
// result = result.replaceAll("\"","&quot;"); //replace "
324
return result;
325     }
326
327     private String JavaDoc removeEntityReference(String JavaDoc text) {
328         String JavaDoc result = text;
329         result = AMPERSAND_PATTERN.matcher(result).replaceAll("&"); //replace with &
330
result = LESS_THAN_PATTERN.matcher(result).replaceAll("<"); //replace with <
331
result = GREATER_THAN_PATTERN.matcher(result).replaceAll(">"); //replace with >
332
// result = result.replaceAll("&apos;","'"); //replace with '
333
// result = result.replaceAll("&quot;","\""); //replace with "
334
return result;
335     }
336
337     private static final Pattern JavaDoc AMPERSAND_PATTERN = Pattern.compile("&amp;"); //NOI18N
338
private static final Pattern JavaDoc LESS_THAN_PATTERN = Pattern.compile("&lt;"); //NOI18N
339
private static final Pattern JavaDoc GREATER_THAN_PATTERN = Pattern.compile("&gt;"); //NOI18N
340
}
341
Popular Tags