KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > urls > URICodec


1 package org.jahia.urls;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 /**
7  * <p>Title: </p>
8  * <p>Description: </p>
9  * <p>Copyright: Copyright (c) 2002</p>
10  * <p>Company: Jahia Ltd</p>
11  * @author Serge Huber
12  * @version 1.0
13  */

14
15 public class URICodec {
16
17     private static org.apache.log4j.Logger logger =
18             org.apache.log4j.Logger.getLogger (URICodec.class);
19
20     private static final String JavaDoc hexString = "0123456789ABCDEF";
21
22     public static final String JavaDoc ALPHANUM_CHARS =
23         "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
24     public static final String JavaDoc MARK_CHARS = "-_.!~*'()";
25     public static final String JavaDoc UNRESERVED_CHARS = ALPHANUM_CHARS + MARK_CHARS;
26     /*
27      * The following comply to the standard, but allow for the "+" character
28      * which causes problems in URIs because it gets translated back to space
29      * characters.
30     public static final String RESERVED_CHARS =
31         ";/?:@&=+$,";
32     public static final String AUTHORITY_AUTHORIZEDCHARS = UNRESERVED_CHARS + "$,;:@&=+";
33     public static final String PATH_AUTHORIZEDCHARS = UNRESERVED_CHARS + "/;:@&=+$,";
34      * so instead we use the following :
35      */

36     public static final String JavaDoc RESERVED_CHARS =
37         ";/?:@&=$,";
38     public static final String JavaDoc AUTHORITY_AUTHORIZEDCHARS = UNRESERVED_CHARS + "$,;:@&=";
39     public static final String JavaDoc PATH_AUTHORIZEDCHARS = UNRESERVED_CHARS + "/;:@&=$,";
40
41     public static final String JavaDoc QUERY_AUTHORIZEDCHARS = UNRESERVED_CHARS + "=&";
42     public static final String JavaDoc FRAGMENT_AUTHORIZEDCHARS = UNRESERVED_CHARS;
43
44     public static final String JavaDoc DEFAULT_AUTHORIZEDCHARS = ALPHANUM_CHARS +
45         MARK_CHARS + RESERVED_CHARS;
46
47     // private static String defaultEncoding = "ISO-8859-1";
48
private static String JavaDoc defaultEncoding = "UTF-8";
49     private static String JavaDoc defaultAuthorizedChars = DEFAULT_AUTHORIZEDCHARS;
50
51     public static String JavaDoc encode(String JavaDoc input, String JavaDoc encoding, String JavaDoc authorizedChars)
52         throws java.io.UnsupportedEncodingException JavaDoc {
53         if ((input == null) || (encoding == null)) {
54             return null;
55         }
56         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
57         for (int i = 0; i < input.length(); i++) {
58             String JavaDoc curChar = input.substring(i, i+1);
59             if (authorizedChars.indexOf(curChar) == -1) {
60                 // we found a character that is not authorized, we must encode it.
61
// first we must determine if it's a multi-byte character or
62
// not.
63
byte[] charBytes = curChar.getBytes(encoding);
64                 for (int j=0; j < charBytes.length; j++) {
65                     result.append("%");
66                     char hiHex = hexString.charAt((charBytes[j] & 0xFF) >> 4);
67                     result.append(hiHex);
68                     char lowHex = hexString.charAt(charBytes[j] & 0xF);
69                     result.append(lowHex);
70                 }
71             } else {
72                 result.append(curChar);
73             }
74         }
75         return result.toString();
76     }
77
78     public static String JavaDoc encode(String JavaDoc input, String JavaDoc authorizedChars)
79         throws java.io.UnsupportedEncodingException JavaDoc {
80         return encode(input, defaultEncoding, authorizedChars);
81     }
82
83     public static String JavaDoc encode(String JavaDoc input)
84         throws java.io.UnsupportedEncodingException JavaDoc {
85         return encode(input, defaultEncoding, defaultAuthorizedChars );
86     }
87
88     public static String JavaDoc decode(String JavaDoc input, String JavaDoc encoding)
89         throws java.io.UnsupportedEncodingException JavaDoc {
90         if ((input == null) || (encoding == null)) {
91             return null;
92         }
93         ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
94         int currentPos = 0;
95         try {
96             while ( (input.indexOf("%", currentPos) != -1) &&
97                    (currentPos < input.length())) {
98                 int encodedBytePos = input.indexOf("%", currentPos);
99                 if (encodedBytePos > currentPos) {
100                 byteOut.write(input.substring(currentPos, encodedBytePos).
101                               getBytes(encoding));
102                 }
103                 char hiHexChar = input.charAt(encodedBytePos + 1);
104                 char lowHexChar = input.charAt(encodedBytePos + 2);
105                 int hiHex = hexString.indexOf(hiHexChar);
106                 int lowHex = hexString.indexOf(lowHexChar);
107                 if ( (hiHex >= 0) && (lowHex >= 0)) {
108                     byte curByte = (byte) (( (hiHex << 4) + lowHex) & 0xFF);
109                     byteOut.write(curByte);
110                     currentPos = encodedBytePos + 3;
111                 } else {
112                     currentPos = encodedBytePos + 1;
113                 }
114             }
115             byteOut.write(input.substring(currentPos).getBytes(encoding));
116             byteOut.flush();
117         } catch (IOException JavaDoc ioe) {
118             logger.error("Error while writing to ByteArrayOutputStream", ioe);
119         }
120         return byteOut.toString(encoding);
121     }
122
123     public static String JavaDoc decode(String JavaDoc input)
124         throws java.io.UnsupportedEncodingException JavaDoc {
125         return decode(input, defaultEncoding);
126     }
127
128     static public String JavaDoc getDefaultEncoding() {
129         return defaultEncoding;
130     }
131
132     static public void setDefaultEncoding(String JavaDoc encoding) {
133         defaultEncoding = encoding;
134     }
135
136     static public String JavaDoc getDefaultAuthorizedChars () {
137         return defaultAuthorizedChars;
138     }
139
140     static public void setDefaultAuthorizedChars (String JavaDoc authorizedChars) {
141         defaultAuthorizedChars = authorizedChars;
142     }
143
144 }
145
Popular Tags