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 &quot;<code>a</code>&quot; through
019     *     &quot;<code>z</code>&quot;, &quot;<code>A</code>&quot; through
020     *     &quot;<code>Z</code>&quot; and &quot;<code>0</code>&quot; 
021     *     through &quot;<code>9</code>&quot; remain the same.
022     * <li>The special characters &quot;<code>.</code>&quot;,
023     *     &quot;<code>-</code>&quot;, &quot;<code>*</code>&quot;, and
024     *     &quot;<code>_</code>&quot; remain the same. 
025     * <li>The space character &quot;<code>&nbsp;</code>&quot; is
026     *     converted into a plus sign &quot;<code>+</code>&quot;.
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     *     &quot;<code>%<i>xy</i></code>&quot;, 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 &quot;The
039     * string &#252;@foo-bar&quot; would get converted to
040     * &quot;The+string+%C3%BC%40foo-bar&quot; because in UTF-8 the character
041     * &#252; 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    }