HTMLDecoder,&#开头的编码换转成中文

HTMLDecoder,&#开头的编码换转成中文


HTMLDecoder,中文转换成&#开头的编码实现

  1. package test.com.gjob.services;
  2. import java.util.HashMap;  
  3. /** 
  4.  * 替换HTMl里面的字符 e.g.: < > " å И 水 
  5.  *  
  6.  * @author 老紫竹(laozizhu.com) 
  7.  */ 
  8. public class HTMLDecoder {  
  9.    
  10.     public static void main(String args[]){
  11.            String tt1 = "你好,我想给你说一个事情";
  12.           
  13.            System.out.println(HTMLDecoder.decode(tt1));
  14.         System.out.println(HTMLDecoder.decode("中国"));
  15.         System.out.println(HTMLDecoder.compact("\u4F60\u597D"));
  16.        
  17.     }
  18.  
  19.   public static final HashMap<String, Character> charTable;  
  20.  
  21.   public static String decode(String s) {  
  22.     String t;  
  23.     Character ch;  
  24.     int tmpPos, i;  
  25.  
  26.     int maxPos = s.length();  
  27.     StringBuffer sb = new StringBuffer(maxPos);  
  28.     int curPos = 0;  
  29.     while (curPos < maxPos) {  
  30.       char c = s.charAt(curPos++);  
  31.       if (c == '&') {  
  32.         tmpPos = curPos;  
  33.         if (tmpPos < maxPos) {  
  34.           char d = s.charAt(tmpPos++);  
  35.           if (d == '#') {  
  36.             if (tmpPos < maxPos) {  
  37.               d = s.charAt(tmpPos++);  
  38.               if ((d == 'x') || (d == 'X')) {  
  39.                 if (tmpPos < maxPos) {  
  40.                   d = s.charAt(tmpPos++);  
  41.                   if (isHexDigit(d)) {  
  42.                     while (tmpPos < maxPos) {  
  43.                       d = s.charAt(tmpPos++);  
  44.                       if (!isHexDigit(d)) {  
  45.                         if (d == ';') {  
  46.                           t = s.substring(curPos + 2, tmpPos - 1);  
  47.                           try {  
  48.                             i = Integer.parseInt(t, 16);  
  49.                             if ((i >= 0) && (i < 65536)) {  
  50.                               c = (char) i;  
  51.                               curPos = tmpPos;  
  52.                             }  
  53.                           } catch (NumberFormatException e) {  
  54.                           }  
  55.                         }  
  56.                         break;  
  57.                       }  
  58.                     }  
  59.                   }  
  60.                 }  
  61.               } else if (isDigit(d)) {  
  62.                 while (tmpPos < maxPos) {  
  63.                   d = s.charAt(tmpPos++);  
  64.                   if (!isDigit(d)) {  
  65.                     if (d == ';') {  
  66.                       t = s.substring(curPos + 1, tmpPos - 1);  
  67.                       try {  
  68.                         i = Integer.parseInt(t);  
  69.                         if ((i >= 0) && (i < 65536)) {  
  70.                           c = (char) i;  
  71.                           curPos = tmpPos;  
  72.                         }  
  73.                       } catch (NumberFormatException e) {  
  74.                       }  
  75.                     }  
  76.                     break;  
  77.                   }  
  78.                 }  
  79.               }  
  80.             }  
  81.           } else if (isLetter(d)) {  
  82.             while (tmpPos < maxPos) {  
  83.               d = s.charAt(tmpPos++);  
  84.               if (!isLetterOrDigit(d)) {  
  85.                 if (d == ';') {  
  86.                   t = s.substring(curPos, tmpPos - 1);  
  87.                   ch = (Character) charTable.get(t);  
  88.                   if (ch != null) {  
  89.                     c = ch.charValue();  
  90.                     curPos = tmpPos;  
  91.                   }  
  92.                 }  
  93.                 break;  
  94.               }  
  95.             }  
  96.           }  
  97.         }  
  98.       }  
  99.       sb.append(c);  
  100.     }  
  101.     return sb.toString();  
  102.   }  
  103.  
  104.   private static boolean isLetterOrDigit(char c) {  
  105.     return isLetter(c) || isDigit(c);  
  106.   }  
  107.  
  108.   private static boolean isHexDigit(char c) {  
  109.     return isHexLetter(c) || isDigit(c);  
  110.   }  
  111.  
  112.   private static boolean isLetter(char c) {  
  113.     return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));  
  114.   }  
  115.  
  116.   private static boolean isHexLetter(char c) {  
  117.     return ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));  
  118.   }  
  119.  
  120.   private static boolean isDigit(char c) {  
  121.     return (c >= '0') && (c <= '9');  
  122.   }  
  123.  
  124.   public static String compact(String s) {  
  125.     int maxPos = s.length();  
  126.     StringBuffer sb = new StringBuffer(maxPos);  
  127.     int curPos = 0;  
  128.     while (curPos < maxPos) {  
  129.       char c = s.charAt(curPos++);  
  130.       if (isWhitespace(c)) {  
  131.         while ((curPos < maxPos) && isWhitespace(s.charAt(curPos))) {  
  132.           curPos++;  
  133.         }  
  134.         c = '\u0020';  
  135.       }  
  136.       sb.append(c);  
  137.     }  
  138.     return sb.toString();  
  139.   }  
  140.  
  141.   // HTML is very particular about what constitutes white space.  
  142.   public static boolean isWhitespace(char ch) {  
  143.     return (ch == '\u0020') || (ch == '\r') || (ch == '\n') || (ch == '\u0009') || (ch == '\u000c') || (ch == '\u200b');  
  144.   }  
  145.  
  146.   static {  
  147.     charTable = new HashMap<String, Character>();  
  148.     charTable.put("quot", new Character((char) 34));  
  149.     charTable.put("amp", new Character((char) 38));  
  150.     charTable.put("apos", new Character((char) 39));  
  151.     charTable.put("lt", new Character((char) 60));  
  152.     charTable.put("gt", new Character((char) 62));  
  153.     charTable.put("nbsp", new Character((char) 160));  
  154.     charTable.put("iexcl", new Character((char) 161));  
  155.     charTable.put("cent", new Character((char) 162));  
  156.     charTable.put("pound", new Character((char) 163));  
  157.     charTable.put("curren", new Character((char) 164));  
  158.     charTable.put("yen", new Character((char) 165));  
  159.     charTable.put("brvbar", new Character((char) 166));  
  160.     charTable.put("sect", new Character((char) 167));  
  161.     charTable.put("uml", new Character((char) 168));  
  162.     charTable.put("copy", new Character((char) 169));  
  163.     charTable.put("ordf", new Character((char) 170));  
  164.     charTable.put("laquo", new Character((char) 171));  
  165.     charTable.put("not", new Character((char) 172));  
  166.     charTable.put("shy", new Character((char) 173));  
  167.     charTable.put("reg", new Character((char) 174));  
  168.     charTable.put("macr", new Character((char) 175));  
  169.     charTable.put("deg", new Character((char) 176));  
  170.     charTable.put("plusmn", new Character((char) 177));  
  171.     charTable.put("sup2", new Character((char) 178));  
  172.     charTable.put("sup3", new Character((char) 179));  
  173.     charTable.put("acute", new Character((char) 180));  
  174.     charTable.put("micro", new Character((char) 181));  
  175.     charTable.put("para", new Character((char) 182));  
  176.     charTable.put("middot", new Character((char) 183));  
  177.     charTable.put("cedil", new Character((char) 184));  
  178.     charTable.put("sup1", new Character((char) 185));  
  179.     charTable.put("ordm", new Character((char) 186));  
  180.     charTable.put("raquo", new Character((char) 187));  
  181.     charTable.put("frac14", new Character((char) 188));  
  182.     charTable.put("frac12", new Character((char) 189));  
  183.     charTable.put("frac34", new Character((char) 190));  
  184.     charTable.put("iquest", new Character((char) 191));  
  185.     charTable.put("Agrave", new Character((char) 192));  
  186.     charTable.put("Aacute", new Character((char) 193));  
  187.     charTable.put("Acirc", new Character((char) 194));  
  188.     charTable.put("Atilde", new Character((char) 195));  
  189.     charTable.put("Auml", new Character((char) 196));  
  190.     charTable.put("Aring", new Character((char) 197));  
  191.     charTable.put("AElig", new Character((char) 198));  
  192.     charTable.put("Ccedil", new Character((char) 199));  
  193.     charTable.put("Egrave", new Character((char) 200));  
  194.     charTable.put("Eacute", new Character((char) 201));  
  195.     charTable.put("Ecirc", new Character((char) 202));  
  196.     charTable.put("Euml", new Character((char) 203));  
  197.     charTable.put("Igrave", new Character((char) 204));  
  198.     charTable.put("Iacute", new Character((char) 205));  
  199.     charTable.put("Icirc", new Character((char) 206));  
  200.     charTable.put("Iuml", new Character((char) 207));  
  201.     charTable.put("ETH", new Character((char) 208));  
  202.     charTable.put("Ntilde", new Character((char) 209));  
  203.     charTable.put("Ograve", new Character((char) 210));  
  204.     charTable.put("Oacute", new Character((char) 211));  
  205.     charTable.put("Ocirc", new Character((char) 212));  
  206.     charTable.put("Otilde", new Character((char) 213));  
  207.     charTable.put("Ouml", new Character((char) 214));  
  208.     charTable.put("times", new Character((char) 215));  
  209.     charTable.put("Oslash", new Character((char) 216));  
  210.     charTable.put("Ugrave", new Character((char) 217));  
  211.     charTable.put("Uacute", new Character((char) 218));  
  212.     charTable.put("Ucirc", new Character((char) 219));  
  213.     charTable.put("Uuml", new Character((char) 220));  
  214.     charTable.put("Yacute", new Character((char) 221));  
  215.     charTable.put("THORN", new Character((char) 222));  
  216.     charTable.put("szlig", new Character((char) 223));  
  217.     charTable.put("agrave", new Character((char) 224));  
  218.     charTable.put("aacute", new Character((char) 225));  
  219.     charTable.put("acirc", new Character((char) 226));  
  220.     charTable.put("atilde", new Character((char) 227));  
  221.     charTable.put("auml", new Character((char) 228));  
  222.     charTable.put("aring", new Character((char) 229));  
  223.     charTable.put("aelig", new Character((char) 230));  
  224.     charTable.put("ccedil", new Character((char) 231));  
  225.     charTable.put("egrave", new Character((char) 232));  
  226.     charTable.put("eacute", new Character((char) 233));  
  227.     charTable.put("ecirc", new Character((char) 234));  
  228.     charTable.put("euml", new Character((char) 235));  
  229.     charTable.put("igrave", new Character((char) 236));  
  230.     charTable.put("iacute", new Character((char) 237));  
  231.     charTable.put("icirc", new Character((char) 238));  
  232.     charTable.put("iuml", new Character((char) 239));  
  233.     charTable.put("eth", new Character((char) 240));  
  234.     charTable.put("ntilde", new Character((char) 241));  
  235.     charTable.put("ograve", new Character((char) 242));  
  236.     charTable.put("oacute", new Character((char) 243));  
  237.     charTable.put("ocirc", new Character((char) 244));  
  238.     charTable.put("otilde", new Character((char) 245));  
  239.     charTable.put("ouml", new Character((char) 246));  
  240.     charTable.put("divide", new Character((char) 247));  
  241.     charTable.put("oslash", new Character((char) 248));  
  242.     charTable.put("ugrave", new Character((char) 249));  
  243.     charTable.put("uacute", new Character((char) 250));  
  244.     charTable.put("ucirc", new Character((char) 251));  
  245.     charTable.put("uuml", new Character((char) 252));  
  246.     charTable.put("yacute", new Character((char) 253));  
  247.     charTable.put("thorn", new Character((char) 254));  
  248.     charTable.put("yuml", new Character((char) 255));  
  249.     charTable.put("OElig", new Character((char) 338));  
  250.     charTable.put("oelig", new Character((char) 339));  
  251.     charTable.put("Scaron", new Character((char) 352));  
  252.     charTable.put("scaron", new Character((char) 353));  
  253.     charTable.put("fnof", new Character((char) 402));  
  254.     charTable.put("circ", new Character((char) 710));  
  255.     charTable.put("tilde", new Character((char) 732));  
  256.     charTable.put("Alpha", new Character((char) 913));  
  257.     charTable.put("Beta", new Character((char) 914));  
  258.     charTable.put("Gamma", new Character((char) 915));  
  259.     charTable.put("Delta", new Character((char) 916));  
  260.     charTable.put("Epsilon", new Character((char) 917));  
  261.     charTable.put("Zeta", new Character((char) 918));  
  262.     charTable.put("Eta", new Character((char) 919));  
  263.     charTable.put("Theta", new Character((char) 920));  
  264.     charTable.put("Iota", new Character((char) 921));  
  265.     charTable.put("Kappa", new Character((char) 922));  
  266.     charTable.put("Lambda", new Character((char) 923));  
  267.     charTable.put("Mu", new Character((char) 924));  
  268.     charTable.put("Nu", new Character((char) 925));  
  269.     charTable.put("Xi", new Character((char) 926));  
  270.     charTable.put("Omicron", new Character((char) 927));  
  271.     charTable.put("Pi", new Character((char) 928));  
  272.     charTable.put("Rho", new Character((char) 929));  
  273.     charTable.put("Sigma", new Character((char) 931));  
  274.     charTable.put("Tau", new Character((char) 932));  
  275.     charTable.put("Upsilon", new Character((char) 933));  
  276.     charTable.put("Phi", new Character((char) 934));  
  277.     charTable.put("Chi", new Character((char) 935));  
  278.     charTable.put("Psi", new Character((char) 936));  
  279.     charTable.put("Omega", new Character((char) 937));  
  280.     charTable.put("alpha", new Character((char) 945));  
  281.     charTable.put("beta", new Character((char) 946));  
  282.     charTable.put("gamma", new Character((char) 947));  
  283.     charTable.put("delta", new Character((char) 948));  
  284.     charTable.put("epsilon", new Character((char) 949));  
  285.     charTable.put("zeta", new Character((char) 950));  
  286.     charTable.put("eta", new Character((char) 951));  
  287.     charTable.put("theta", new Character((char) 952));  
  288.     charTable.put("iota", new Character((char) 953));  
  289.     charTable.put("kappa", new Character((char) 954));  
  290.     charTable.put("lambda", new Character((char) 955));  
  291.     charTable.put("mu", new Character((char) 956));  
  292.     charTable.put("nu", new Character((char) 957));  
  293.     charTable.put("xi", new Character((char) 958));  
  294.     charTable.put("omicron", new Character((char) 959));  
  295.     charTable.put("pi", new Character((char) 960));  
  296.     charTable.put("rho", new Character((char) 961));  
  297.     charTable.put("sigmaf", new Character((char) 962));  
  298.     charTable.put("sigma", new Character((char) 963));  
  299.     charTable.put("tau", new Character((char) 964));  
  300.     charTable.put("upsilon", new Character((char) 965));  
  301.     charTable.put("phi", new Character((char) 966));  
  302.     charTable.put("chi", new Character((char) 967));  
  303.     charTable.put("psi", new Character((char) 968));  
  304.     charTable.put("omega", new Character((char) 969));  
  305.     charTable.put("thetasym", new Character((char) 977));  
  306.     charTable.put("upsih", new Character((char) 978));  
  307.     charTable.put("piv", new Character((char) 982));  
  308.     charTable.put("ensp", new Character((char) 8194));  
  309.     charTable.put("emsp", new Character((char) 8195));  
  310.     charTable.put("thinsp", new Character((char) 8201));  
  311.     charTable.put("zwnj", new Character((char) 8204));  
  312.     charTable.put("zwj", new Character((char) 8205));  
  313.     charTable.put("lrm", new Character((char) 8206));  
  314.     charTable.put("rlm", new Character((char) 8207));  
  315.     charTable.put("ndash", new Character((char) 8211));  
  316.     charTable.put("mdash", new Character((char) 8212));  
  317.     charTable.put("lsquo", new Character((char) 8216));  
  318.     charTable.put("rsquo", new Character((char) 8217));  
  319.     charTable.put("sbquo", new Character((char) 8218));  
  320.     charTable.put("ldquo", new Character((char) 8220));  
  321.     charTable.put("rdquo", new Character((char) 8221));  
  322.     charTable.put("bdquo", new Character((char) 8222));  
  323.     charTable.put("dagger", new Character((char) 8224));  
  324.     charTable.put("Dagger", new Character((char) 8225));  
  325.     charTable.put("bull", new Character((char) 8226));  
  326.     charTable.put("hellip", new Character((char) 8230));  
  327.     charTable.put("permil", new Character((char) 8240));  
  328.     charTable.put("prime", new Character((char) 8242));  
  329.     charTable.put("Prime", new Character((char) 8243));  
  330.     charTable.put("lsaquo", new Character((char) 8249));  
  331.     charTable.put("rsaquo", new Character((char) 8250));  
  332.     charTable.put("oline", new Character((char) 8254));  
  333.     charTable.put("frasl", new Character((char) 8260));  
  334.     charTable.put("euro", new Character((char) 8364));  
  335.     charTable.put("image", new Character((char) 8465));  
  336.     charTable.put("weierp", new Character((char) 8472));  
  337.     charTable.put("real", new Character((char) 8476));  
  338.     charTable.put("trade", new Character((char) 8482));  
  339.     charTable.put("alefsym", new Character((char) 8501));  
  340.     charTable.put("larr", new Character((char) 8592));  
  341.     charTable.put("uarr", new Character((char) 8593));  
  342.     charTable.put("rarr", new Character((char) 8594));  
  343.     charTable.put("darr", new Character((char) 8595));  
  344.     charTable.put("harr", new Character((char) 8596));  
  345.     charTable.put("crarr", new Character((char) 8629));  
  346.     charTable.put("lArr", new Character((char) 8656));  
  347.     charTable.put("uArr", new Character((char) 8657));  
  348.     charTable.put("rArr", new Character((char) 8658));  
  349.     charTable.put("dArr", new Character((char) 8659));  
  350.     charTable.put("hArr", new Character((char) 8660));  
  351.     charTable.put("forall", new Character((char) 8704));  
  352.     charTable.put("part", new Character((char) 8706));  
  353.     charTable.put("exist", new Character((char) 8707));  
  354.     charTable.put("empty", new Character((char) 8709));  
  355.     charTable.put("nabla", new Character((char) 8711));  
  356.     charTable.put("isin", new Character((char) 8712));  
  357.     charTable.put("notin", new Character((char) 8713));  
  358.     charTable.put("ni", new Character((char) 8715));  
  359.     charTable.put("prod", new Character((char) 8719));  
  360.     charTable.put("sum", new Character((char) 8721));  
  361.     charTable.put("minus", new Character((char) 8722));  
  362.     charTable.put("lowast", new Character((char) 8727));  
  363.     charTable.put("radic", new Character((char) 8730));  
  364.     charTable.put("prop", new Character((char) 8733));  
  365.     charTable.put("infin", new Character((char) 8734));  
  366.     charTable.put("ang", new Character((char) 8736));  
  367.     charTable.put("and", new Character((char) 8743));  
  368.     charTable.put("or", new Character((char) 8744));  
  369.     charTable.put("cap", new Character((char) 8745));  
  370.     charTable.put("cup", new Character((char) 8746));  
  371.     charTable.put("int", new Character((char) 8747));  
  372.     charTable.put("there4", new Character((char) 8756));  
  373.     charTable.put("sim", new Character((char) 8764));  
  374.     charTable.put("cong", new Character((char) 8773));  
  375.     charTable.put("asymp", new Character((char) 8776));  
  376.     charTable.put("ne", new Character((char) 8800));  
  377.     charTable.put("equiv", new Character((char) 8801));  
  378.     charTable.put("le", new Character((char) 8804));  
  379.     charTable.put("ge", new Character((char) 8805));  
  380.     charTable.put("sub", new Character((char) 8834));  
  381.     charTable.put("sup", new Character((char) 8835));  
  382.     charTable.put("nsub", new Character((char) 8836));  
  383.     charTable.put("sube", new Character((char) 8838));  
  384.     charTable.put("supe", new Character((char) 8839));  
  385.     charTable.put("oplus", new Character((char) 8853));  
  386.     charTable.put("otimes", new Character((char) 8855));  
  387.     charTable.put("perp", new Character((char) 8869));  
  388.     charTable.put("sdot", new Character((char) 8901));  
  389.     charTable.put("lceil", new Character((char) 8968));  
  390.     charTable.put("rceil", new Character((char) 8969));  
  391.     charTable.put("lfloor", new Character((char) 8970));  
  392.     charTable.put("rfloor", new Character((char) 8971));  
  393.     charTable.put("lang", new Character((char) 9001));  
  394.     charTable.put("rang", new Character((char) 9002));  
  395.     charTable.put("loz", new Character((char) 9674));  
  396.     charTable.put("spades", new Character((char) 9824));  
  397.     charTable.put("clubs", new Character((char) 9827));  
  398.     charTable.put("hearts", new Character((char) 9829));  
  399.     charTable.put("diams", new Character((char) 9830));  
  400.   }  
  401. }

你可能感兴趣的:(HTMLDecoder,&#开头的编码换转成中文)