1 21 24 package org.lobobrowser.html.domimpl; 25 26 import org.w3c.dom.*; 27 import org.lobobrowser.util.*; 28 29 public class TextImpl extends CharacterDataImpl implements Text { 30 public TextImpl() { 31 this(""); 32 } 33 34 public TextImpl(String text) { 35 this.text = text; 36 } 37 38 39 42 public boolean isElementContentWhitespace() { 43 String t = this.text; 44 return t == null || t.trim().equals(""); 45 } 46 47 50 public Text replaceWholeText(String content) throws DOMException { 51 NodeImpl parent = (NodeImpl) this.getParentNode(); 52 if(parent == null) { 53 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text node has no parent"); 54 } 55 return parent.replaceAdjacentTextNodes(this, content); 56 } 57 58 61 public Text splitText(int offset) throws DOMException { 62 NodeImpl parent = (NodeImpl) this.getParentNode(); 63 if(parent == null) { 64 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text node has no parent"); 65 } 66 String t = this.text; 67 if(offset < 0 || offset > t.length()) { 68 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Bad offset: " + offset); 69 } 70 String content1 = t.substring(0, offset); 71 String content2 = t.substring(offset); 72 this.text = content1; 73 TextImpl newNode = new TextImpl(content2); 74 newNode.setOwnerDocument(this.document); 75 return (Text) parent.insertAfter(newNode, this); 76 } 77 78 81 public String getWholeText() { 82 NodeImpl parent = (NodeImpl) this.getParentNode(); 83 if(parent == null) { 84 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text node has no parent"); 85 } 86 return parent.getTextContent(); 87 } 88 89 92 public String getLocalName() { 93 return null; 94 } 95 96 99 public String getNodeName() { 100 return "#text"; 101 } 102 103 104 105 108 public short getNodeType() { 109 return Node.TEXT_NODE; 110 } 111 112 115 public String getNodeValue() throws DOMException { 116 return this.text; 117 } 118 119 122 public void setNodeValue(String nodeValue) throws DOMException { 123 this.text = nodeValue; 124 } 125 126 public void setTextContent(String textContent) throws DOMException { 127 this.text = textContent; 128 } 129 130 protected Node createSimilarNode() { 131 return new TextImpl(this.text); 132 } 133 134 public String toString() { 135 String text = this.text; 136 int textLength = text == null ? 0 : text.length(); 137 return "#text[length=" + textLength + ",value=\"" + Strings.truncate(text, 64) + "\",renderState=" + this.getRenderState() + "]"; 138 } 139 } 140 | Popular Tags |