1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.CharacterData ; 20 import org.w3c.dom.DOMException ; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.Text ; 23 24 42 public class TextImpl 43 extends CharacterDataImpl 44 implements CharacterData , Text { 45 46 50 51 55 56 static final long serialVersionUID = -5294980852957403469L; 57 58 62 63 public TextImpl(){} 64 65 66 public TextImpl(CoreDocumentImpl ownerDoc, String data) { 67 super(ownerDoc, data); 68 } 69 70 76 public void setValues(CoreDocumentImpl ownerDoc, String data){ 77 78 flags=0; 79 nextSibling = null; 80 previousSibling=null; 81 setOwnerDocument(ownerDoc); 82 super.data = data; 83 } 84 88 92 public short getNodeType() { 93 return Node.TEXT_NODE; 94 } 95 96 97 public String getNodeName() { 98 return "#text"; 99 } 100 101 104 public void setIgnorableWhitespace(boolean ignore) { 105 106 if (needsSyncData()) { 107 synchronizeData(); 108 } 109 isIgnorableWhitespace(ignore); 110 111 } 113 114 124 public boolean isElementContentWhitespace() { 125 if (needsSyncData()) { 127 synchronizeData(); 128 } 129 return internalIsIgnorableWhitespace(); 130 } 131 132 133 139 public String getWholeText(){ 140 141 if (needsSyncData()) { 142 synchronizeData(); 143 } 144 145 if (fBufferStr == null){ 146 fBufferStr = new StringBuffer (); 147 } 148 else { 149 fBufferStr.setLength(0); 150 } 151 if (data != null && data.length() != 0) { 152 fBufferStr.append(data); 153 } 154 155 getWholeTextBackward(this.getPreviousSibling(), fBufferStr, this.getParentNode()); 157 String temp = fBufferStr.toString(); 158 159 fBufferStr.setLength(0); 161 162 getWholeTextForward(this.getNextSibling(), fBufferStr, this.getParentNode()); 164 165 return temp + fBufferStr.toString(); 166 167 } 168 169 175 protected void insertTextContent(StringBuffer buf) throws DOMException { 176 String content = getNodeValue(); 177 if (content != null) { 178 buf.insert(0, content); 179 } 180 } 181 182 192 private boolean getWholeTextForward(Node node, StringBuffer buffer, Node parent){ 193 boolean inEntRef = false; 195 196 if (parent!=null) { 197 inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE; 198 } 199 200 while (node != null) { 201 short type = node.getNodeType(); 202 if (type == Node.ENTITY_REFERENCE_NODE) { 203 if (getWholeTextForward(node.getFirstChild(), buffer, node)){ 204 return true; 205 } 206 } 207 else if (type == Node.TEXT_NODE || 208 type == Node.CDATA_SECTION_NODE) { 209 ((NodeImpl)node).getTextContent(buffer); 210 } 211 else { 212 return true; 213 } 214 215 node = node.getNextSibling(); 216 } 217 218 if (inEntRef) { 222 getWholeTextForward(parent.getNextSibling(), buffer, parent.getParentNode()); 223 return true; 224 } 225 226 return false; 227 } 228 229 239 private boolean getWholeTextBackward(Node node, StringBuffer buffer, Node parent){ 240 241 boolean inEntRef = false; 243 if (parent!=null) { 244 inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE; 245 } 246 247 while (node != null) { 248 short type = node.getNodeType(); 249 if (type == Node.ENTITY_REFERENCE_NODE) { 250 if (getWholeTextBackward(node.getLastChild(), buffer, node)){ 251 return true; 252 } 253 } 254 else if (type == Node.TEXT_NODE || 255 type == Node.CDATA_SECTION_NODE) { 256 ((TextImpl)node).insertTextContent(buffer); 257 } 258 else { 259 return true; 260 } 261 262 node = node.getPreviousSibling(); 263 } 264 265 if (inEntRef) { 269 getWholeTextBackward(parent.getPreviousSibling(), buffer, parent.getParentNode()); 270 return true; 271 } 272 273 return false; 274 } 275 276 287 public Text replaceWholeText(String content) throws DOMException { 288 289 if (needsSyncData()) { 290 synchronizeData(); 291 } 292 293 Node parent = this.getParentNode(); 295 if (content == null || content.length() == 0) { 296 if (parent != null) { parent.removeChild(this); 299 } 300 return null; 301 } 302 303 if (ownerDocument().errorChecking) { 305 if (!canModifyPrev(this)) { 306 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 307 DOMMessageFormatter.formatMessage( 308 DOMMessageFormatter.DOM_DOMAIN, 309 "NO_MODIFICATION_ALLOWED_ERR", null)); 310 } 311 312 if (!canModifyNext(this)) { 314 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 315 DOMMessageFormatter.formatMessage( 316 DOMMessageFormatter.DOM_DOMAIN, 317 "NO_MODIFICATION_ALLOWED_ERR", null)); 318 } 319 } 320 321 Text currentNode = null; 323 if (isReadOnly()) { 324 Text newNode = this.ownerDocument().createTextNode(content); 325 if (parent != null) { parent.insertBefore(newNode, this); 327 parent.removeChild(this); 328 currentNode = newNode; 329 } else { 330 return newNode; 331 } 332 } else { 333 this.setData(content); 334 currentNode = this; 335 } 336 337 Node prev = currentNode.getPreviousSibling(); 339 while (prev != null) { 340 if ((prev.getNodeType() == Node.TEXT_NODE) 345 || (prev.getNodeType() == Node.CDATA_SECTION_NODE) 346 || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) { 347 parent.removeChild(prev); 348 prev = currentNode; 349 } else { 350 break; 351 } 352 prev = prev.getPreviousSibling(); 353 } 354 355 Node next = currentNode.getNextSibling(); 357 while (next != null) { 358 if ((next.getNodeType() == Node.TEXT_NODE) 363 || (next.getNodeType() == Node.CDATA_SECTION_NODE) 364 || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) { 365 parent.removeChild(next); 366 next = currentNode; 367 } else { 368 break; 369 } 370 next = next.getNextSibling(); 371 } 372 373 return currentNode; 374 } 375 376 395 private boolean canModifyPrev(Node node) { 396 boolean textLastChild = false; 397 398 Node prev = node.getPreviousSibling(); 399 400 while (prev != null) { 401 402 short type = prev.getNodeType(); 403 404 if (type == Node.ENTITY_REFERENCE_NODE) { 405 Node lastChild = prev.getLastChild(); 408 409 if (lastChild == null) { 412 return false; 413 } 414 415 while (lastChild != null) { 419 short lType = lastChild.getNodeType(); 420 421 if (lType == Node.TEXT_NODE 422 || lType == Node.CDATA_SECTION_NODE) { 423 textLastChild = true; 424 } else if (lType == Node.ENTITY_REFERENCE_NODE) { 425 if (!canModifyPrev(lastChild)) { 426 return false; 427 } else { 428 textLastChild = true; 432 } 433 } else { 434 if (textLastChild) { 438 return false; 439 } else { 440 return true; 441 } 442 } 443 lastChild = lastChild.getPreviousSibling(); 444 } 445 } else if (type == Node.TEXT_NODE 446 || type == Node.CDATA_SECTION_NODE) { 447 } else { 449 return true; 453 } 454 455 prev = prev.getPreviousSibling(); 456 } 457 458 return true; 459 } 460 461 480 private boolean canModifyNext(Node node) { 481 boolean textFirstChild = false; 482 483 Node next = node.getNextSibling(); 484 while (next != null) { 485 486 short type = next.getNodeType(); 487 488 if (type == Node.ENTITY_REFERENCE_NODE) { 489 Node firstChild = next.getFirstChild(); 492 493 if (firstChild == null) { 496 return false; 497 } 498 499 while (firstChild != null) { 503 short lType = firstChild.getNodeType(); 504 505 if (lType == Node.TEXT_NODE 506 || lType == Node.CDATA_SECTION_NODE) { 507 textFirstChild = true; 508 } else if (lType == Node.ENTITY_REFERENCE_NODE) { 509 if (!canModifyNext(firstChild)) { 510 return false; 511 } else { 512 textFirstChild = true; 516 } 517 } else { 518 if (textFirstChild) { 521 return false; 522 } else { 523 return true; 524 } 525 } 526 firstChild = firstChild.getNextSibling(); 527 } 528 } else if (type == Node.TEXT_NODE 529 || type == Node.CDATA_SECTION_NODE) { 530 } else { 532 return true; 536 } 537 538 next = next.getNextSibling(); 539 } 540 541 return true; 542 } 543 544 550 private boolean hasTextOnlyChildren(Node node) { 551 552 Node child = node; 553 554 if (child == null) { 555 return false; 556 } 557 558 child = child.getFirstChild(); 559 while (child != null) { 560 int type = child.getNodeType(); 561 562 if (type == Node.ENTITY_REFERENCE_NODE) { 563 return hasTextOnlyChildren(child); 564 } 565 else if (type != Node.TEXT_NODE 566 && type != Node.CDATA_SECTION_NODE 567 && type != Node.ENTITY_REFERENCE_NODE) { 568 return false; 569 } 570 child = child.getNextSibling(); 571 } 572 return true; 573 } 574 575 576 579 public boolean isIgnorableWhitespace() { 580 581 if (needsSyncData()) { 582 synchronizeData(); 583 } 584 return internalIsIgnorableWhitespace(); 585 586 } 588 589 593 611 public Text splitText(int offset) 612 throws DOMException { 613 614 if (isReadOnly()) { 615 throw new DOMException ( 616 DOMException.NO_MODIFICATION_ALLOWED_ERR, 617 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null)); 618 } 619 620 if (needsSyncData()) { 621 synchronizeData(); 622 } 623 if (offset < 0 || offset > data.length() ) { 624 throw new DOMException (DOMException.INDEX_SIZE_ERR, 625 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null)); 626 } 627 628 Text newText = 630 getOwnerDocument().createTextNode(data.substring(offset)); 631 setNodeValue(data.substring(0, offset)); 632 633 Node parentNode = getParentNode(); 635 if (parentNode != null) { 636 parentNode.insertBefore(newText, nextSibling); 637 } 638 639 return newText; 640 641 } 643 644 647 public void replaceData (String value){ 648 data = value; 649 } 650 651 652 656 public String removeData (){ 657 String olddata=data; 658 data = ""; 659 return olddata; 660 } 661 662 } | Popular Tags |