Source Code Cross Referenced for HTML2TextReader.java in  » IDE-Eclipse » Eclipse-plug-in-development » org » eclipse » pde » internal » ui » editor » contentassist » display » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » Eclipse plug in development » org.eclipse.pde.internal.ui.editor.contentassist.display 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
w_w__w.j__a__va___2_s__.c_o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.