现在的位置: 首页 > 综合 > 正文

对HTML 通过GET 方法进行提交后,对信息进行解码JAVA类

2013年10月02日 ⁄ 综合 ⁄ 共 3303字 ⁄ 字号 评论关闭
package net.java2000.tools;

import java.util.HashMap;

/**
 * 替换HTMl里面的字符 e.g.: < > " å И 水
 * 
 * 
@author 赵学庆
 
*/

public class HTMLDecoder {

  
public static final HashMap<String, Character> charTable;

  
public static String decode(String s) {
    String t;
    Character ch;
    
int tmpPos, i;

    
int maxPos = s.length();
    StringBuffer sb 
= new StringBuffer(maxPos);
    
int curPos = 0;
    
while (curPos < maxPos) {
      
char c = s.charAt(curPos++);
      
if (c == '&'{
        tmpPos 
= curPos;
        
if (tmpPos < maxPos) {
          
char d = s.charAt(tmpPos++);
          
if (d == '#'{
            
if (tmpPos < maxPos) {
              d 
= s.charAt(tmpPos++);
              
if ((d == 'x'|| (d == 'X')) {
                
if (tmpPos < maxPos) {
                  d 
= s.charAt(tmpPos++);
                  
if (isHexDigit(d)) {
                    
while (tmpPos < maxPos) {
                      d 
= s.charAt(tmpPos++);
                      
if (!isHexDigit(d)) {
                        
if (d == ';'{
                          t 
= s.substring(curPos + 2, tmpPos - 1);
                          
try {
                            i 
= Integer.parseInt(t, 16);
                            
if ((i >= 0&& (i < 65536)) {
                              c 
= (char) i;
                              curPos 
= tmpPos;
                            }

                          }
 catch (NumberFormatException e) {
                          }

                        }

                        
break;
                      }

                    }

                  }

                }

              }
 else if (isDigit(d)) {
                
while (tmpPos < maxPos) {
                  d 
= s.charAt(tmpPos++);
                  
if (!isDigit(d)) {
                    
if (d == ';'{
                      t 
= s.substring(curPos + 1, tmpPos - 1);
                      
try {
                        i 
= Integer.parseInt(t);
                        
if ((i >= 0&& (i < 65536)) {
                          c 
= (char) i;
                          curPos 
= tmpPos;
                        }

                      }
 catch (NumberFormatException e) {
                      }

                    }

                    
break;
                  }

                }

              }

            }

          }
 else if (isLetter(d)) {
            
while (tmpPos < maxPos) {
              d 
= s.charAt(tmpPos++);
              
if (!isLetterOrDigit(d)) {
                
if (d == ';'{
                  t 
= s.substring(curPos, tmpPos - 1);
                  ch 
= (Character) charTable.get(t);
                  
if (ch != null{
                    c 
= ch.charValue();
                    curPos 
= tmpPos;
                  }

                }

                
break;
              }

            }

          }

        }

      }

      sb.append(c);
    }

    
return sb.toString();
  }


  
private static boolean isLetterOrDigit(char c) {
    
return isLetter(c) || isDigit(c);
  }


  
private static boolean isHexDigit(char c) {
    
return isHexLetter(c) || isDigit(c);
  }


  
private static boolean isLetter(char c) {
    
return ((c >= 'a'&& (c <= 'z')) || ((c >= 'A'&& (c <= 'Z'));
  }


  
private static boolean isHexLetter(char c) {
    
return ((c >= 'a'&& (c <= 'f')) || ((c >= 'A'&& (c <=

抱歉!评论已关闭.