KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > URLEncoder


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.util;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.util.BitSet JavaDoc;
23
24 /**
25  *
26  * This class is very similar to the java.net.URLEncoder class.
27  *
28  * Unfortunately, with java.net.URLEncoder there is no way to specify to the
29  * java.net.URLEncoder which characters should NOT be encoded.
30  *
31  * This code was moved from DefaultServlet.java
32  *
33  * @author Craig R. McClanahan
34  * @author Remy Maucherat
35  */

36 public class URLEncoder {
37     protected static final char[] hexadecimal =
38     {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
39      'A', 'B', 'C', 'D', 'E', 'F'};
40
41     //Array containing the safe characters set.
42
protected BitSet JavaDoc safeCharacters = new BitSet JavaDoc(256);
43
44     public URLEncoder() {
45         for (char i = 'a'; i <= 'z'; i++) {
46             addSafeCharacter(i);
47         }
48         for (char i = 'A'; i <= 'Z'; i++) {
49             addSafeCharacter(i);
50         }
51         for (char i = '0'; i <= '9'; i++) {
52             addSafeCharacter(i);
53         }
54     }
55
56     public void addSafeCharacter( char c ) {
57     safeCharacters.set( c );
58     }
59
60     public String JavaDoc encode( String JavaDoc path ) {
61         int maxBytesPerChar = 10;
62         int caseDiff = ('a' - 'A');
63         StringBuffer JavaDoc rewrittenPath = new StringBuffer JavaDoc(path.length());
64         ByteArrayOutputStream JavaDoc buf = new ByteArrayOutputStream JavaDoc(maxBytesPerChar);
65         OutputStreamWriter JavaDoc writer = null;
66         try {
67             writer = new OutputStreamWriter JavaDoc(buf, "UTF8");
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace();
70             writer = new OutputStreamWriter JavaDoc(buf);
71         }
72
73         for (int i = 0; i < path.length(); i++) {
74             int c = (int) path.charAt(i);
75             if (safeCharacters.get(c)) {
76                 rewrittenPath.append((char)c);
77             } else {
78                 // convert to external encoding before hex conversion
79
try {
80                     writer.write((char)c);
81                     writer.flush();
82                 } catch(IOException JavaDoc e) {
83                     buf.reset();
84                     continue;
85                 }
86                 byte[] ba = buf.toByteArray();
87                 for (int j = 0; j < ba.length; j++) {
88                     // Converting each byte in the buffer
89
byte toEncode = ba[j];
90                     rewrittenPath.append('%');
91                     int low = (int) (toEncode & 0x0f);
92                     int high = (int) ((toEncode & 0xf0) >> 4);
93                     rewrittenPath.append(hexadecimal[high]);
94                     rewrittenPath.append(hexadecimal[low]);
95                 }
96                 buf.reset();
97             }
98         }
99         return rewrittenPath.toString();
100     }
101 }
102
Popular Tags