OSDN Git Service

Add license clarification.
[pf3gnuchains/gcc-fork.git] / libjava / java / net / URLEncoder.java
1 /* URLEncoder.java -- Class to convert strings to a properly encoded URL
2    Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10  
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.net;
39
40 import 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   */
64 public 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    * @since 1.4
98    */
99   public static String encode(String s, String encoding)
100     throws UnsupportedEncodingException
101   {
102     StringBuffer result = new StringBuffer();
103     int length = s.length();
104     int start = 0;
105     int i = 0;
106
107     while (true)
108     {
109       while ( i < length && isSafe(s.charAt(i)) )
110         i++;
111
112       // Safe character can just be added
113       result.append(s.substring(start, i));
114
115       // Are we done?
116       if (i >= length)
117         return result.toString();
118       else if (s.charAt(i) == ' ')
119         {
120           result.append('+');  // Replace space char with plus symbol.
121           i++;
122         }
123       else
124         {
125           // Get all unsafe characters
126           start = i;
127           char c;
128           while ( i < length && (c = s.charAt(i)) != ' ' && !isSafe(c) )
129             i++;
130
131           // Convert them to %XY encoded strings
132           String unsafe = s.substring(start,i);
133           byte bytes[] = unsafe.getBytes(encoding);
134           for (int j = 0; j < bytes.length; j++)
135             {
136               result.append('%');
137               result.append(Integer.toHexString(((int) bytes[j]) & 0xFF));
138             }
139         }
140       start = i;
141     }
142   }
143
144   /**
145    * Private static method that returns true if the given char is either
146    * a uppercase or lowercase letter from 'a' till 'z', or a digit froim
147    * '0' till '9', or one of the characters '-', '_', '.' or '*'. Such
148    * 'safe' character don't have to be url encoded.
149    */
150   private static boolean isSafe(char c)
151   {
152     return  ((c >= 'a' && c <= 'z') ||
153              (c >= 'A' && c <= 'Z') ||
154              (c >= '0' && c <= '9') ||
155              c == '-' || c == '_' || c == '.' || c == '*');
156   }
157
158   /**
159    * Private constructor that does nothing. Included to avoid a default
160    * public constructor being created by the compiler.
161    */
162   private URLEncoder() { }
163
164 } // class URLEncoder