1 12 package org.eclipse.jdt.internal.formatter.comment; 13 14 import java.io.IOException ; 15 import java.io.Reader ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 20 21 28 public class HTMLEntity2JavaReader extends SubstitutionTextReader { 29 30 31 private static final Map fgEntityLookup; 32 33 static { 34 fgEntityLookup= new HashMap (7); 35 fgEntityLookup.put("lt", "<"); fgEntityLookup.put("gt", ">"); fgEntityLookup.put("nbsp", " "); fgEntityLookup.put("amp", "&"); fgEntityLookup.put("circ", "^"); fgEntityLookup.put("tilde", "~"); fgEntityLookup.put("quot", "\""); } 43 44 49 public HTMLEntity2JavaReader(Reader reader) { 50 super(reader); 51 setSkipWhitespace(false); 52 } 53 54 57 protected String computeSubstitution(int c) throws IOException { 58 if (c == '&') 59 return processEntity(); 60 return null; 61 } 62 63 70 protected String entity2Text(String 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 e) { 81 } 83 } else { 84 String str= (String ) fgEntityLookup.get(symbol); 85 if (str != null) { 86 return str; 87 } 88 } 89 return "&" + symbol; } 91 92 99 private String processEntity() throws IOException { 100 StringBuffer buf= new StringBuffer (); 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 |