001 package com.croftsoft.core.net; 002 003 /********************************************************************* 004 * 005 * Java 1.1 substitute for URLDecoder and URLEncoder. 006 * 007 * <P> 008 * 009 * The classes java.net.URLDecoder and java.net.URLEncoder were not 010 * available until Java 1.2. This class provides a substitute for 011 * those programs which must use the Java 1.1 API. 012 * 013 * <P> 014 * 015 * @see 016 * java.net.URLDecoder 017 * @see 018 * java.net.URLEncoder 019 * 020 * @author 021 * <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A> 022 * @version 023 * 2000-04-21 024 *********************************************************************/ 025 026 public final class URLCoder 027 ////////////////////////////////////////////////////////////////////// 028 ////////////////////////////////////////////////////////////////////// 029 { 030 031 private URLCoder ( ) { } 032 033 ////////////////////////////////////////////////////////////////////// 034 ////////////////////////////////////////////////////////////////////// 035 036 /********************************************************************* 037 * Displays the encoded value of the first argument. 038 *********************************************************************/ 039 public static void main ( String [ ] args ) 040 ////////////////////////////////////////////////////////////////////// 041 { 042 System.out.println ( encode ( args [ 0 ] ) ); 043 } 044 045 /********************************************************************* 046 * Performs the equivalent of java.net.URLDecoder.decode(). 047 *********************************************************************/ 048 public static String decode ( String s ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 if ( s == null ) 052 { 053 throw new IllegalArgumentException ( "null" ); 054 } 055 056 StringBuffer stringBuffer = new StringBuffer ( ); 057 058 int length = s.length ( ); 059 060 for ( int i = 0; i < length; i++ ) 061 { 062 char c = s.charAt ( i ); 063 064 if ( ( ( c >= 'a' ) && ( c <= 'z' ) ) 065 || ( ( c >= 'A' ) && ( c <= 'Z' ) ) 066 || ( ( c >= '0' ) && ( c <= '9' ) ) 067 || ( c == '.' ) 068 || ( c == '-' ) 069 || ( c == '*' ) 070 || ( c == '_' ) ) 071 { 072 stringBuffer.append ( c ); 073 } 074 else if ( c == '+' ) 075 { 076 stringBuffer.append ( ' ' ); 077 } 078 else if ( c == '%' ) 079 { 080 stringBuffer.append ( ( char ) 081 ( 16 * Character.digit ( s.charAt ( ++i ), 16 ) 082 + Character.digit ( s.charAt ( ++i ), 16 ) ) ); 083 } 084 else 085 { 086 stringBuffer.append ( c ); 087 } 088 } 089 090 return stringBuffer.toString ( ); 091 } 092 093 /********************************************************************* 094 * Performs the equivalent of java.net.URLEncoder.encode(). 095 *********************************************************************/ 096 public static String encode ( String s ) 097 ////////////////////////////////////////////////////////////////////// 098 { 099 if ( s == null ) 100 { 101 throw new IllegalArgumentException ( "null" ); 102 } 103 104 StringBuffer stringBuffer = new StringBuffer ( ); 105 106 int length = s.length ( ); 107 108 for ( int i = 0; i < length; i++ ) 109 { 110 char c = s.charAt ( i ); 111 112 if ( ( ( c >= 'a' ) && ( c <= 'z' ) ) 113 || ( ( c >= 'A' ) && ( c <= 'Z' ) ) 114 || ( ( c >= '0' ) && ( c <= '9' ) ) 115 || ( c == '.' ) 116 || ( c == '-' ) 117 || ( c == '*' ) 118 || ( c == '_' ) ) 119 { 120 stringBuffer.append ( c ); 121 } 122 else if ( c == ' ' ) 123 { 124 stringBuffer.append ( '+' ); 125 } 126 else 127 { 128 stringBuffer.append ( "%" + Integer.toHexString ( 129 ( ( int ) c ) & 0xFF ).toUpperCase ( ) ); 130 } 131 } 132 133 return stringBuffer.toString ( ); 134 } 135 136 ////////////////////////////////////////////////////////////////////// 137 ////////////////////////////////////////////////////////////////////// 138 }