KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > URLCodec


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

18 package org.apache.beehive.netui.core;
19
20 import java.io.UnsupportedEncodingException JavaDoc;
21
22 import org.apache.commons.codec.EncoderException;
23 import org.apache.commons.codec.DecoderException;
24
25 import org.apache.beehive.netui.util.Bundle;
26
27 /**
28  * Class that provides static methods for URL encoding/decoding
29  */

30 public final class URLCodec {
31
32     private final static org.apache.commons.codec.net.URLCodec s_codec =
33         new org.apache.commons.codec.net.URLCodec();
34
35     /**
36      * URL encodes a string.
37      * @param decoded the string to encode
38      * @param charset the character set to use
39      * @return the encoded string
40      */

41     public static String JavaDoc encode(final String JavaDoc decoded, final String JavaDoc charset)
42         throws UnsupportedEncodingException JavaDoc {
43         return s_codec.encode(decoded, charset);
44     }
45
46     /**
47      * URL encodes a string using the default character set
48      * @param decoded the string to encode
49      * @return the encoded string
50      */

51     public static String JavaDoc encode(final String JavaDoc decoded) {
52         try {
53             return s_codec.encode(decoded);
54         } catch (EncoderException e) {
55             throw new IllegalStateException JavaDoc(Bundle.getErrorString("URLCodec_encodeException", new String JavaDoc[] {e.getMessage()}), e);
56         }
57     }
58
59     /**
60      * URL decodes a string.
61      * @param encoded the string to decode
62      * @param charset the character set to use
63      * @return the decoded string
64      */

65     public static String JavaDoc decode(final String JavaDoc encoded, final String JavaDoc charset)
66         throws UnsupportedEncodingException JavaDoc {
67         try {
68             return s_codec.decode(encoded, charset);
69         } catch (DecoderException e) {
70             throw new IllegalStateException JavaDoc(Bundle.getErrorString("URLCodec_decodeException", new String JavaDoc[] {e.getMessage()}), e);
71         }
72     }
73
74
75     /**
76      * URL decodes a string using the default character set
77      * @param encoded the string to decode
78      * @return the decoded string
79      */

80     public static String JavaDoc decode(final String JavaDoc encoded) {
81         try {
82             return s_codec.decode(encoded);
83         } catch (DecoderException e) {
84             throw new IllegalStateException JavaDoc(Bundle.getErrorString("URLCodec_decodeException", new String JavaDoc[] {e.getMessage()}), e);
85         }
86     }
87 }
88
Popular Tags