001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.contentassist.display;
011:
012: import java.io.IOException;
013: import java.io.PushbackReader;
014: import java.io.Reader;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.custom.StyleRange;
022:
023: import org.eclipse.jface.text.TextPresentation;
024:
025: /**
026: * Reads the text contents from a reader of HTML contents and translates
027: * the tags or cut them out.
028: */
029: public class HTML2TextReader extends SubstitutionTextReader {
030:
031: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
032: private static final Map fgEntityLookup;
033: private static final Set fgTags;
034:
035: static {
036:
037: fgTags = new HashSet();
038: fgTags.add("b"); //$NON-NLS-1$
039: fgTags.add("br"); //$NON-NLS-1$
040: fgTags.add("br/"); //$NON-NLS-1$
041: fgTags.add("div"); //$NON-NLS-1$
042: fgTags.add("h1"); //$NON-NLS-1$
043: fgTags.add("h2"); //$NON-NLS-1$
044: fgTags.add("h3"); //$NON-NLS-1$
045: fgTags.add("h4"); //$NON-NLS-1$
046: fgTags.add("h5"); //$NON-NLS-1$
047: fgTags.add("p"); //$NON-NLS-1$
048: fgTags.add("dl"); //$NON-NLS-1$
049: fgTags.add("dt"); //$NON-NLS-1$
050: fgTags.add("dd"); //$NON-NLS-1$
051: fgTags.add("li"); //$NON-NLS-1$
052: fgTags.add("ul"); //$NON-NLS-1$
053: fgTags.add("pre"); //$NON-NLS-1$
054: fgTags.add("head"); //$NON-NLS-1$
055:
056: fgEntityLookup = new HashMap(7);
057: fgEntityLookup.put("lt", "<"); //$NON-NLS-1$ //$NON-NLS-2$
058: fgEntityLookup.put("gt", ">"); //$NON-NLS-1$ //$NON-NLS-2$
059: fgEntityLookup.put("nbsp", " "); //$NON-NLS-1$ //$NON-NLS-2$
060: fgEntityLookup.put("amp", "&"); //$NON-NLS-1$ //$NON-NLS-2$
061: fgEntityLookup.put("circ", "^"); //$NON-NLS-1$ //$NON-NLS-2$
062: fgEntityLookup.put("tilde", "~"); //$NON-NLS-2$ //$NON-NLS-1$
063: fgEntityLookup.put("quot", "\""); //$NON-NLS-1$ //$NON-NLS-2$
064: }
065:
066: private int fCounter = 0;
067: private TextPresentation fTextPresentation;
068: private int fBold = 0;
069: private int fStartOffset = -1;
070: private boolean fInParagraph = false;
071: private boolean fIsPreformattedText = false;
072: private boolean fIgnore = false;
073:
074: /**
075: * Transforms the HTML text from the reader to formatted text.
076: *
077: * @param reader the reader
078: * @param presentation If not <code>null</code>, formattings will be applied to
079: * the presentation.
080: */
081: public HTML2TextReader(Reader reader, TextPresentation presentation) {
082: super (new PushbackReader(reader));
083: fTextPresentation = presentation;
084: }
085:
086: public int read() throws IOException {
087: int c = super .read();
088: if (c != -1)
089: ++fCounter;
090: return c;
091: }
092:
093: protected void startBold() {
094: if (fBold == 0)
095: fStartOffset = fCounter;
096: ++fBold;
097: }
098:
099: protected void startPreformattedText() {
100: fIsPreformattedText = true;
101: setSkipWhitespace(false);
102: }
103:
104: protected void stopPreformattedText() {
105: fIsPreformattedText = false;
106: setSkipWhitespace(true);
107: }
108:
109: protected void stopBold() {
110: --fBold;
111: if (fBold == 0) {
112: if (fTextPresentation != null) {
113: fTextPresentation.addStyleRange(new StyleRange(
114: fStartOffset, fCounter - fStartOffset, null,
115: null, SWT.BOLD));
116: }
117: fStartOffset = -1;
118: }
119: }
120:
121: /*
122: * @see org.eclipse.jdt.internal.ui.text.SubstitutionTextReader#computeSubstitution(int)
123: */
124: protected String computeSubstitution(int c) throws IOException {
125:
126: if (c == '<')
127: return processHTMLTag();
128: else if (fIgnore)
129: return EMPTY_STRING;
130: else if (c == '&')
131: return processEntity();
132: else if (fIsPreformattedText)
133: return processPreformattedText(c);
134:
135: return null;
136: }
137:
138: private String html2Text(String html) {
139:
140: if (html == null || html.length() == 0)
141: return EMPTY_STRING;
142:
143: html = html.toLowerCase();
144:
145: String tag = html;
146: if ('/' == tag.charAt(0))
147: tag = tag.substring(1);
148:
149: if (!fgTags.contains(tag))
150: return EMPTY_STRING;
151:
152: if ("pre".equals(html)) { //$NON-NLS-1$
153: startPreformattedText();
154: return EMPTY_STRING;
155: }
156:
157: if ("/pre".equals(html)) { //$NON-NLS-1$
158: stopPreformattedText();
159: return EMPTY_STRING;
160: }
161:
162: if (fIsPreformattedText)
163: return EMPTY_STRING;
164:
165: if ("b".equals(html)) { //$NON-NLS-1$
166: startBold();
167: return EMPTY_STRING;
168: }
169:
170: if ((html.length() > 1 && html.charAt(0) == 'h' && Character
171: .isDigit(html.charAt(1)))
172: || "dt".equals(html)) { //$NON-NLS-1$
173: startBold();
174: return EMPTY_STRING;
175: }
176:
177: if ("dl".equals(html)) //$NON-NLS-1$
178: return LINE_DELIM;
179:
180: if ("dd".equals(html)) //$NON-NLS-1$
181: return "\t"; //$NON-NLS-1$
182:
183: if ("li".equals(html)) //$NON-NLS-1$
184: // FIXME: this hard-coded prefix does not work for RTL languages, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=91682
185: return LINE_DELIM + "\t- "; //$NON-NLS-1$
186:
187: if ("/b".equals(html)) { //$NON-NLS-1$
188: stopBold();
189: return EMPTY_STRING;
190: }
191:
192: if ("p".equals(html)) { //$NON-NLS-1$
193: fInParagraph = true;
194: return LINE_DELIM;
195: }
196:
197: if ("br".equals(html) || "br/".equals(html) || "div".equals(html)) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
198: return LINE_DELIM;
199:
200: if ("/p".equals(html)) { //$NON-NLS-1$
201: boolean inParagraph = fInParagraph;
202: fInParagraph = false;
203: return inParagraph ? EMPTY_STRING : LINE_DELIM;
204: }
205:
206: if ((html.startsWith("/h") && html.length() > 2 && Character.isDigit(html.charAt(2))) || "/dt".equals(html)) { //$NON-NLS-1$ //$NON-NLS-2$
207: stopBold();
208: return LINE_DELIM;
209: }
210:
211: if ("/dd".equals(html)) //$NON-NLS-1$
212: return LINE_DELIM;
213:
214: if ("head".equals(html)) { //$NON-NLS-1$
215: fIgnore = true;
216: return EMPTY_STRING;
217: }
218:
219: if ("/head".equals(html)) { //$NON-NLS-1$
220: fIgnore = false;
221: return EMPTY_STRING;
222: }
223:
224: return EMPTY_STRING;
225: }
226:
227: /*
228: * A '<' has been read. Process a html tag
229: */
230: private String processHTMLTag() throws IOException {
231:
232: StringBuffer buf = new StringBuffer();
233: int ch;
234: do {
235:
236: ch = nextChar();
237:
238: while (ch != -1 && ch != '>') {
239: buf.append(Character.toLowerCase((char) ch));
240: ch = nextChar();
241: if (ch == '"') {
242: buf.append(Character.toLowerCase((char) ch));
243: ch = nextChar();
244: while (ch != -1 && ch != '"') {
245: buf.append(Character.toLowerCase((char) ch));
246: ch = nextChar();
247: }
248: }
249: if (ch == '<') {
250: unread(ch);
251: return '<' + buf.toString();
252: }
253: }
254:
255: if (ch == -1)
256: return null;
257:
258: int tagLen = buf.length();
259: // needs special treatment for comments
260: if ((tagLen >= 3 && "!--".equals(buf.substring(0, 3))) //$NON-NLS-1$
261: && !(tagLen >= 5 && "--".equals(buf.substring(tagLen - 2)))) { //$NON-NLS-1$
262: // unfinished comment
263: buf.append(ch);
264: } else {
265: break;
266: }
267: } while (true);
268:
269: return html2Text(buf.toString());
270: }
271:
272: private String processPreformattedText(int c) {
273: if (c == '\r' || c == '\n')
274: fCounter++;
275: return null;
276: }
277:
278: private void unread(int ch) throws IOException {
279: ((PushbackReader) getReader()).unread(ch);
280: }
281:
282: protected String entity2Text(String symbol) {
283: if (symbol.length() > 1 && symbol.charAt(0) == '#') {
284: int ch;
285: try {
286: if (symbol.charAt(1) == 'x') {
287: ch = Integer.parseInt(symbol.substring(2), 16);
288: } else {
289: ch = Integer.parseInt(symbol.substring(1), 10);
290: }
291: return EMPTY_STRING + (char) ch;
292: } catch (NumberFormatException e) {
293: }
294: } else {
295: String str = (String) fgEntityLookup.get(symbol);
296: if (str != null) {
297: return str;
298: }
299: }
300: return "&" + symbol; // not found //$NON-NLS-1$
301: }
302:
303: /*
304: * A '&' has been read. Process a entity
305: */
306: private String processEntity() throws IOException {
307: StringBuffer buf = new StringBuffer();
308: int ch = nextChar();
309: while (Character.isLetterOrDigit((char) ch) || ch == '#') {
310: buf.append((char) ch);
311: ch = nextChar();
312: }
313:
314: if (ch == ';')
315: return entity2Text(buf.toString());
316:
317: buf.insert(0, '&');
318: if (ch != -1)
319: buf.append((char) ch);
320: return buf.toString();
321: }
322: }
|