001 package railo.commons.net; 002 003 import java.io.UnsupportedEncodingException; 004 005 import railo.commons.lang.StringUtil; 006 007 /** 008 * Utility class for HTML form encoding. This class contains static methods 009 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME 010 * format. For more information about HTML form encoding, consult the HTML 011 * <A HREF="http://www.w3.org/TR/html4/">specification</A>. 012 * 013 * <p> 014 * When encoding a String, the following rules apply: 015 * 016 * <p> 017 * <ul> 018 * <li>The alphanumeric characters "<code>a</code>" through 019 * "<code>z</code>", "<code>A</code>" through 020 * "<code>Z</code>" and "<code>0</code>" 021 * through "<code>9</code>" remain the same. 022 * <li>The special characters "<code>.</code>", 023 * "<code>-</code>", "<code>*</code>", and 024 * "<code>_</code>" remain the same. 025 * <li>The space character "<code> </code>" is 026 * converted into a plus sign "<code>+</code>". 027 * <li>All other characters are unsafe and are first converted into 028 * one or more bytes using some encoding scheme. Then each byte is 029 * represented by the 3-character string 030 * "<code>%<i>xy</i></code>", where <i>xy</i> is the 031 * two-digit hexadecimal representation of the byte. 032 * The recommended encoding scheme to use is UTF-8. However, 033 * for compatibility reasons, if an encoding is not specified, 034 * then the default encoding of the platform is used. 035 * </ul> 036 * 037 * <p> 038 * For example using UTF-8 as the encoding scheme the string "The 039 * string ü@foo-bar" would get converted to 040 * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character 041 * ü is encoded as two bytes C3 (hex) and BC (hex), and the 042 * character @ is encoded as one byte 40 (hex). 043 * 044 * @author Herb Jellinek 045 * @version 1.25, 12/03/01 046 * @since JDK1.0 047 */ 048 public class URLEncoder { 049 050 /** 051 * You can't call the constructor. 052 */ 053 private URLEncoder() { } 054 055 /** 056 * Translates a string into <code>x-www-form-urlencoded</code> 057 * format. This method uses the platform's default encoding 058 * as the encoding scheme to obtain the bytes for unsafe characters. 059 * 060 * @param s <code>String</code> to be translated. 061 * @deprecated The resulting string may vary depending on the platform's 062 * default encoding. Instead, use the encode(String,String) 063 * method to specify the encoding. 064 * @return the translated <code>String</code>. 065 */ 066 public static String encode(String s) { 067 s= java.net.URLEncoder.encode(s); 068 if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false); 069 return s; 070 } 071 072 public static String encode(String s, String enc) throws UnsupportedEncodingException { 073 s= java.net.URLEncoder.encode(s, enc); 074 if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false); 075 return s; 076 } 077 }