source: trunk/gcc/libjava/java/net/URLEncoder.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.4 KB
Line 
1/* URLEncoder.java -- Class to convert strings to a properly encoded URL
2 Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.net;
39
40import java.io.UnsupportedEncodingException;
41
42/*
43 * Written using on-line Java Platform 1.2/1.4 API Specification, as well
44 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
45 * Status: Believed complete and correct.
46 */
47
48 /**
49 * This utility class contains static methods that converts a
50 * string into a fully encoded URL string in x-www-form-urlencoded
51 * format. This format replaces certain disallowed characters with
52 * encoded equivalents. All upper case and lower case letters in the
53 * US alphabet remain as is, the space character (' ') is replaced with
54 * '+' sign, and all other characters are converted to a "%XX" format
55 * where XX is the hexadecimal representation of that character in a
56 * certain encoding (by default "UTF-8").
57 * <p>
58 * This method is very useful for encoding strings to be sent to CGI scripts
59 *
60 * @author Aaron M. Renn (arenn@urbanophile.com)
61 * @author Warren Levy <warrenl@cygnus.com>
62 * @author Mark Wielaard (mark@klomp.org)
63 */
64public class URLEncoder
65{
66 /**
67 * This method translates the passed in string into x-www-form-urlencoded
68 * format using the standard "UTF-8" character encoding to hex-encode the
69 * unsafe characters.
70 *
71 * @param s The String to convert
72 *
73 * @return The converted String
74 */
75 public static String encode(String s)
76 {
77 try
78 {
79 return encode(s, "UTF-8");
80 }
81 catch (UnsupportedEncodingException uee)
82 {
83 // Should never happen since UTF-8 should always be supported
84 return s;
85 }
86 }
87
88 /**
89 * This method translates the passed in string into x-www-form-urlencoded
90 * format using the character encoding to hex-encode the unsafe characters.
91 *
92 * @param s The String to convert
93 * @param encoding The encoding to use for unsafe characters
94 *
95 * @return The converted String
96 *
97 * @exception UnsupportedEncodingException If the named encoding is not
98 * supported
99 *
100 * @since 1.4
101 */
102 public static String encode(String s, String encoding)
103 throws UnsupportedEncodingException
104 {
105 int length = s.length();
106 int start = 0;
107 int i = 0;
108
109 StringBuffer result = new StringBuffer(length);
110 while (true)
111 {
112 while ( i < length && isSafe(s.charAt(i)) )
113 i++;
114
115 // Safe character can just be added
116 result.append(s.substring(start, i));
117
118 // Are we done?
119 if (i >= length)
120 return result.toString();
121 else if (s.charAt(i) == ' ')
122 {
123 result.append('+'); // Replace space char with plus symbol.
124 i++;
125 }
126 else
127 {
128 // Get all unsafe characters
129 start = i;
130 char c;
131 while ( i < length && (c = s.charAt(i)) != ' ' && !isSafe(c) )
132 i++;
133
134 // Convert them to %XY encoded strings
135 String unsafe = s.substring(start,i);
136 byte bytes[] = unsafe.getBytes(encoding);
137 for (int j = 0; j < bytes.length; j++)
138 {
139 result.append('%');
140 result.append(Integer.toHexString(((int) bytes[j]) & 0xFF));
141 }
142 }
143 start = i;
144 }
145 }
146
147 /**
148 * Private static method that returns true if the given char is either
149 * a uppercase or lowercase letter from 'a' till 'z', or a digit froim
150 * '0' till '9', or one of the characters '-', '_', '.' or '*'. Such
151 * 'safe' character don't have to be url encoded.
152 */
153 private static boolean isSafe(char c)
154 {
155 return ((c >= 'a' && c <= 'z') ||
156 (c >= 'A' && c <= 'Z') ||
157 (c >= '0' && c <= '9') ||
158 c == '-' || c == '_' || c == '.' || c == '*');
159 }
160
161 /**
162 * Private constructor that does nothing. Included to avoid a default
163 * public constructor being created by the compiler.
164 */
165 private URLEncoder() { }
166
167} // class URLEncoder
Note: See TracBrowser for help on using the repository browser.