1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import org.w3c.dom.DOMException ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.NodeList ; 63 64 79 public abstract class CharacterDataImpl 80 extends ChildNode { 81 82 86 87 static final long serialVersionUID = 7931170150428474230L; 88 89 93 protected String data; 94 95 96 private static transient NodeList singletonNodeList = new NodeList () { 97 public Node item(int index) { return null; } 98 public int getLength() { return 0; } 99 }; 100 101 105 106 protected CharacterDataImpl(CoreDocumentImpl ownerDocument, String data) { 107 super(ownerDocument); 108 this.data = data; 109 } 110 111 115 116 public NodeList getChildNodes() { 117 return singletonNodeList; 118 } 119 120 123 public String getNodeValue() { 124 if (needsSyncData()) { 125 synchronizeData(); 126 } 127 return data; 128 } 129 130 137 protected void setNodeValueInternal(String value) { 138 if (isReadOnly()) 139 throw new DOMException ( 140 DOMException.NO_MODIFICATION_ALLOWED_ERR, 141 "DOM001 Modification not allowed"); 142 if (needsSyncData()) { 145 synchronizeData(); 146 } 147 148 String oldvalue = this.data; 150 151 CoreDocumentImpl ownerDocument = ownerDocument(); 152 153 ownerDocument.modifyingCharacterData(this); 155 156 this.data = value; 157 158 ownerDocument.modifiedCharacterData(this, oldvalue, value); 160 } 161 162 166 public void setNodeValue(String value) { 167 168 setNodeValueInternal(value); 169 170 ownerDocument().replacedText(this); 172 } 173 174 178 186 public String getData() { 187 if (needsSyncData()) { 188 synchronizeData(); 189 } 190 return data; 191 } 192 193 197 public int getLength() { 198 if (needsSyncData()) { 199 synchronizeData(); 200 } 201 return data.length(); 202 } 203 204 212 public void appendData(String data) { 213 214 if (isReadOnly()) { 215 throw new DOMException ( 216 DOMException.NO_MODIFICATION_ALLOWED_ERR, 217 "DOM001 Modification not allowed"); 218 } 219 220 if (needsSyncData()) { 221 synchronizeData(); 222 } 223 224 setNodeValue(this.data + data); 225 226 } 228 240 public void deleteData(int offset, int count) 241 throws DOMException { 242 243 if (isReadOnly()) { 244 throw new DOMException ( 245 DOMException.NO_MODIFICATION_ALLOWED_ERR, 246 "DOM001 Modification not allowed"); 247 } 248 249 if (count < 0) { 250 throw new DOMException (DOMException.INDEX_SIZE_ERR, 251 "DOM004 Index out of bounds"); 252 } 253 254 if (needsSyncData()) { 255 synchronizeData(); 256 } 257 int tailLength = Math.max(data.length() - count - offset, 0); 258 try { 259 String value = data.substring(0, offset) + 260 (tailLength > 0 261 ? data.substring(offset + count, offset + count + tailLength) 262 : ""); 263 264 setNodeValueInternal(value); 265 266 ownerDocument().deletedText(this, offset, count); 268 } 269 catch (StringIndexOutOfBoundsException e) { 270 throw new DOMException (DOMException.INDEX_SIZE_ERR, 271 "DOM004 Index out of bounds"); 272 } 273 274 } 276 285 public void insertData(int offset, String data) 286 throws DOMException { 287 288 if (isReadOnly()) { 289 throw new DOMException ( 290 DOMException.NO_MODIFICATION_ALLOWED_ERR, 291 "DOM001 Modification not allowed"); 292 } 293 294 if (needsSyncData()) { 295 synchronizeData(); 296 } 297 try { 298 String value = 299 new StringBuffer (this.data).insert(offset, data).toString(); 300 301 setNodeValueInternal(value); 302 303 ownerDocument().insertedText(this, offset, data.length()); 305 } 306 catch (StringIndexOutOfBoundsException e) { 307 throw new DOMException (DOMException.INDEX_SIZE_ERR, 308 "DOM004 Index out of bounds"); 309 } 310 311 } 313 337 public void replaceData(int offset, int count, String data) 338 throws DOMException { 339 340 deleteData(offset, count); 347 insertData(offset, data); 348 349 } 351 356 public void setData(String value) 357 throws DOMException { 358 setNodeValue(value); 359 } 360 361 381 public String substringData(int offset, int count) 382 throws DOMException { 383 384 if (needsSyncData()) { 385 synchronizeData(); 386 } 387 388 int length = data.length(); 389 if (count < 0 || offset < 0 || offset > length - 1) { 390 throw new DOMException (DOMException.INDEX_SIZE_ERR, 391 "DOM004 Index out of bounds"); 392 } 393 394 int tailIndex = Math.min(offset + count, length); 395 396 return data.substring(offset, tailIndex); 397 398 } 400 } | Popular Tags |