1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44:
45:
53: public abstract class DomCharacterData
54: extends DomNode
55: implements CharacterData
56: {
57:
58: private String text;
59:
60:
61: DomCharacterData(short nodeType, DomDocument doc, String value)
62: {
63: super(nodeType, doc);
64: text = (value == null) ? "" : value;
65: }
66:
67:
68: DomCharacterData(short nodeType, DomDocument doc,
69: char[] buf, int offset, int length)
70: {
71: super(nodeType, doc);
72: text = (buf == null) ? "" : new String(buf, offset, length);
73: }
74:
75:
80: public void appendData(String arg)
81: {
82: if (isReadonly())
83: {
84: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
85: }
86: String value = text + arg;
87: mutating(value);
88: text = value;
89: }
90:
91:
96: public void deleteData(int offset, int count)
97: {
98: if (isReadonly())
99: {
100: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
101: }
102: char[] raw = text.toCharArray();
103: if (offset < 0 || count < 0 || offset > raw.length)
104: {
105: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
106: }
107: if ((offset + count) > raw.length)
108: {
109: count = raw.length - offset;
110: }
111: if (count == 0)
112: {
113: return;
114: }
115: try
116: {
117: char[] buf = new char[raw.length - count];
118: System.arraycopy(raw, 0, buf, 0, offset);
119: System.arraycopy(raw, offset + count, buf, offset,
120: raw.length - (offset + count));
121: String value = new String(buf);
122: mutating(value);
123: text = value;
124: }
125: catch (IndexOutOfBoundsException x)
126: {
127: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
128: }
129: }
130:
131:
135: public String getNodeValue()
136: {
137: return text;
138: }
139:
140:
144: public final String getData()
145: {
146: return text;
147: }
148:
149:
153: public int getLength()
154: {
155: return text.length();
156: }
157:
158:
162: public void insertData(int offset, String arg)
163: {
164: if (isReadonly())
165: {
166: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
167: }
168: char[] raw = text.toCharArray();
169: char[] tmp = arg.toCharArray ();
170: char[] buf = new char[raw.length + tmp.length];
171:
172: try
173: {
174: System.arraycopy(raw, 0, buf, 0, offset);
175: System.arraycopy(tmp, 0, buf, offset, tmp.length);
176: System.arraycopy(raw, offset, buf, offset + tmp.length,
177: raw.length - offset);
178: String value = new String(buf);
179: mutating(value);
180: text = value;
181: }
182: catch (IndexOutOfBoundsException x)
183: {
184: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
185: }
186: }
187:
188:
193: public void replaceData(int offset, int count, String arg)
194: {
195: if (readonly)
196: {
197: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
198: }
199: char[] raw = text.toCharArray();
200:
201:
202: if (offset < 0 || count < 0 || offset > raw.length)
203: {
204: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
205: }
206: if ((offset + count) > raw.length)
207: {
208: count = raw.length - offset;
209: }
210: try
211: {
212: char[] buf = new char[raw.length - count];
213: System.arraycopy(raw, 0, buf, 0, offset);
214: System.arraycopy(raw, offset + count, buf, offset,
215: raw.length - (offset + count));
216:
217:
218: char[] tmp = arg.toCharArray ();
219: char[] buf2 = new char[buf.length + tmp.length];
220: System.arraycopy(raw, 0, buf, 0, offset);
221: System.arraycopy(tmp, 0, buf, offset, tmp.length);
222: System.arraycopy(raw, offset, buf, offset + tmp.length,
223: raw.length - offset);
224: String value = new String(buf);
225: mutating(value);
226: text = value;
227: }
228: catch (IndexOutOfBoundsException x)
229: {
230: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
231: }
232: }
233:
234:
239: public void setNodeValue(String value)
240: {
241: if (isReadonly())
242: {
243: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
244: }
245: if (value == null)
246: {
247: value = "";
248: }
249: mutating(value);
250: text = value;
251: }
252:
253:
257: final public void setData(String data)
258: {
259: setNodeValue(data);
260: }
261:
262:
266: public String substringData(int offset, int count)
267: {
268: try
269: {
270: return text.substring(offset, count);
271: }
272: catch (StringIndexOutOfBoundsException e)
273: {
274: if (offset >= 0 && count >= 0 && offset < text.length())
275: {
276: return text.substring(offset);
277: }
278: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
279: }
280: }
281:
282:
286: public final String getBaseURI()
287: {
288: return null;
289: }
290:
291: private void mutating(String newValue)
292: {
293: if (!reportMutations)
294: {
295: return;
296: }
297:
298:
299:
300: MutationEvent event;
301:
302: event = (MutationEvent) createEvent("MutationEvents");
303: event.initMutationEvent("DOMCharacterDataModified",
304: true , false ,
305: null, text, newValue, null, (short) 0);
306: dispatchEvent(event);
307: }
308:
309: }