KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > file > URLEncodeIdMapper


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/file/URLEncodeIdMapper.java,v 1.2 2005/01/09 15:12:12 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.2 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.file;
30
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.net.URLEncoder JavaDoc;
33
34 import org.apache.commons.codec.binary.Base64;
35
36 /**
37  * URL encodes a resource.
38  *
39  * @since 1.1
40  */

41 public class URLEncodeIdMapper implements ResourceIdToPathMapper {
42     public String JavaDoc getPathForId(Object JavaDoc resourceId) {
43         String JavaDoc path = resourceId.toString();
44         try {
45             // XXX weired replacement for the fine JDK1.4 URLEncoder.encode(path, "UTF-8")
46
// method
47
// using this combination as a simple URLEncoder.encode without
48
// charset may fail depending on local settings
49
// for this reason the string will be encoded into base64 consisting
50
// of ascii characters only
51
// a further URL encoding is need as base64 might contain '/' which
52
// might be a problem for some file systems
53
path = new String JavaDoc(Base64.encodeBase64(path.getBytes("UTF-8")), "ASCII");
54             path = URLEncoder.encode(path);
55         } catch (UnsupportedEncodingException JavaDoc e) {
56             // we know this will not happen
57
}
58         return path;
59     }
60 }
61
Popular Tags