KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > DomCharacterData


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 /**
41  * Wrapper for the DOM node CharacterData.
42  *
43  * @version $Revision: 100 $
44  * @author David K. Taylor
45  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
46  */

47 public abstract class DomCharacterData extends DomNode {
48
49     /** the data string */
50     private String JavaDoc data_;
51
52     /**
53      * Create an instance of DomCharacterData
54      *
55      * @param page The HtmlPage that contains this element.
56      * @param data the data string wrapped by this node
57      */

58     public DomCharacterData(final HtmlPage page, final String JavaDoc data) {
59         super(page);
60         data_ = data;
61     }
62
63
64     /**
65      * Get the data character string for this character data.
66      * @return The data String.
67      */

68     public String JavaDoc getData() {
69         return data_;
70     }
71
72
73     /**
74      * Set the data character string to the new string.
75      * @param newValue The new String of data.
76      */

77     public void setData( final String JavaDoc newValue ) {
78         data_ = newValue;
79     }
80
81     /**
82      * Set the data character string to the new string.
83      * @param newValue The new String of data.
84      */

85     public void setNodeValue(String JavaDoc newValue) {
86         data_ = newValue;
87     }
88
89     /**
90      * Get the number of characters in the character data.
91      * @return The number of characters.
92      */

93     public int getLength() {
94         return data_.length();
95     }
96
97
98     /**
99      * Append a string to character data.
100      * @param newData The string to be appended to the character data.
101      */

102     public void appendData(final String JavaDoc newData) {
103         data_ += newData;
104     }
105
106
107     /**
108      * Delete characters from character data.
109      * @param offset The position of the first character to be deleted.
110      * @param count The number of characters to be deleted.
111      */

112     public void deleteData(final int offset, final int count) {
113         if(offset < 0 || count < 0) {
114             throw new IllegalArgumentException JavaDoc("offset: "+offset+" count: "+count);
115         }
116
117         final int tailLength = Math.max(data_.length() - count - offset, 0);
118         if(tailLength > 0) {
119             data_ = data_.substring(0, offset) + data_.substring(offset + count, offset + count + tailLength);
120         }
121         else {
122             data_ = "";
123         }
124     }
125
126
127     /**
128      * Insert a string into character data.
129      * @param offset The position within the first character at which
130      * the string is to be inserted.
131      * @param arg The string to insert.
132      */

133     public void insertData(final int offset, final String JavaDoc arg) {
134         data_ = new StringBuffer JavaDoc(data_).insert(offset, arg).toString();
135     }
136
137
138     /**
139      * Replace characters of character data with a string.
140      * @param offset The position within the first character at which
141      * the string is to be replaced.
142      * @param count The number of characters to be replaced.
143      * @param arg The string that replaces the count characters beginning at
144      * the character at offset.
145      */

146     public void replaceData(final int offset, final int count, final String JavaDoc arg) {
147         deleteData(offset, count);
148         insertData(offset, arg);
149     }
150
151
152     /**
153      * Extract a substring from character data.
154      * @param offset The position of the first character to be extracted.
155      * @param count The number of characters to be extracted.
156      * @return A string that consists of the count characters of the
157      * character data starting from the character at position offset.
158      */

159     public String JavaDoc substringData(final int offset, final int count) {
160         final int length = data_.length();
161         if (count < 0 || offset < 0 || offset > length - 1) {
162             throw new IllegalArgumentException JavaDoc("offset: "+offset+" count: "+count);
163         }
164
165         final int tailIndex = Math.min(offset + count, length);
166         return data_.substring(offset, tailIndex);
167     }
168
169     /**
170      * @return the string data held by this node
171      */

172     public String JavaDoc getNodeValue() {
173         return data_;
174     }
175 }
176
Popular Tags