1 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 38 public class TextNode 39 extends 40 AbstractNode 41 implements 42 Text 43 { 44 47 protected String mText; 48 49 54 public TextNode (String text) 55 { 56 super (null, 0, 0); 57 setText (text); 58 } 59 60 66 public TextNode (Page page, int start, int end) 67 { 68 super (page, start, end); 69 mText = null; 70 } 71 72 75 public String getText () 76 { 77 return (toHtml ()); 78 } 79 80 84 public void setText (String text) 85 { 86 mText = text; 87 nodeBegin = 0; 88 nodeEnd = mText.length (); 89 } 90 91 public String toPlainTextString () 92 { 93 return (toHtml ()); 94 } 95 96 public String toHtml () 97 { 98 String ret; 99 100 ret = mText; 101 if (null == ret) 102 ret = mPage.getText (getStartPosition (), getEndPosition ()); 103 104 return (ret); 105 } 106 107 114 public String toString () 115 { 116 int startpos; 117 int endpos; 118 Cursor start; 119 Cursor end; 120 char c; 121 StringBuffer ret; 122 123 startpos = getStartPosition (); 124 endpos = getEndPosition (); 125 ret = new StringBuffer (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 } 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 206 public void accept (NodeVisitor visitor) 207 { 208 visitor.visitStringNode (this); 209 } 210 } 211 | Popular Tags |