1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.DOMException ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 23 40 public abstract class CharacterDataImpl 41 extends ChildNode { 42 43 47 48 static final long serialVersionUID = 7931170150428474230L; 49 50 54 protected String data; 55 56 57 private static transient NodeList singletonNodeList = new NodeList () { 58 public Node item(int index) { return null; } 59 public int getLength() { return 0; } 60 }; 61 62 66 public CharacterDataImpl(){} 67 68 69 protected CharacterDataImpl(CoreDocumentImpl ownerDocument, String data) { 70 super(ownerDocument); 71 this.data = data; 72 } 73 74 78 79 public NodeList getChildNodes() { 80 return singletonNodeList; 81 } 82 83 86 public String getNodeValue() { 87 if (needsSyncData()) { 88 synchronizeData(); 89 } 90 return data; 91 } 92 93 96 protected void setNodeValueInternal (String value) { 97 setNodeValueInternal(value, false); 98 } 99 100 107 protected void setNodeValueInternal(String value, boolean replace) { 108 109 CoreDocumentImpl ownerDocument = ownerDocument(); 110 111 if (ownerDocument.errorChecking && isReadOnly()) { 112 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 113 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 114 } 115 116 if (needsSyncData()) { 119 synchronizeData(); 120 } 121 122 String oldvalue = this.data; 124 125 ownerDocument.modifyingCharacterData(this, replace); 127 128 this.data = value; 129 130 ownerDocument.modifiedCharacterData(this, oldvalue, value, replace); 132 } 133 134 138 public void setNodeValue(String value) { 139 140 setNodeValueInternal(value); 141 142 ownerDocument().replacedText(this); 144 } 145 146 150 158 public String getData() { 159 if (needsSyncData()) { 160 synchronizeData(); 161 } 162 return data; 163 } 164 165 169 public int getLength() { 170 if (needsSyncData()) { 171 synchronizeData(); 172 } 173 return data.length(); 174 } 175 176 184 public void appendData(String data) { 185 186 if (isReadOnly()) { 187 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 188 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 189 } 190 if (data == null) { 191 return; 192 } 193 if (needsSyncData()) { 194 synchronizeData(); 195 } 196 197 setNodeValue(this.data + data); 198 199 } 201 213 public void deleteData(int offset, int count) 214 throws DOMException { 215 216 internalDeleteData(offset, count, false); 217 } 219 220 225 void internalDeleteData (int offset, int count, boolean replace) 226 throws DOMException { 227 228 CoreDocumentImpl ownerDocument = ownerDocument(); 229 if (ownerDocument.errorChecking) { 230 if (isReadOnly()) { 231 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 232 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 233 } 234 235 if (count < 0) { 236 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 237 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 238 } 239 } 240 241 if (needsSyncData()) { 242 synchronizeData(); 243 } 244 int tailLength = Math.max(data.length() - count - offset, 0); 245 try { 246 String value = data.substring(0, offset) + 247 (tailLength > 0 ? data.substring(offset + count, offset + count + tailLength) : ""); 248 249 setNodeValueInternal(value, replace); 250 251 ownerDocument.deletedText(this, offset, count); 253 } 254 catch (StringIndexOutOfBoundsException e) { 255 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 256 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 257 } 258 259 } 261 270 public void insertData(int offset, String data) 271 throws DOMException { 272 273 internalInsertData(offset, data, false); 274 275 } 277 278 279 284 void internalInsertData (int offset, String data, boolean replace) 285 throws DOMException { 286 287 CoreDocumentImpl ownerDocument = ownerDocument(); 288 289 if (ownerDocument.errorChecking && isReadOnly()) { 290 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 291 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 292 } 293 294 if (needsSyncData()) { 295 synchronizeData(); 296 } 297 try { 298 String value = 299 new StringBuffer (this.data).insert(offset, data).toString(); 300 301 302 setNodeValueInternal(value, replace); 303 304 ownerDocument.insertedText(this, offset, data.length()); 306 } 307 catch (StringIndexOutOfBoundsException e) { 308 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 309 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 310 } 311 312 } 314 315 316 340 public void replaceData(int offset, int count, String data) 341 throws DOMException { 342 343 CoreDocumentImpl ownerDocument = ownerDocument(); 344 345 if (ownerDocument.errorChecking && isReadOnly()) { 352 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 353 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 354 } 355 356 if (needsSyncData()) { 357 synchronizeData(); 358 } 359 360 ownerDocument.replacingData(this); 362 363 String oldvalue = this.data; 365 366 internalDeleteData(offset, count, true); 367 internalInsertData(offset, data, true); 368 369 ownerDocument.replacedCharacterData(this, oldvalue, this.data); 370 371 } 373 378 public void setData(String value) 379 throws DOMException { 380 setNodeValue(value); 381 } 382 383 403 public String substringData(int offset, int count) 404 throws DOMException { 405 406 if (needsSyncData()) { 407 synchronizeData(); 408 } 409 410 int length = data.length(); 411 if (count < 0 || offset < 0 || offset > length - 1) { 412 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 413 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 414 } 415 416 int tailIndex = Math.min(offset + count, length); 417 418 return data.substring(offset, tailIndex); 419 420 } 422 } | Popular Tags |