KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > nodes > TextNode


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/nodes/TextNode.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/17 13:45:04 $
10
// $Revision: 1.3 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.nodes;
28
29 import org.htmlparser.Text;
30 import org.htmlparser.lexer.Cursor;
31 import org.htmlparser.lexer.Page;
32 import org.htmlparser.util.ParserException;
33 import org.htmlparser.visitors.NodeVisitor;
34
35 /**
36  * Normal text in the HTML document is represented by this class.
37  */

38 public class TextNode
39     extends
40         AbstractNode
41     implements
42         Text
43 {
44     /**
45      * The contents of the string node, or override text.
46      */

47     protected String JavaDoc mText;
48
49     /**
50      * Constructor takes in the text string.
51      * @param text The string node text. For correct generation of HTML, this
52      * should not contain representations of tags (unless they are balanced).
53      */

54     public TextNode (String JavaDoc text)
55     {
56         super (null, 0, 0);
57         setText (text);
58     }
59
60     /**
61      * Constructor takes in the page and beginning and ending posns.
62      * @param page The page this string is on.
63      * @param start The beginning position of the string.
64      * @param end The ending positiong of the string.
65      */

66     public TextNode (Page page, int start, int end)
67     {
68         super (page, start, end);
69         mText = null;
70     }
71
72     /**
73      * Returns the text of the string line.
74      */

75     public String JavaDoc getText ()
76     {
77         return (toHtml ());
78     }
79
80     /**
81      * Sets the string contents of the node.
82      * @param text The new text for the node.
83      */

84     public void setText (String JavaDoc text)
85     {
86         mText = text;
87         nodeBegin = 0;
88         nodeEnd = mText.length ();
89     }
90
91     public String JavaDoc toPlainTextString ()
92     {
93         return (toHtml ());
94     }
95
96     public String JavaDoc toHtml ()
97     {
98         String JavaDoc ret;
99         
100         ret = mText;
101         if (null == ret)
102             ret = mPage.getText (getStartPosition (), getEndPosition ());
103
104         return (ret);
105     }
106
107     /**
108      * Express this string node as a printable string
109      * This is suitable for display in a debugger or output to a printout.
110      * Control characters are replaced by their equivalent escape
111      * sequence and contents is truncated to 80 characters.
112      * @return A string representation of the string node.
113      */

114     public String JavaDoc toString ()
115     {
116         int startpos;
117         int endpos;
118         Cursor start;
119         Cursor end;
120         char c;
121         StringBuffer JavaDoc ret;
122
123         startpos = getStartPosition ();
124         endpos = getEndPosition ();
125         ret = new StringBuffer JavaDoc (endpos - startpos + 20);
126         if (null == mText)
127         {
128             start = new Cursor (getPage (), startpos);
129             end = new Cursor (getPage (), endpos);
130             ret.append ("Txt (");
131             ret.append (start);
132             ret.append (",");
133             ret.append (end);
134             ret.append ("): ");
135             while (start.getPosition () < endpos)
136             {
137                 try
138                 {
139                     c = mPage.getCharacter (start);
140                     switch (c)
141                     {
142                         case '\t':
143                             ret.append ("\\t");
144                             break;
145                         case '\n':
146                             ret.append ("\\n");
147                             break;
148                         case '\r':
149                             ret.append ("\\r");
150                             break;
151                         default:
152                             ret.append (c);
153                     }
154                 }
155                 catch (ParserException pe)
156                 {
157                     // not really expected, but we're only doing toString, so ignore
158
}
159                 if (77 <= ret.length ())
160                 {
161                     ret.append ("...");
162                     break;
163                 }
164             }
165         }
166         else
167         {
168             ret.append ("Txt (");
169             ret.append (startpos);
170             ret.append (",");
171             ret.append (endpos);
172             ret.append ("): ");
173             for (int i = 0; i < mText.length (); i++)
174             {
175                 c = mText.charAt (i);
176                 switch (c)
177                 {
178                     case '\t':
179                         ret.append ("\\t");
180                         break;
181                     case '\n':
182                         ret.append ("\\n");
183                         break;
184                     case '\r':
185                         ret.append ("\\r");
186                         break;
187                     default:
188                         ret.append (c);
189                 }
190                 if (77 <= ret.length ())
191                 {
192                     ret.append ("...");
193                     break;
194                 }
195             }
196         }
197
198         return (ret.toString ());
199     }
200
201     /**
202      * String visiting code.
203      * @param visitor The <code>NodeVisitor</code> object to invoke
204      * <code>visitStringNode()</code> on.
205      */

206     public void accept (NodeVisitor visitor)
207     {
208         visitor.visitStringNode (this);
209     }
210 }
211
Popular Tags