01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * $Header:$
18: */
19: package org.apache.beehive.netui.core;
20:
21: import java.io.UnsupportedEncodingException;
22:
23: import org.apache.commons.codec.EncoderException;
24: import org.apache.commons.codec.DecoderException;
25:
26: import org.apache.beehive.netui.util.Bundle;
27:
28: /**
29: * Class that provides static methods for URL encoding/decoding
30: */
31: public final class URLCodec {
32:
33: private final static org.apache.commons.codec.net.URLCodec s_codec = 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 encode(final String decoded,
42: final String charset) throws UnsupportedEncodingException {
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 encode(final String decoded) {
52: try {
53: return s_codec.encode(decoded);
54: } catch (EncoderException e) {
55: throw new IllegalStateException(Bundle.getErrorString(
56: "URLCodec_encodeException", new String[] { e
57: .getMessage() }), e);
58: }
59: }
60:
61: /**
62: * URL decodes a string.
63: * @param encoded the string to decode
64: * @param charset the character set to use
65: * @return the decoded string
66: */
67: public static String decode(final String encoded,
68: final String charset) throws UnsupportedEncodingException {
69: try {
70: return s_codec.decode(encoded, charset);
71: } catch (DecoderException e) {
72: throw new IllegalStateException(Bundle.getErrorString(
73: "URLCodec_decodeException", new String[] { e
74: .getMessage() }), e);
75: }
76: }
77:
78: /**
79: * URL decodes a string using the default character set
80: * @param encoded the string to decode
81: * @return the decoded string
82: */
83: public static String decode(final String encoded) {
84: try {
85: return s_codec.decode(encoded);
86: } catch (DecoderException e) {
87: throw new IllegalStateException(Bundle.getErrorString(
88: "URLCodec_decodeException", new String[] { e
89: .getMessage() }), e);
90: }
91: }
92: }
|