/ org.htmlparser / src / org / htmlparser / nodes / RemarkNode.java
RemarkNode.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - 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: 2005/04/10 23:20:44 $
 10  // $Revision: 1.4 $
 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 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 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 getText()
 77      {
 78          int start;
 79          int end;
 80          String 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 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      /**
112       * Return the remark text.
113       * @return The HTML comment.
114       */
115      public String toPlainTextString ()
116      {
117          return (getText());
118      }
119  
120      /**
121       * Return The full HTML remark.
122       * @return The comment, i.e. {@.html <!-- this is a comment -->}.
123       */
124      public String toHtml ()
125      {
126          StringBuffer buffer;
127          String ret;
128          
129          if (null == mText)
130              ret = mPage.getText (getStartPosition (), getEndPosition ());
131          else
132          {
133              buffer = new StringBuffer (mText.length () + 7);
134              buffer.append ("<!--");
135              buffer.append (mText);
136              buffer.append ("-->");
137              ret = buffer.toString ();
138          }
139  
140          return (ret);
141      }
142  
143      /**
144       * Print the contents of the remark tag.
145       * This is suitable for display in a debugger or output to a printout.
146       * Control characters are replaced by their equivalent escape
147       * sequence and contents is truncated to 80 characters.
148       * @return A string representation of the remark node.
149       */
150      public String toString()
151      {
152          int startpos;
153          int endpos;
154          Cursor start;
155          Cursor end;
156          char c;
157          StringBuffer ret;
158  
159          startpos = getStartPosition ();
160          endpos = getEndPosition ();
161          ret = new StringBuffer (endpos - startpos + 20);
162          if (null == mText)
163          {
164              start = new Cursor (getPage (), startpos);
165              end = new Cursor (getPage (), endpos);
166              ret.append ("Rem (");
167              ret.append (start);
168              ret.append (",");
169              ret.append (end);
170              ret.append ("): ");
171              start.setPosition (startpos + 4); // <!--
172              endpos -= 3; // -->
173              while (start.getPosition () < endpos)
174              {
175                  try
176                  {
177                      c = mPage.getCharacter (start);
178                      switch (c)
179                      {
180                          case '\t':
181                              ret.append ("\\t");
182                              break;
183                          case '\n':
184                              ret.append ("\\n");
185                              break;
186                          case '\r':
187                              ret.append ("\\r");
188                              break;
189                          default:
190                              ret.append (c);
191                      }
192                  }
193                  catch (ParserException pe)
194                  {
195                      // not really expected, but we're only doing toString, so ignore
196                  }
197                  if (77 <= ret.length ())
198                  {
199                      ret.append ("...");
200                      break;
201                  }
202              }
203          }
204          else
205          {
206              ret.append ("Rem (");
207              ret.append (startpos);
208              ret.append (",");
209              ret.append (endpos);
210              ret.append ("): ");
211              for (int i = 0; i < mText.length (); i++)
212              {
213                  c = mText.charAt (i);
214                  switch (c)
215                  {
216                      case '\t':
217                          ret.append ("\\t");
218                          break;
219                      case '\n':
220                          ret.append ("\\n");
221                          break;
222                      case '\r':
223                          ret.append ("\\r");
224                          break;
225                      default:
226                          ret.append (c);
227                  }
228                  if (77 <= ret.length ())
229                  {
230                      ret.append ("...");
231                      break;
232                  }
233              }
234          }
235  
236          return (ret.toString ());
237      }
238  
239      /**
240       * Remark visiting code.
241       * @param visitor The <code>NodeVisitor</code> object to invoke 
242       * <code>visitRemarkNode()</code> on.
243       */
244      public void accept (NodeVisitor visitor)
245      {
246          visitor.visitRemarkNode (this);
247      }
248  }