KickJava   Java API By Example, From Geeks To Geeks.

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


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/RemarkNode.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.Remark;
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  * The remark tag is identified and represented by this class.
37  */

38 public class RemarkNode
39     extends
40         AbstractNode
41     implements
42         Remark
43 {
44     /**
45      * The contents of the remark 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 RemarkNode (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 remark is on.
63      * @param start The beginning position of the remark.
64      * @param end The ending positiong of the remark.
65      */

66     public RemarkNode (Page page, int start, int end)
67     {
68         super (page, start, end);
69         mText = null;
70     }
71
72     /**
73      * Returns the text contents of the comment tag.
74      * @return The contents of the text inside the comment delimiters.
75      */

76     public String JavaDoc getText()
77     {
78         int start;
79         int end;
80         String JavaDoc ret;
81
82         if (null == mText)
83         {
84             start = getStartPosition () + 4; // <!--
85
end = getEndPosition () - 3; // -->
86
if (start >= end)
87                 ret = "";
88             else
89                 ret = mPage.getText (start, end);
90         }
91         else
92             ret = mText;
93
94         return (ret);
95     }
96
97     /**
98      * Sets the string contents of the node.
99      * If the text has the remark delimiters (&lt;!-- --&gt;), these are stripped off.
100      * @param text The new text for the node.
101      */

102     public void setText (String JavaDoc text)
103     {
104         mText = text;
105         if (text.startsWith ("<!--") && text.endsWith ("-->"))
106             mText = text.substring (4, text.length () - 3);
107         nodeBegin = 0;
108         nodeEnd = mText.length ();
109     }
110
111     public String JavaDoc toPlainTextString()
112     {
113         return (getText());
114     }
115     
116     public String JavaDoc toHtml()
117     {
118         StringBuffer JavaDoc buffer;
119         String JavaDoc ret;
120         
121         if (null == mText)
122             ret = mPage.getText (getStartPosition (), getEndPosition ());
123         else
124         {
125             buffer = new StringBuffer JavaDoc (mText.length () + 7);
126             buffer.append ("<!--");
127             buffer.append (mText);
128             buffer.append ("-->");
129             ret = buffer.toString ();
130         }
131
132         return (ret);
133     }
134
135     /**
136      * Print the contents of the remark tag.
137      * This is suitable for display in a debugger or output to a printout.
138      * Control characters are replaced by their equivalent escape
139      * sequence and contents is truncated to 80 characters.
140      * @return A string representation of the remark node.
141      */

142     public String JavaDoc toString()
143     {
144         int startpos;
145         int endpos;
146         Cursor start;
147         Cursor end;
148         char c;
149         StringBuffer JavaDoc ret;
150
151         startpos = getStartPosition ();
152         endpos = getEndPosition ();
153         ret = new StringBuffer JavaDoc (endpos - startpos + 20);
154         if (null == mText)
155         {
156             start = new Cursor (getPage (), startpos);
157             end = new Cursor (getPage (), endpos);
158             ret.append ("Rem (");
159             ret.append (start);
160             ret.append (",");
161             ret.append (end);
162             ret.append ("): ");
163             start.setPosition (startpos + 4); // <!--
164
endpos -= 3; // -->
165
while (start.getPosition () < endpos)
166             {
167                 try
168                 {
169                     c = mPage.getCharacter (start);
170                     switch (c)
171                     {
172                         case '\t':
173                             ret.append ("\\t");
174                             break;
175                         case '\n':
176                             ret.append ("\\n");
177                             break;
178                         case '\r':
179                             ret.append ("\\r");
180                             break;
181                         default:
182                             ret.append (c);
183                     }
184                 }
185                 catch (ParserException pe)
186                 {
187                     // not really expected, but we're only doing toString, so ignore
188
}
189                 if (77 <= ret.length ())
190                 {
191                     ret.append ("...");
192                     break;
193                 }
194             }
195         }
196         else
197         {
198             ret.append ("Rem (");
199             ret.append (startpos);
200             ret.append (",");
201             ret.append (endpos);
202             ret.append ("): ");
203             for (int i = 0; i < mText.length (); i++)
204             {
205                 c = mText.charAt (i);
206                 switch (c)
207                 {
208                     case '\t':
209                         ret.append ("\\t");
210                         break;
211                     case '\n':
212                         ret.append ("\\n");
213                         break;
214                     case '\r':
215                         ret.append ("\\r");
216                         break;
217                     default:
218                         ret.append (c);
219                 }
220                 if (77 <= ret.length ())
221                 {
222                     ret.append ("...");
223                     break;
224                 }
225             }
226         }
227
228         return (ret.toString ());
229     }
230
231     /**
232      * Remark visiting code.
233      * @param visitor The <code>NodeVisitor</code> object to invoke
234      * <code>visitRemarkNode()</code> on.
235      */

236     public void accept (NodeVisitor visitor)
237     {
238         visitor.visitRemarkNode (this);
239     }
240 }
241
Popular Tags