1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.apache.harmony.xml.dom;
18 
19 import org.w3c.dom.CharacterData;
20 import org.w3c.dom.DOMException;
21 
22 /**
23  * Provides a straightforward implementation of the corresponding W3C DOM
24  * interface. The class is used internally only, thus only notable members that
25  * are not in the original interface are documented (the W3C docs are quite
26  * extensive). Hope that's ok.
27  * <p>
28  * Some of the fields may have package visibility, so other classes belonging to
29  * the DOM implementation can easily access them while maintaining the DOM tree
30  * structure.
31  */
32 public abstract class CharacterDataImpl extends LeafNodeImpl implements
33         CharacterData {
34 
35     protected StringBuffer buffer;
36 
CharacterDataImpl(DocumentImpl document, String data)37     CharacterDataImpl(DocumentImpl document, String data) {
38         super(document);
39         setData(data);
40     }
41 
appendData(String arg)42     public void appendData(String arg) throws DOMException {
43         buffer.append(arg);
44     }
45 
deleteData(int offset, int count)46     public void deleteData(int offset, int count) throws DOMException {
47         buffer.delete(offset, offset + count);
48     }
49 
getData()50     public String getData() throws DOMException {
51         return buffer.toString();
52     }
53 
54     /**
55      * Appends this node's text content to the given builder.
56      */
appendDataTo(StringBuilder stringBuilder)57     public void appendDataTo(StringBuilder stringBuilder) {
58         stringBuilder.append(buffer);
59     }
60 
getLength()61     public int getLength() {
62         return buffer.length();
63     }
64 
65     @Override
getNodeValue()66     public String getNodeValue() {
67         return getData();
68     }
69 
insertData(int offset, String arg)70     public void insertData(int offset, String arg) throws DOMException {
71         try {
72             buffer.insert(offset, arg);
73         } catch (ArrayIndexOutOfBoundsException ex) {
74             throw new DOMException(DOMException.INDEX_SIZE_ERR, null);
75         }
76     }
77 
replaceData(int offset, int count, String arg)78     public void replaceData(int offset, int count, String arg)
79             throws DOMException {
80         try {
81             buffer.replace(offset, offset + count, arg);
82         } catch (ArrayIndexOutOfBoundsException ex) {
83             throw new DOMException(DOMException.INDEX_SIZE_ERR, null);
84         }
85     }
86 
setData(String data)87     public void setData(String data) throws DOMException {
88         buffer = new StringBuffer(data);
89     }
90 
substringData(int offset, int count)91     public String substringData(int offset, int count) throws DOMException {
92         try {
93             return buffer.substring(offset, offset + count);
94         } catch (ArrayIndexOutOfBoundsException ex) {
95             throw new DOMException(DOMException.INDEX_SIZE_ERR, null);
96         }
97     }
98 
99 }
100