001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import com.gargoylesoftware.htmlunit.Page;
041:
042: /**
043: * Wrapper for the DOM node CharacterData.
044: *
045: * @version $Revision: 2132 $
046: * @author David K. Taylor
047: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
048: * @author Ahmed Ashour
049: */
050: public abstract class DomCharacterData extends DomNode {
051:
052: /** the data string */
053: private String data_;
054:
055: /**
056: * Create an instance of DomCharacterData
057: *
058: * @param page The Page that contains this element.
059: * @param data the data string wrapped by this node
060: */
061: public DomCharacterData(final Page page, final String data) {
062: super (page);
063: data_ = data;
064: }
065:
066: /**
067: * Get the data character string for this character data.
068: * @return The data String.
069: */
070: public String getData() {
071: return data_;
072: }
073:
074: /**
075: * Set the data character string to the new string.
076: * @param newValue The new String of data.
077: */
078: public void setData(final String newValue) {
079: data_ = newValue;
080: }
081:
082: /**
083: * Set the data character string to the new string.
084: * @param newValue The new String of data.
085: */
086: public void setNodeValue(final String newValue) {
087: data_ = newValue;
088: }
089:
090: /**
091: * Get the number of characters in the character data.
092: * @return The number of characters.
093: */
094: public int getLength() {
095: return data_.length();
096: }
097:
098: /**
099: * 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 newData) {
103: data_ += newData;
104: }
105:
106: /**
107: * Delete characters from character data.
108: * @param offset The position of the first character to be deleted.
109: * @param count The number of characters to be deleted.
110: */
111: public void deleteData(final int offset, final int count) {
112: if (offset < 0 || count < 0) {
113: throw new IllegalArgumentException("offset: " + offset
114: + " count: " + count);
115: }
116:
117: final int tailLength = Math.max(
118: data_.length() - count - offset, 0);
119: if (tailLength > 0) {
120: data_ = data_.substring(0, offset)
121: + data_.substring(offset + count, offset + count
122: + tailLength);
123: } else {
124: data_ = "";
125: }
126: }
127:
128: /**
129: * Insert a string into character data.
130: * @param offset The position within the first character at which
131: * the string is to be inserted.
132: * @param arg The string to insert.
133: */
134: public void insertData(final int offset, final String arg) {
135: data_ = new StringBuffer(data_).insert(offset, arg).toString();
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,
147: final String arg) {
148: deleteData(offset, count);
149: insertData(offset, arg);
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 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("offset: " + offset
163: + " count: " + count);
164: }
165:
166: final int tailIndex = Math.min(offset + count, length);
167: return data_.substring(offset, tailIndex);
168: }
169:
170: /**
171: * @return the string data held by this node
172: */
173: public String getNodeValue() {
174: return data_;
175: }
176: }
|