1 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 38 public class RemarkNode 39 extends 40 AbstractNode 41 implements 42 Remark 43 { 44 47 protected String mText; 48 49 54 public RemarkNode (String text) 55 { 56 super (null, 0, 0); 57 setText (text); 58 } 59 60 66 public RemarkNode (Page page, int start, int end) 67 { 68 super (page, start, end); 69 mText = null; 70 } 71 72 76 public String getText() 77 { 78 int start; 79 int end; 80 String ret; 81 82 if (null == mText) 83 { 84 start = getStartPosition () + 4; end = getEndPosition () - 3; 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 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 public String toPlainTextString() 112 { 113 return (getText()); 114 } 115 116 public String toHtml() 117 { 118 StringBuffer buffer; 119 String ret; 120 121 if (null == mText) 122 ret = mPage.getText (getStartPosition (), getEndPosition ()); 123 else 124 { 125 buffer = new StringBuffer (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 142 public String toString() 143 { 144 int startpos; 145 int endpos; 146 Cursor start; 147 Cursor end; 148 char c; 149 StringBuffer ret; 150 151 startpos = getStartPosition (); 152 endpos = getEndPosition (); 153 ret = new StringBuffer (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); endpos -= 3; 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 } 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 236 public void accept (NodeVisitor visitor) 237 { 238 visitor.visitRemarkNode (this); 239 } 240 } 241 | Popular Tags |