KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > dom > TextImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.dom;
59
60 import org.w3c.dom.CharacterData JavaDoc;
61 import org.w3c.dom.DOMException JavaDoc;
62 import org.w3c.dom.Node JavaDoc;
63 import org.w3c.dom.Text JavaDoc;
64
65 /**
66  * Text nodes hold the non-markup, non-Entity content of
67  * an Element or Attribute.
68  * <P>
69  * When a document is first made available to the DOM, there is only
70  * one Text object for each block of adjacent plain-text. Users (ie,
71  * applications) may create multiple adjacent Texts during editing --
72  * see {@link org.w3c.dom.Element#normalize} for discussion.
73  * <P>
74  * Note that CDATASection is a subclass of Text. This is conceptually
75  * valid, since they're really just two different ways of quoting
76  * characters when they're written out as part of an XML stream.
77  *
78  * @version $Id: TextImpl.java,v 1.22 2004/02/10 17:09:45 elena Exp $
79  * @since PR-DOM-Level-1-19980818.
80  */

81 public class TextImpl
82     extends CharacterDataImpl
83     implements CharacterData JavaDoc, Text JavaDoc {
84
85     //
86
// Constants
87
//
88

89     /** Serialization version. */
90     static final long serialVersionUID = -5294980852957403469L;
91         
92     //
93
// Constructors
94
//
95

96     /** Default constructor */
97     public TextImpl(){}
98
99     /** Factory constructor. */
100     public TextImpl(CoreDocumentImpl ownerDoc, String JavaDoc data) {
101         super(ownerDoc, data);
102     }
103     
104     /**
105      * NON-DOM: resets node and sets specified values for the current node
106      *
107      * @param ownerDoc
108      * @param data
109      */

110     public void setValues(CoreDocumentImpl ownerDoc, String JavaDoc data){
111
112         flags=0;
113         nextSibling = null;
114         previousSibling=null;
115         setOwnerDocument(ownerDoc);
116         super.data = data;
117     }
118     //
119
// Node methods
120
//
121

122     /**
123      * A short integer indicating what type of node this is. The named
124      * constants for this value are defined in the org.w3c.dom.Node interface.
125      */

126     public short getNodeType() {
127         return Node.TEXT_NODE;
128     }
129
130     /** Returns the node name. */
131     public String JavaDoc getNodeName() {
132         return "#text";
133     }
134
135     /**
136      * NON-DOM: Set whether this Text is ignorable whitespace.
137      */

138     public void setIgnorableWhitespace(boolean ignore) {
139
140         if (needsSyncData()) {
141             synchronizeData();
142         }
143         isIgnorableWhitespace(ignore);
144
145     } // setIgnorableWhitespace(boolean)
146

147
148     /**
149      * DOM L3 Core CR - Experimental
150      *
151      * Returns whether this text node contains
152      * element content whitespace</a>, often abusively called "ignorable whitespace".
153      * The text node is determined to contain whitespace in element content
154      * during the load of the document or if validation occurs while using
155      * <code>Document.normalizeDocument()</code>.
156      * @since DOM Level 3
157      */

158     public boolean isElementContentWhitespace() {
159         // REVISIT: is this implemenation correct?
160
if (needsSyncData()) {
161             synchronizeData();
162         }
163         return internalIsIgnorableWhitespace();
164     }
165
166
167     /**
168      * DOM Level 3 WD - Experimental.
169      * Returns all text of <code>Text</code> nodes logically-adjacent text
170      * nodes to this node, concatenated in document order.
171      * @since DOM Level 3
172      */

173     public String JavaDoc getWholeText(){
174         
175         if (needsSyncData()) {
176             synchronizeData();
177         }
178         if (nextSibling == null) {
179             return data;
180         }
181         if (fBufferStr == null){
182             fBufferStr = new StringBuffer JavaDoc();
183         }
184         else {
185             fBufferStr.setLength(0);
186         }
187         if (data != null && data.length() != 0) {
188             fBufferStr.append(data);
189         }
190         getWholeText(nextSibling, fBufferStr);
191         return fBufferStr.toString();
192     
193     }
194
195     /**
196      * Concatenates the text of all logically-adjacent text nodes
197      *
198      * @param node
199      * @param buffer
200      * @return true - if execution was stopped because the type of node
201      * other than EntityRef, Text, CDATA is encountered, otherwise
202      * return false
203      */

204     private boolean getWholeText(Node JavaDoc node, StringBuffer JavaDoc buffer){
205         String JavaDoc text;
206         while (node != null) {
207             short type = node.getNodeType();
208             if (type == Node.ENTITY_REFERENCE_NODE) {
209                 if (getWholeText(node.getFirstChild(), buffer)){
210                     return true;
211                 }
212             }
213             else if (type == Node.TEXT_NODE ||
214                      type == Node.CDATA_SECTION_NODE) {
215                 ((NodeImpl)node).getTextContent(buffer);
216             }
217             else {
218                 return true;
219             }
220
221             node = node.getNextSibling();
222         }
223         return false;
224     }
225
226     /**
227     * DOM Level 3 WD - Experimental.
228     */

229     public Text JavaDoc replaceWholeText(String JavaDoc content)
230                                  throws DOMException JavaDoc{
231
232         if (needsSyncData()) {
233             synchronizeData();
234         }
235
236         // make sure we can make the replacement
237
if (!canModify(nextSibling)) {
238             throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR,
239                 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
240         }
241
242         Node JavaDoc parent = this.getParentNode();
243         if (content == null || content.length() == 0) {
244             // remove current node
245
if (parent !=null) { // check if node in the tree
246
parent.removeChild(this);
247                 return null;
248             }
249         }
250         Text JavaDoc currentNode = null;
251         if (isReadOnly()){
252             Text JavaDoc newNode = this.ownerDocument().createTextNode(content);
253             if (parent !=null) { // check if node in the tree
254
parent.insertBefore(newNode, this);
255                 parent.removeChild(this);
256                 currentNode = newNode;
257             } else {
258                 return newNode;
259             }
260         } else {
261             this.setData(content);
262             currentNode = this;
263         }
264         Node JavaDoc sibling = currentNode.getNextSibling();
265         while ( sibling !=null) {
266             parent.removeChild(sibling);
267             sibling = currentNode.getNextSibling();
268         }
269
270         return currentNode;
271     }
272
273     /**
274      * If any EntityReference to be removed has descendants
275      * that are not EntityReference, Text, or CDATASection
276      * nodes, the replaceWholeText method must fail before
277      * performing any modification of the document, raising a
278      * DOMException with the code NO_MODIFICATION_ALLOWED_ERR.
279      *
280      * @param node
281      * @return true - can replace text
282      * false - can't replace exception must be raised
283      */

284     private boolean canModify(Node JavaDoc node){
285         while (node != null) {
286             short type = node.getNodeType();
287             if (type == Node.ENTITY_REFERENCE_NODE) {
288                 if (!canModify(node.getFirstChild())){
289                     return false;
290                 }
291             }
292             else if (type != Node.TEXT_NODE &&
293                      type != Node.CDATA_SECTION_NODE) {
294                 return false;
295             }
296
297             node = node.getNextSibling();
298         }
299         return true;
300     }
301
302     /**
303      * NON-DOM: Returns whether this Text is ignorable whitespace.
304      */

305     public boolean isIgnorableWhitespace() {
306
307         if (needsSyncData()) {
308             synchronizeData();
309         }
310         return internalIsIgnorableWhitespace();
311
312     } // isIgnorableWhitespace():boolean
313

314     
315     //
316
// Text methods
317
//
318

319     /**
320      * Break a text node into two sibling nodes. (Note that if the
321      * current node has no parent, they won't wind up as "siblings" --
322      * they'll both be orphans.)
323      *
324      * @param offset The offset at which to split. If offset is at the
325      * end of the available data, the second node will be empty.
326      *
327      * @return A reference to the new node (containing data after the
328      * offset point). The original node will contain data up to that
329      * point.
330      *
331      * @throws DOMException(INDEX_SIZE_ERR) if offset is <0 or >length.
332      *
333      * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is read-only.
334      */

335     public Text JavaDoc splitText(int offset)
336         throws DOMException JavaDoc {
337
338         if (isReadOnly()) {
339             throw new DOMException JavaDoc(
340             DOMException.NO_MODIFICATION_ALLOWED_ERR,
341                 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
342         }
343
344         if (needsSyncData()) {
345             synchronizeData();
346         }
347         if (offset < 0 || offset > data.length() ) {
348             throw new DOMException JavaDoc(DOMException.INDEX_SIZE_ERR,
349                 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null));
350         }
351             
352         // split text into two separate nodes
353
Text JavaDoc newText =
354             getOwnerDocument().createTextNode(data.substring(offset));
355         setNodeValue(data.substring(0, offset));
356
357         // insert new text node
358
Node JavaDoc parentNode = getParentNode();
359         if (parentNode != null) {
360             parentNode.insertBefore(newText, nextSibling);
361         }
362
363         return newText;
364
365     } // splitText(int):Text
366

367     
368     /**
369      * NON-DOM (used by DOMParser): Reset data for the node.
370      */

371     public void replaceData (String JavaDoc value){
372         data = value;
373     }
374
375
376     /**
377      * NON-DOM (used by DOMParser: Sets data to empty string.
378      * Returns the value the data was set to.
379      */

380     public String JavaDoc removeData (){
381         String JavaDoc olddata=data;
382         data = "";
383         return olddata;
384     }
385
386
387 } // class TextImpl
388
Popular Tags