KickJava   Java API By Example, From Geeks To Geeks.

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


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 197169 and complementary fix for 109636
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 html entities for
23  * html symbols encountered in the original text. Line breaks and whitespaces
24  * are preserved.
25  *
26  * @since 3.0
27  */

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

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

60     protected String JavaDoc computeSubstitution(int c) throws IOException JavaDoc {
61         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
62         // Accumulate *s into the buffer until we see something other than *.
63
while (c == '*') {
64             this.bits &= ~BEGIN_LINE;
65             c = nextChar();
66             buf.append('*');
67         }
68         if (c == -1)
69             // Snippet must have ended with *s. Just return them.
70
return buf.toString();
71         if (c == '/' && buf.length() > 0) {
72             /*
73              * Translate a * that precedes a / to &#42; so it isn't
74              * misinterpreted as the end of the Javadoc comment that contains
75              * the code we are formatting.
76              * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=109636
77              */

78             buf.setLength(buf.length() - 1);
79             buf.append("&#42;/"); //$NON-NLS-1$
80
} else if (c == '@' && (this.bits & BEGIN_LINE) != 0) {
81             /*
82              * When @ is first on a line, translate it to &#064; so it isn't
83              * misinterpreted as a Javadoc tag.
84              * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=197169
85              */

86             buf.append("&#064;"); //$NON-NLS-1$
87
} else {
88             /*
89              * Ordinary processing. If the character needs an entity in HTML,
90              * add the entity, otherwise add the character.
91              */

92             String JavaDoc entity = (String JavaDoc) fgEntityLookup.get(String.valueOf((char) c));
93             if (entity != null)
94                 buf.append(entity);
95             else
96                 buf.append((char) c);
97         }
98         // Update bits for the benefit of the next character.
99
if (c == '\n' || c == '\r') {
100             this.bits |= BEGIN_LINE;
101         } else if (!ScannerHelper.isWhitespace((char) c)) {
102             this.bits &= ~BEGIN_LINE;
103         }
104         return buf.toString();
105     }
106 }
107
Popular Tags