1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import org.w3c.dom.CharacterData ; 61 import org.w3c.dom.DOMException ; 62 import org.w3c.dom.Node ; 63 import org.w3c.dom.Text ; 64 65 81 public class TextImpl 82 extends CharacterDataImpl 83 implements CharacterData , Text { 84 85 89 90 static final long serialVersionUID = -5294980852957403469L; 91 92 96 97 public TextImpl(){} 98 99 100 public TextImpl(CoreDocumentImpl ownerDoc, String data) { 101 super(ownerDoc, data); 102 } 103 104 110 public void setValues(CoreDocumentImpl ownerDoc, String data){ 111 112 flags=0; 113 nextSibling = null; 114 previousSibling=null; 115 setOwnerDocument(ownerDoc); 116 super.data = data; 117 } 118 122 126 public short getNodeType() { 127 return Node.TEXT_NODE; 128 } 129 130 131 public String getNodeName() { 132 return "#text"; 133 } 134 135 138 public void setIgnorableWhitespace(boolean ignore) { 139 140 if (needsSyncData()) { 141 synchronizeData(); 142 } 143 isIgnorableWhitespace(ignore); 144 145 } 147 148 158 public boolean isElementContentWhitespace() { 159 if (needsSyncData()) { 161 synchronizeData(); 162 } 163 return internalIsIgnorableWhitespace(); 164 } 165 166 167 173 public String getWholeText(){ 174 175 if (needsSyncData()) { 176 synchronizeData(); 177 } 178 if (nextSibling == null) { 179 return data; 180 } 181 if (fBufferStr == null){ 182 fBufferStr = new StringBuffer (); 183 } 184 else { 185 fBufferStr.setLength(0); 186 } 187 if (data != null && data.length() != 0) { 188 fBufferStr.append(data); 189 } 190 getWholeText(nextSibling, fBufferStr); 191 return fBufferStr.toString(); 192 193 } 194 195 204 private boolean getWholeText(Node node, StringBuffer buffer){ 205 String text; 206 while (node != null) { 207 short type = node.getNodeType(); 208 if (type == Node.ENTITY_REFERENCE_NODE) { 209 if (getWholeText(node.getFirstChild(), buffer)){ 210 return true; 211 } 212 } 213 else if (type == Node.TEXT_NODE || 214 type == Node.CDATA_SECTION_NODE) { 215 ((NodeImpl)node).getTextContent(buffer); 216 } 217 else { 218 return true; 219 } 220 221 node = node.getNextSibling(); 222 } 223 return false; 224 } 225 226 229 public Text replaceWholeText(String content) 230 throws DOMException { 231 232 if (needsSyncData()) { 233 synchronizeData(); 234 } 235 236 if (!canModify(nextSibling)) { 238 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 239 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null)); 240 } 241 242 Node parent = this.getParentNode(); 243 if (content == null || content.length() == 0) { 244 if (parent !=null) { parent.removeChild(this); 247 return null; 248 } 249 } 250 Text currentNode = null; 251 if (isReadOnly()){ 252 Text newNode = this.ownerDocument().createTextNode(content); 253 if (parent !=null) { parent.insertBefore(newNode, this); 255 parent.removeChild(this); 256 currentNode = newNode; 257 } else { 258 return newNode; 259 } 260 } else { 261 this.setData(content); 262 currentNode = this; 263 } 264 Node sibling = currentNode.getNextSibling(); 265 while ( sibling !=null) { 266 parent.removeChild(sibling); 267 sibling = currentNode.getNextSibling(); 268 } 269 270 return currentNode; 271 } 272 273 284 private boolean canModify(Node node){ 285 while (node != null) { 286 short type = node.getNodeType(); 287 if (type == Node.ENTITY_REFERENCE_NODE) { 288 if (!canModify(node.getFirstChild())){ 289 return false; 290 } 291 } 292 else if (type != Node.TEXT_NODE && 293 type != Node.CDATA_SECTION_NODE) { 294 return false; 295 } 296 297 node = node.getNextSibling(); 298 } 299 return true; 300 } 301 302 305 public boolean isIgnorableWhitespace() { 306 307 if (needsSyncData()) { 308 synchronizeData(); 309 } 310 return internalIsIgnorableWhitespace(); 311 312 } 314 315 319 335 public Text splitText(int offset) 336 throws DOMException { 337 338 if (isReadOnly()) { 339 throw new DOMException ( 340 DOMException.NO_MODIFICATION_ALLOWED_ERR, 341 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null)); 342 } 343 344 if (needsSyncData()) { 345 synchronizeData(); 346 } 347 if (offset < 0 || offset > data.length() ) { 348 throw new DOMException (DOMException.INDEX_SIZE_ERR, 349 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null)); 350 } 351 352 Text newText = 354 getOwnerDocument().createTextNode(data.substring(offset)); 355 setNodeValue(data.substring(0, offset)); 356 357 Node parentNode = getParentNode(); 359 if (parentNode != null) { 360 parentNode.insertBefore(newText, nextSibling); 361 } 362 363 return newText; 364 365 } 367 368 371 public void replaceData (String value){ 372 data = value; 373 } 374 375 376 380 public String removeData (){ 381 String olddata=data; 382 data = ""; 383 return olddata; 384 } 385 386 387 } | Popular Tags |