1 57 58 package com.sun.org.apache.xerces.internal.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 public CharacterDataImpl(){} 106 107 108 protected CharacterDataImpl(CoreDocumentImpl ownerDocument, String data) { 109 super(ownerDocument); 110 this.data = data; 111 } 112 113 117 118 public NodeList getChildNodes() { 119 return singletonNodeList; 120 } 121 122 125 public String getNodeValue() { 126 if (needsSyncData()) { 127 synchronizeData(); 128 } 129 return data; 130 } 131 132 139 protected void setNodeValueInternal(String value) { 140 if (isReadOnly()) { 141 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 142 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 143 } 144 if (needsSyncData()) { 147 synchronizeData(); 148 } 149 150 String oldvalue = this.data; 152 153 CoreDocumentImpl ownerDocument = ownerDocument(); 154 155 ownerDocument.modifyingCharacterData(this); 157 158 this.data = value; 159 160 ownerDocument.modifiedCharacterData(this, oldvalue, value); 162 } 163 164 168 public void setNodeValue(String value) { 169 170 setNodeValueInternal(value); 171 172 ownerDocument().replacedText(this); 174 } 175 176 180 188 public String getData() { 189 if (needsSyncData()) { 190 synchronizeData(); 191 } 192 return data; 193 } 194 195 199 public int getLength() { 200 if (needsSyncData()) { 201 synchronizeData(); 202 } 203 return data.length(); 204 } 205 206 214 public void appendData(String data) { 215 216 if (isReadOnly()) { 217 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 218 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 219 } 220 if (data == null) { 221 return; 222 } 223 if (needsSyncData()) { 224 synchronizeData(); 225 } 226 227 setNodeValue(this.data + data); 228 229 } 231 243 public void deleteData(int offset, int count) 244 throws DOMException { 245 246 if (isReadOnly()) { 247 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 248 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 249 } 250 251 if (count < 0) { 252 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 253 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 254 } 255 256 if (needsSyncData()) { 257 synchronizeData(); 258 } 259 int tailLength = Math.max(data.length() - count - offset, 0); 260 try { 261 String value = data.substring(0, offset) + 262 (tailLength > 0 263 ? data.substring(offset + count, offset + count + tailLength) 264 : ""); 265 266 setNodeValueInternal(value); 267 268 ownerDocument().deletedText(this, offset, count); 270 } 271 catch (StringIndexOutOfBoundsException e) { 272 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 273 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 274 } 275 276 } 278 287 public void insertData(int offset, String data) 288 throws DOMException { 289 290 if (isReadOnly()) { 291 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 292 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 293 } 294 295 if (needsSyncData()) { 296 synchronizeData(); 297 } 298 try { 299 String value = 300 new StringBuffer (this.data).insert(offset, data).toString(); 301 302 setNodeValueInternal(value); 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 338 public void replaceData(int offset, int count, String data) 339 throws DOMException { 340 341 deleteData(offset, count); 348 insertData(offset, data); 349 350 } 352 357 public void setData(String value) 358 throws DOMException { 359 setNodeValue(value); 360 } 361 362 382 public String substringData(int offset, int count) 383 throws DOMException { 384 385 if (needsSyncData()) { 386 synchronizeData(); 387 } 388 389 int length = data.length(); 390 if (count < 0 || offset < 0 || offset > length - 1) { 391 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null); 392 throw new DOMException (DOMException.INDEX_SIZE_ERR, msg); 393 } 394 395 int tailIndex = Math.min(offset + count, length); 396 397 return data.substring(offset, tailIndex); 398 399 } 401 } | Popular Tags |