Java version of the ecape unecape function and [to]

  Java version of the escape and unescape function [to] 

  Class EscapeUnescape 
  ( 
  Public static String escape (String src) 
  ( 
  Int i; 
  Char j; 
  StringBuffer tmp = new StringBuffer (); 
  Tmp.ensureCapacity (src.length () * 6); 

  For (i = 0; i <src.length (); i + +) 
  ( 

  J = src.charAt (i); 

  If (Character.isDigit (j) | | Character.isLowerCase (j) | | Character.isUpperCase (j)) 
  Tmp.append (j); 
  Else 
  If (j <256) 
  ( 
  Tmp.append ( "%"); 
  If (j <16) 
  Tmp.append ( "0"); 
  Tmp.append (Integer.toString (j, 16)); 
  ) 
  Else 
  ( 
  Tmp.append ( "% u"); 
  Tmp.append (Integer.toString (j, 16)); 
  ) 
  ) 
  Return tmp.toString (); 
  ) 

  Public static String unescape (String src) 
  ( 
  StringBuffer tmp = new StringBuffer (); 
  Tmp.ensureCapacity (src.length ()); 
  Int lastPos = 0, pos = 0; 
  Char ch; 
  While (lastPos <src.length ()) 
  ( 
  Pos = src.indexOf ("%", lastPos); 
  If (pos == lastPos) 
  ( 
  If (src.charAt (pos + 1) == 'u') 
  ( 
  Ch = (char) Integer.parseInt (src.substring (+2 pos, pos +6), 16); 
  Tmp.append (ch); 
  LastPos +6 = pos; 
  ) 
  Else 
  ( 
  Ch = (char) Integer.parseInt (src.substring (+1 pos, pos +3), 16); 
  Tmp.append (ch); 
  LastPos +3 = pos; 
  ) 
  ) 
  Else 
  ( 
  If (pos == -1) 
  ( 
  Tmp.append (src.substring (lastPos)); 
  LastPos = src.length (); 
  ) 
  Else 
  ( 
  Tmp.append (src.substring (lastPos, pos)); 
  LastPos = pos; 
  ) 
  ) 
  ) 
  Return tmp.toString (); 
  ) 

  Public static void main (String [] args) 
  ( 
  String tmp ="<;'][{} \ "> ~!@#$%^&*()_+| \ \ =-,./?><;'][{} \" "; 
  System.out.println ( "testing escape:" + tmp); 
  Tmp = escape (tmp); 
  System.out.println (tmp); 
  System.out.println ( "testing unescape:" + tmp); 
  System.out.println (unescape (tmp)); 
  ) 
  ) 

  Posted on 2005-12-12 15:42 on the wind reading (133) Comments (0) edit their collections quoted Category: java 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply