1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import org.w3c.dom.DOMException ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.NodeList ; 63 import org.w3c.dom.events.MutationEvent ; 64 import org.xquark.xpath.datamodel.xerces.dom.events.MutationEventImpl; 65 66 82 public abstract class CharacterDataImpl 83 extends ChildNode { 84 85 89 90 static final long serialVersionUID = 7931170150428474230L; 91 92 96 protected String data; 97 98 99 private static transient NodeList singletonNodeList = new NodeList () { 100 public Node item(int index) { return null; } 101 public int getLength() { return 0; } 102 }; 103 104 108 109 protected CharacterDataImpl(DocumentImpl ownerDocument, String data) { 110 super(ownerDocument); 111 this.data = data; 112 } 113 114 118 119 public NodeList getChildNodes() { 120 return singletonNodeList; 121 } 122 123 126 public String getNodeValue() { 127 if (needsSyncData()) { 128 synchronizeData(); 129 } 130 return data; 131 } 132 133 140 void setNodeValueInternal(String value) { 141 144 setValueCalled(true); 145 setNodeValue(value); 146 setValueCalled(false); 147 } 148 149 153 public void setNodeValue(String value) { 154 if (isReadOnly()) 155 throw new DOMException ( 156 DOMException.NO_MODIFICATION_ALLOWED_ERR, 157 "DOM001 Modification not allowed"); 158 if (needsSyncData()) { 161 synchronizeData(); 162 } 163 164 String oldvalue = this.data; 166 EnclosingAttr enclosingAttr=null; 167 if(MUTATIONEVENTS) 168 { 169 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 174 if(lc.captures+lc.bubbles+lc.defaults>0) 175 { 176 enclosingAttr=getEnclosingAttr(); 177 } 178 } 180 this.data = value; 181 if (!setValueCalled()) { 182 ownerDocument().replacedText(this); 184 } 185 186 if(MUTATIONEVENTS) 187 { 188 LCount lc = 190 LCount.lookup(MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED); 191 if(lc.captures+lc.bubbles+lc.defaults>0) 192 { 193 MutationEvent me= new MutationEventImpl(); 194 me.initMutationEvent( 195 MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED, 196 true,false,null,oldvalue,value,null,(short)0); 197 dispatchEvent(me); 198 } 199 200 dispatchAggregateEvents(enclosingAttr); 203 } 205 } 207 211 219 public String getData() { 220 if (needsSyncData()) { 221 synchronizeData(); 222 } 223 return data; 224 } 225 226 230 public int getLength() { 231 if (needsSyncData()) { 232 synchronizeData(); 233 } 234 return data.length(); 235 } 236 237 245 public void appendData(String data) { 246 247 if (isReadOnly()) { 248 throw new DOMException ( 249 DOMException.NO_MODIFICATION_ALLOWED_ERR, 250 "DOM001 Modification not allowed"); 251 } 252 253 if (needsSyncData()) { 254 synchronizeData(); 255 } 256 257 setNodeValue(this.data + data); 259 260 } 262 274 public void deleteData(int offset, int count) 275 throws DOMException { 276 277 if (isReadOnly()) { 278 throw new DOMException ( 279 DOMException.NO_MODIFICATION_ALLOWED_ERR, 280 "DOM001 Modification not allowed"); 281 } 282 283 if (count < 0) { 284 throw new DOMException (DOMException.INDEX_SIZE_ERR, 285 "DOM004 Index out of bounds"); 286 } 287 288 if (needsSyncData()) { 289 synchronizeData(); 290 } 291 int tailLength = Math.max(data.length() - count - offset, 0); 292 try { 293 setNodeValueInternal(data.substring(0, offset) + 295 (tailLength > 0 296 ? data.substring(offset + count, offset + count + tailLength) 297 : "") ); 298 ownerDocument().deletedText(this, offset, count); 300 } 301 catch (StringIndexOutOfBoundsException e) { 302 throw new DOMException (DOMException.INDEX_SIZE_ERR, 303 "DOM004 Index out of bounds"); 304 } 305 306 } 308 317 public void insertData(int offset, String data) 318 throws DOMException { 319 320 if (isReadOnly()) { 321 throw new DOMException ( 322 DOMException.NO_MODIFICATION_ALLOWED_ERR, 323 "DOM001 Modification not allowed"); 324 } 325 326 if (needsSyncData()) { 327 synchronizeData(); 328 } 329 try { 330 setNodeValueInternal( 332 new StringBuffer (this.data).insert(offset, data).toString() 333 ); 334 ownerDocument().insertedText(this, offset, data.length()); 336 } 337 catch (StringIndexOutOfBoundsException e) { 338 throw new DOMException (DOMException.INDEX_SIZE_ERR, 339 "DOM004 Index out of bounds"); 340 } 341 342 } 344 368 public void replaceData(int offset, int count, String data) 369 throws DOMException { 370 371 deleteData(offset, count); 378 insertData(offset, data); 379 380 } 382 387 public void setData(String value) 388 throws DOMException { 389 setNodeValue(value); 390 } 391 392 412 public String substringData(int offset, int count) 413 throws DOMException { 414 415 if (needsSyncData()) { 416 synchronizeData(); 417 } 418 419 int length = data.length(); 420 if (count < 0 || offset < 0 || offset > length - 1) { 421 throw new DOMException (DOMException.INDEX_SIZE_ERR, 422 "DOM004 Index out of bounds"); 423 } 424 425 int tailIndex = Math.min(offset + count, length); 426 427 return data.substring(offset, tailIndex); 428 429 } 431 } | Popular Tags |