1 11 package org.eclipse.jdt.internal.ui.text.comment; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import org.eclipse.jdt.internal.ui.text.SubstitutionTextReader; 18 19 20 27 public class HTMLEntity2JavaReader extends SubstitutionTextReader { 28 29 30 private static final Map fgEntityLookup; 31 32 static { 33 fgEntityLookup= new HashMap (7); 34 fgEntityLookup.put("lt", "<"); fgEntityLookup.put("gt", ">"); fgEntityLookup.put("nbsp", " "); fgEntityLookup.put("amp", "&"); fgEntityLookup.put("circ", "^"); fgEntityLookup.put("tilde", "~"); fgEntityLookup.put("quot", "\""); } 42 43 48 public HTMLEntity2JavaReader(Reader reader) { 49 super(reader); 50 setSkipWhitespace(false); 51 } 52 53 56 protected String computeSubstitution(int c) throws IOException { 57 if (c == '&') 58 return processEntity(); 59 return null; 60 } 61 62 69 protected String entity2Text(String 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; } catch (NumberFormatException e) { 80 } 81 } else { 82 String str= (String ) fgEntityLookup.get(symbol); 83 if (str != null) { 84 return str; 85 } 86 } 87 return "&" + symbol; } 89 90 97 private String processEntity() throws IOException { 98 StringBuffer buf= new StringBuffer (); 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 |