2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Utility class for HTML form encoding. This class contains static methods 0N/A * format. For more information about HTML form encoding, consult the HTML 0N/A * When encoding a String, the following rules apply: 0N/A * <li>The alphanumeric characters "<code>a</code>" through 0N/A * "<code>z</code>", "<code>A</code>" through 0N/A * "<code>Z</code>" and "<code>0</code>" 0N/A * through "<code>9</code>" remain the same. 0N/A * <li>The special characters "<code>.</code>", 0N/A * "<code>-</code>", "<code>*</code>", and 0N/A * "<code>_</code>" remain the same. 0N/A * <li>The space character "<code> </code>" is 0N/A * converted into a plus sign "<code>+</code>". 0N/A * <li>All other characters are unsafe and are first converted into 0N/A * one or more bytes using some encoding scheme. Then each byte is 0N/A * represented by the 3-character string 0N/A * "<code>%<i>xy</i></code>", where <i>xy</i> is the 0N/A * two-digit hexadecimal representation of the byte. 0N/A * The recommended encoding scheme to use is UTF-8. However, 0N/A * for compatibility reasons, if an encoding is not specified, 0N/A * then the default encoding of the platform is used. 0N/A * For example using UTF-8 as the encoding scheme the string "The 0N/A * string ü@foo-bar" would get converted to 0N/A * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character 0N/A * ü is encoded as two bytes C3 (hex) and BC (hex), and the 0N/A * character @ is encoded as one byte 40 (hex). 0N/A * @author Herb Jellinek 0N/A /* The list of characters that are not encoded has been 0N/A * determined as follows: 0N/A * Data characters that are allowed in a URI but do not have a 0N/A * reserved purpose are called unreserved. These include upper 0N/A * and lower case letters, decimal digits, and a limited set of 0N/A * punctuation marks and symbols. 0N/A * unreserved = alphanum | mark 0N/A * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" 0N/A * Unreserved characters can be escaped without changing the 0N/A * semantics of the URI, but this should not be done unless the 0N/A * URI is being used in a context that does not allow the 0N/A * unescaped character to appear. 0N/A * It appears that both Netscape and Internet Explorer escape 0N/A * all special characters from this list with the exception 0N/A * of "-", "_", ".", "*". While it is not clear why they are 0N/A * escaping the other characters, perhaps it is safest to 0N/A * assume that there might be contexts in which the others 0N/A * are unsafe if not escaped. Therefore, we will use the same 0N/A * list. It is also noteworthy that this is consistent with 0N/A * O'Reilly's "HTML: The Definitive Guide" (page 164). 0N/A * As a last note, Intenet Explorer does not encode the "@" 0N/A * character which is clearly not unreserved according to the 0N/A * RFC. We are being consistent with the RFC in this matter, 0N/A for (i =
'a'; i <=
'z'; i++) {
0N/A for (i =
'A'; i <=
'Z'; i++) {
0N/A for (i =
'0'; i <=
'9'; i++) {
0N/A * in the encode() method */ 0N/A * You can't call the constructor. 0N/A * Translates a string into <code>x-www-form-urlencoded</code> 0N/A * format. This method uses the platform's default encoding 0N/A * as the encoding scheme to obtain the bytes for unsafe characters. 0N/A * @param s <code>String</code> to be translated. 0N/A * @deprecated The resulting string may vary depending on the platform's 0N/A * default encoding. Instead, use the encode(String,String) 0N/A * method to specify the encoding. 0N/A * @return the translated <code>String</code>. 0N/A // The system should always have the platform default 0N/A * format using a specific encoding scheme. This method uses the 0N/A * supplied encoding scheme to obtain the bytes for unsafe 0N/A * <em><strong>Note:</strong> The <a href= 0N/A * World Wide Web Consortium Recommendation</a> states that 0N/A * UTF-8 should be used. Not doing so may introduce 0N/A * incompatibilites.</em> 0N/A * @param s <code>String</code> to be translated. 0N/A * @param enc The name of a supported 0N/A * @return the translated <code>String</code>. 0N/A * @exception UnsupportedEncodingException 0N/A * If the named encoding is not supported 0N/A * @see URLDecoder#decode(java.lang.String, java.lang.String) 0N/A //System.out.println("Examining character: " + c); 0N/A //System.out.println("Storing: " + c); 0N/A // convert to external encoding before hex conversion 0N/A * If this character represents the start of a Unicode 0N/A * surrogate pair, then pass in two characters. It's not 0N/A * clear what should be done if a bytes reserved in the 0N/A * surrogate pairs range occurs outside of a legal 0N/A * surrogate pair. For now, just treat it as if it were 0N/A * any other character. 0N/A if (c >=
0xD800 && c <=
0xDBFF) {
0N/A System.out.println(Integer.toHexString(c) 0N/A + " is high surrogate"); 0N/A System.out.println("\tExamining " 0N/A + Integer.toHexString(d)); 0N/A if (d >=
0xDC00 && d <=
0xDFFF) {
0N/A System.out.println("\t" 0N/A + Integer.toHexString(d) 0N/A + " is low surrogate"); 0N/A // converting to use uppercase letter as part of 0N/A // the hex value if ch is a letter.