KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > comment > HTMLEntity2JavaReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.comment;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.eclipse.jdt.internal.ui.text.SubstitutionTextReader;
18
19
20 /**
21  * <code>SubstitutionTextReader</code> that will substitute plain text values
22  * for html entities encountered in the original text. Line breaks and
23  * whitespaces are preserved.
24  *
25  * @since 3.0
26  */

27 public class HTMLEntity2JavaReader extends SubstitutionTextReader {
28
29     /** The hardcoded entity map. */
30     private static final Map JavaDoc fgEntityLookup;
31     
32     static {
33         fgEntityLookup= new HashMap JavaDoc(7);
34         fgEntityLookup.put("lt", "<"); //$NON-NLS-1$ //$NON-NLS-2$
35
fgEntityLookup.put("gt", ">"); //$NON-NLS-1$ //$NON-NLS-2$
36
fgEntityLookup.put("nbsp", " "); //$NON-NLS-1$ //$NON-NLS-2$
37
fgEntityLookup.put("amp", "&"); //$NON-NLS-1$ //$NON-NLS-2$
38
fgEntityLookup.put("circ", "^"); //$NON-NLS-1$ //$NON-NLS-2$
39
fgEntityLookup.put("tilde", "~"); //$NON-NLS-2$ //$NON-NLS-1$
40
fgEntityLookup.put("quot", "\""); //$NON-NLS-1$ //$NON-NLS-2$
41
}
42
43     /**
44      * Creates a new instance that will read from <code>reader</code>
45      *
46      * @param reader the source reader
47      */

48     public HTMLEntity2JavaReader(Reader JavaDoc reader) {
49         super(reader);
50         setSkipWhitespace(false);
51     }
52
53     /*
54      * @see org.eclipse.jdt.internal.ui.text.SubstitutionTextReader#computeSubstitution(int)
55      */

56     protected String JavaDoc computeSubstitution(int c) throws IOException JavaDoc {
57         if (c == '&')
58             return processEntity();
59         return null;
60     }
61
62     /**
63      * Replaces an HTML entity body (without &amp; and ;) with its plain/text
64      * (or plain/java) counterpart.
65      *
66      * @param symbol the entity body to resolve
67      * @return the plain/text counterpart of <code>symbol</code>
68      */

69     protected String JavaDoc entity2Text(String JavaDoc symbol) {
70         if (symbol.length() > 1 && symbol.charAt(0) == '#') {
71             int ch;
72             try {
73                 if (symbol.charAt(1) == 'x') {
74                     ch= Integer.parseInt(symbol.substring(2), 16);
75                 } else {
76                     ch= Integer.parseInt(symbol.substring(1), 10);
77                 }
78                 return " " + (char) ch; //$NON-NLS-1$
79
} catch (NumberFormatException JavaDoc e) {
80             }
81         } else {
82             String JavaDoc str= (String JavaDoc) fgEntityLookup.get(symbol);
83             if (str != null) {
84                 return str;
85             }
86         }
87         return "&" + symbol; // not found //$NON-NLS-1$
88
}
89
90     /**
91      * Reads an HTML entity from the stream and returns its plain/text
92      * counterpart.
93      *
94      * @return an entity read from the stream, or the stream content.
95      * @throws IOException if the underlying reader throws one
96      */

97     private String JavaDoc processEntity() throws IOException JavaDoc {
98         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
99         int ch= nextChar();
100         while (Character.isLetterOrDigit((char) ch) || ch == '#') {
101             buf.append((char) ch);
102             ch= nextChar();
103         }
104         if (ch == ';')
105             return entity2Text(buf.toString());
106         buf.insert(0, '&');
107         if (ch != -1)
108             buf.append((char) ch);
109         return buf.toString();
110     }
111 }
112
Popular Tags