KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > TextImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Sep 4, 2005
23  */

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 JavaDoc text) {
35         this.text = text;
36     }
37
38     
39     /* (non-Javadoc)
40      * @see org.w3c.dom.html2.Text#isElementContentWhitespace()
41      */

42     public boolean isElementContentWhitespace() {
43         String JavaDoc t = this.text;
44         return t == null || t.trim().equals("");
45     }
46
47     /* (non-Javadoc)
48      * @see org.w3c.dom.html2.Text#replaceWholeText(java.lang.String)
49      */

50     public Text replaceWholeText(String JavaDoc 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     /* (non-Javadoc)
59      * @see org.w3c.dom.html2.Text#splitText(int)
60      */

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 JavaDoc t = this.text;
67         if(offset < 0 || offset > t.length()) {
68             throw new DOMException(DOMException.INDEX_SIZE_ERR, "Bad offset: " + offset);
69         }
70         String JavaDoc content1 = t.substring(0, offset);
71         String JavaDoc 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     /* (non-Javadoc)
79      * @see org.w3c.dom.html2.Text#getwholeText()
80      */

81     public String JavaDoc 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     /* (non-Javadoc)
90      * @see org.xamjwg.html.domimpl.NodeImpl#getlocalName()
91      */

92     public String JavaDoc getLocalName() {
93         return null;
94     }
95
96     /* (non-Javadoc)
97      * @see org.xamjwg.html.domimpl.NodeImpl#getnodeName()
98      */

99     public String JavaDoc getNodeName() {
100         return "#text";
101     }
102     
103     
104
105     /* (non-Javadoc)
106      * @see org.xamjwg.html.domimpl.NodeImpl#getnodeType()
107      */

108     public short getNodeType() {
109         return Node.TEXT_NODE;
110     }
111
112     /* (non-Javadoc)
113      * @see org.xamjwg.html.domimpl.NodeImpl#getnodeValue()
114      */

115     public String JavaDoc getNodeValue() throws DOMException {
116         return this.text;
117     }
118
119     /* (non-Javadoc)
120      * @see org.xamjwg.html.domimpl.NodeImpl#setnodeValue(java.lang.String)
121      */

122     public void setNodeValue(String JavaDoc nodeValue) throws DOMException {
123         this.text = nodeValue;
124     }
125
126     public void setTextContent(String JavaDoc textContent) throws DOMException {
127         this.text = textContent;
128     }
129
130     protected Node createSimilarNode() {
131         return new TextImpl(this.text);
132     }
133
134     public String JavaDoc toString() {
135         String JavaDoc 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