KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > comment > HTMLEntity2JavaReader


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

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

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

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

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

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

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