`
逆风的香1314
  • 浏览: 1398384 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
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) 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics