1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45:
46:
47:
55: public abstract class DomCharacterData
56: extends DomNode
57: implements CharacterData
58: {
59:
60:
63: static class EmptyNodeList
64: implements NodeList
65: {
66:
67: public int getLength()
68: {
69: return 0;
70: }
71:
72: public Node item(int index)
73: {
74: return null;
75: }
76:
77: }
78:
79:
82: static final NodeList CHILD_NODES = new EmptyNodeList();
83:
84: private String text;
85:
86:
87: DomCharacterData(short nodeType, DomDocument doc, String value)
88: {
89: super(nodeType, doc);
90: text = (value == null) ? "" : value;
91: }
92:
93:
94: DomCharacterData(short nodeType, DomDocument doc,
95: char[] buf, int offset, int length)
96: {
97: super(nodeType, doc);
98: text = (buf == null) ? "" : new String(buf, offset, length);
99: }
100:
101:
106: public void appendData(String arg)
107: {
108: if (isReadonly())
109: {
110: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
111: }
112: String value = text + arg;
113: mutating(value);
114: text = value;
115: }
116:
117:
122: public void deleteData(int offset, int count)
123: {
124: if (isReadonly())
125: {
126: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
127: }
128: char[] raw = text.toCharArray();
129: if (offset < 0 || count < 0 || offset > raw.length)
130: {
131: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
132: }
133: if ((offset + count) > raw.length)
134: {
135: count = raw.length - offset;
136: }
137: if (count == 0)
138: {
139: return;
140: }
141: try
142: {
143: char[] buf = new char[raw.length - count];
144: System.arraycopy(raw, 0, buf, 0, offset);
145: System.arraycopy(raw, offset + count, buf, offset,
146: raw.length - (offset + count));
147: String value = new String(buf);
148: mutating(value);
149: text = value;
150: }
151: catch (IndexOutOfBoundsException x)
152: {
153: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
154: }
155: }
156:
157:
161: public String getNodeValue()
162: {
163: return text;
164: }
165:
166:
170: public final String getData()
171: {
172: return text;
173: }
174:
175:
179: public int getLength()
180: {
181: return text.length();
182: }
183:
184:
188: public void insertData(int offset, String arg)
189: {
190: if (isReadonly())
191: {
192: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
193: }
194: char[] raw = text.toCharArray();
195: char[] tmp = arg.toCharArray ();
196: char[] buf = new char[raw.length + tmp.length];
197:
198: try
199: {
200: System.arraycopy(raw, 0, buf, 0, offset);
201: System.arraycopy(tmp, 0, buf, offset, tmp.length);
202: System.arraycopy(raw, offset, buf, offset + tmp.length,
203: raw.length - offset);
204: String value = new String(buf);
205: mutating(value);
206: text = value;
207: }
208: catch (IndexOutOfBoundsException x)
209: {
210: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
211: }
212: }
213:
214:
219: public void replaceData(int offset, int count, String arg)
220: {
221: if (readonly)
222: {
223: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
224: }
225: char[] raw = text.toCharArray();
226:
227:
228: if (offset < 0 || count < 0 || offset > raw.length)
229: {
230: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
231: }
232: if ((offset + count) > raw.length)
233: {
234: count = raw.length - offset;
235: }
236: try
237: {
238: char[] buf = new char[raw.length - count];
239: System.arraycopy(raw, 0, buf, 0, offset);
240: System.arraycopy(raw, offset + count, buf, offset,
241: raw.length - (offset + count));
242:
243:
244: char[] tmp = arg.toCharArray ();
245: char[] buf2 = new char[buf.length + tmp.length];
246: System.arraycopy(raw, 0, buf, 0, offset);
247: System.arraycopy(tmp, 0, buf, offset, tmp.length);
248: System.arraycopy(raw, offset, buf, offset + tmp.length,
249: raw.length - offset);
250: String value = new String(buf);
251: mutating(value);
252: text = value;
253: }
254: catch (IndexOutOfBoundsException x)
255: {
256: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
257: }
258: }
259:
260:
265: public void setNodeValue(String value)
266: {
267: if (isReadonly())
268: {
269: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
270: }
271: if (value == null)
272: {
273: value = "";
274: }
275: mutating(value);
276: text = value;
277: }
278:
279:
283: final public void setData(String data)
284: {
285: setNodeValue(data);
286: }
287:
288:
292: public String substringData(int offset, int count)
293: {
294: try
295: {
296: return text.substring(offset, count);
297: }
298: catch (StringIndexOutOfBoundsException e)
299: {
300: if (offset >= 0 && count >= 0 && offset < text.length())
301: {
302: return text.substring(offset);
303: }
304: throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
305: }
306: }
307:
308:
312: public NodeList getChildNodes()
313: {
314: return CHILD_NODES;
315: }
316:
317:
321: public final String getBaseURI()
322: {
323: return null;
324: }
325:
326: private void mutating(String newValue)
327: {
328: if (!reportMutations)
329: {
330: return;
331: }
332:
333:
334:
335: MutationEvent event;
336:
337: event = (MutationEvent) createEvent("MutationEvents");
338: event.initMutationEvent("DOMCharacterDataModified",
339: true , false ,
340: null, text, newValue, null, (short) 0);
341: dispatchEvent(event);
342: }
343:
344: }