1 /* DomCharacterData.java --
2 Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import org
.w3c
.dom
.CharacterData
;
41 import org
.w3c
.dom
.DOMException
;
42 import org
.w3c
.dom
.events
.MutationEvent
;
46 * <p> Abstract "CharacterData" implementation. This
47 * facilitates reusing code in classes implementing subtypes of that DOM
48 * interface (Text, Comment, CDATASection). </p>
50 * @author David Brownell
51 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
53 public abstract class DomCharacterData
55 implements CharacterData
61 DomCharacterData(short nodeType
, DomDocument doc
, String value
)
64 text
= (value
== null) ?
"" : value
;
68 DomCharacterData(short nodeType
, DomDocument doc
,
69 char[] buf
, int offset
, int length
)
72 text
= (buf
== null) ?
"" : new String(buf
, offset
, length
);
77 * Appends the specified data to the value of this node.
78 * Causes a DOMCharacterDataModified mutation event to be reported.
80 public void appendData(String arg
)
84 throw new DomDOMException(DOMException
.NO_MODIFICATION_ALLOWED_ERR
);
86 String value
= text
+ arg
;
93 * Modifies the value of this node.
94 * Causes a DOMCharacterDataModified mutation event to be reported.
96 public void deleteData(int offset
, int count
)
100 throw new DomDOMException(DOMException
.NO_MODIFICATION_ALLOWED_ERR
);
102 char[] raw
= text
.toCharArray();
103 if (offset
< 0 || count
< 0 || offset
> raw
.length
)
105 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
107 if ((offset
+ count
) > raw
.length
)
109 count
= raw
.length
- offset
;
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
);
125 catch (IndexOutOfBoundsException x
)
127 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
133 * Returns the value of this node.
135 public String
getNodeValue()
142 * Returns the value of this node; same as getNodeValue.
144 public final String
getData()
151 * Returns the length of the data.
153 public int getLength()
155 return text
.length();
160 * Modifies the value of this node.
162 public void insertData(int offset
, String arg
)
166 throw new DomDOMException(DOMException
.NO_MODIFICATION_ALLOWED_ERR
);
168 char[] raw
= text
.toCharArray();
169 char[] tmp
= arg
.toCharArray ();
170 char[] buf
= new char[raw
.length
+ tmp
.length
];
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
);
182 catch (IndexOutOfBoundsException x
)
184 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
190 * Modifies the value of this node. Causes DOMCharacterDataModified
191 * mutation events to be reported (at least one).
193 public void replaceData(int offset
, int count
, String arg
)
197 throw new DomDOMException(DOMException
.NO_MODIFICATION_ALLOWED_ERR
);
199 char[] raw
= text
.toCharArray();
202 if (offset
< 0 || count
< 0 || offset
> raw
.length
)
204 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
206 if ((offset
+ count
) > raw
.length
)
208 count
= raw
.length
- offset
;
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
));
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
);
228 catch (IndexOutOfBoundsException x
)
230 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
236 * Assigns the value of this node.
237 * Causes a DOMCharacterDataModified mutation event to be reported.
239 public void setNodeValue(String value
)
243 throw new DomDOMException(DOMException
.NO_MODIFICATION_ALLOWED_ERR
);
255 * Assigns the value of this node; same as setNodeValue.
257 final public void setData(String data
)
264 * Returns the specified substring.
266 public String
substringData(int offset
, int count
)
270 return text
.substring(offset
, count
);
272 catch (StringIndexOutOfBoundsException e
)
274 if (offset
>= 0 && count
>= 0 && offset
< text
.length())
276 return text
.substring(offset
);
278 throw new DomDOMException(DOMException
.INDEX_SIZE_ERR
);
283 * The base URI for character data is <code>null</code>.
284 * @since DOM Level 3 Core
286 public final String
getBaseURI()
291 private void mutating(String newValue
)
293 if (!reportMutations
)
298 // EVENT: DOMCharacterDataModified, target = this,
299 // prev/new values provided
302 event
= (MutationEvent
) createEvent("MutationEvents");
303 event
.initMutationEvent("DOMCharacterDataModified",
304 true /* bubbles */, false /* nocancel */,
305 null, text
, newValue
, null, (short) 0);
306 dispatchEvent(event
);