1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.io.IOException;
9 import java.io.PushbackReader;
10 import java.io.Reader;
11 import java.util.HashMap;
12 import java.util.HashSet;
16 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
18 import org.eclipse.jface.text.TextPresentation;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.StyleRange;
23 * Reads the text contents from a reader of HTML contents and translates the
24 * tags or cut them out.
26 public class HTML2TextReader extends SubstitutionTextReader {
28 private static final String LINE_DELIM = System.getProperty(
29 "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
31 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
33 private static final Map fgEntityLookup;
35 private static final Set fgTags;
39 fgTags = new HashSet();
40 fgTags.add("b"); //$NON-NLS-1$
41 fgTags.add("br"); //$NON-NLS-1$
42 fgTags.add("h5"); //$NON-NLS-1$
43 fgTags.add("p"); //$NON-NLS-1$
44 fgTags.add("dl"); //$NON-NLS-1$
45 fgTags.add("dt"); //$NON-NLS-1$
46 fgTags.add("dd"); //$NON-NLS-1$
47 fgTags.add("li"); //$NON-NLS-1$
48 fgTags.add("ul"); //$NON-NLS-1$
49 fgTags.add("pre"); //$NON-NLS-1$
51 fgEntityLookup = new HashMap(7);
52 fgEntityLookup.put("lt", "<"); //$NON-NLS-1$ //$NON-NLS-2$
53 fgEntityLookup.put("gt", ">"); //$NON-NLS-1$ //$NON-NLS-2$
54 fgEntityLookup.put("nbsp", " "); //$NON-NLS-1$ //$NON-NLS-2$
55 fgEntityLookup.put("amp", "&"); //$NON-NLS-1$ //$NON-NLS-2$
56 fgEntityLookup.put("circ", "^"); //$NON-NLS-1$ //$NON-NLS-2$
57 fgEntityLookup.put("tilde", "~"); //$NON-NLS-2$ //$NON-NLS-1$
58 fgEntityLookup.put("quot", "\""); //$NON-NLS-1$ //$NON-NLS-2$
61 private int fCounter = 0;
63 private TextPresentation fTextPresentation;
65 private int fBold = 0;
67 private int fStartOffset = -1;
69 private boolean fInParagraph = false;
71 private boolean fIsPreformattedText = false;
74 * Transforms the html text from the reader to formatted text.
77 * If not <code>null</code>, formattings will be applied to
80 public HTML2TextReader(Reader reader, TextPresentation presentation) {
81 super(new PushbackReader(reader));
82 fTextPresentation = presentation;
85 public int read() throws IOException {
92 protected void startBold() {
94 fStartOffset = fCounter;
98 protected void startPreformattedText() {
99 fIsPreformattedText = true;
100 setSkipWhitespace(false);
103 protected void stopPreformattedText() {
104 fIsPreformattedText = false;
105 setSkipWhitespace(true);
108 protected void stopBold() {
111 if (fTextPresentation != null) {
112 fTextPresentation.addStyleRange(new StyleRange(fStartOffset,
113 fCounter - fStartOffset, null, null, SWT.BOLD));
120 * @see SubstitutionTextReader#computeSubstitution(char)
122 protected String computeSubstitution(int c) throws IOException {
125 return processHTMLTag();
127 return processEntity();
128 else if (fIsPreformattedText)
129 return processPreformattedText(c);
134 private String html2Text(String html) {
137 if ('/' == tag.charAt(0))
138 tag = tag.substring(1);
140 if (!fgTags.contains(tag))
143 if ("pre".equals(html)) { //$NON-NLS-1$
144 startPreformattedText();
148 if ("/pre".equals(html)) { //$NON-NLS-1$
149 stopPreformattedText();
153 if (fIsPreformattedText)
156 if ("b".equals(html)) { //$NON-NLS-1$
161 if ("h5".equals(html) || "dt".equals(html)) { //$NON-NLS-1$ //$NON-NLS-2$
166 if ("dl".equals(html)) //$NON-NLS-1$
169 if ("dd".equals(html)) //$NON-NLS-1$
170 return "\t"; //$NON-NLS-1$
172 if ("li".equals(html)) //$NON-NLS-1$
174 + "\t" + PHPUIMessages.getString("HTML2TextReader.dash"); //$NON-NLS-1$ //$NON-NLS-2$
176 if ("/b".equals(html)) { //$NON-NLS-1$
181 if ("p".equals(html)) { //$NON-NLS-1$
186 if ("br".equals(html)) //$NON-NLS-1$
189 if ("/p".equals(html)) { //$NON-NLS-1$
190 boolean inParagraph = fInParagraph;
191 fInParagraph = false;
192 return inParagraph ? EMPTY_STRING : LINE_DELIM;
195 if ("/h5".equals(html) || "/dt".equals(html)) { //$NON-NLS-1$ //$NON-NLS-2$
200 if ("/dd".equals(html)) //$NON-NLS-1$
207 * A '<' has been read. Process a html tag
209 private String processHTMLTag() throws IOException {
211 StringBuffer buf = new StringBuffer();
217 while (ch != -1 && ch != '>') {
218 buf.append(Character.toLowerCase((char) ch));
221 buf.append(Character.toLowerCase((char) ch));
223 while (ch != -1 && ch != '"') {
224 buf.append(Character.toLowerCase((char) ch));
230 return '<' + buf.toString();
237 int tagLen = buf.length();
238 // needs special treatment for comments
239 if ((tagLen >= 3 && "!--".equals(buf.substring(0, 3))) //$NON-NLS-1$
240 && !(tagLen >= 5 && "--!".equals(buf.substring(tagLen - 3)))) { //$NON-NLS-1$
241 // unfinished comment
248 return html2Text(buf.toString());
251 private String processPreformattedText(int c) {
252 if (c == '\r' || c == '\n')
257 private void unread(int ch) throws IOException {
258 ((PushbackReader) getReader()).unread(ch);
261 protected String entity2Text(String symbol) {
262 if (symbol.length() > 1 && symbol.charAt(0) == '#') {
265 if (symbol.charAt(1) == 'x') {
266 ch = Integer.parseInt(symbol.substring(2), 16);
268 ch = Integer.parseInt(symbol.substring(1), 10);
270 return EMPTY_STRING + (char) ch;
271 } catch (NumberFormatException e) {
274 String str = (String) fgEntityLookup.get(symbol);
279 return "&" + symbol; // not found //$NON-NLS-1$
283 * A '&' has been read. Process a entity
285 private String processEntity() throws IOException {
286 StringBuffer buf = new StringBuffer();
288 while (Character.isLetterOrDigit((char) ch) || ch == '#') {
289 buf.append((char) ch);
294 return entity2Text(buf.toString());
298 buf.append((char) ch);
299 return buf.toString();