KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > parsers > FilePathToURI


1 /*
2  * Copyright 2002-2005 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
17 package javax.xml.parsers;
18
19 class FilePathToURI {
20
21     // which ASCII characters need to be escaped
22
private static boolean gNeedEscaping[] = new boolean[128];
23     // the first hex character if a character needs to be escaped
24
private static char gAfterEscaping1[] = new char[128];
25     // the second hex character if a character needs to be escaped
26
private static char gAfterEscaping2[] = new char[128];
27     private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7',
28                                      '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
29     // initialize the above 3 arrays
30
static {
31         for (int i = 0; i <= 0x1f; i++) {
32             gNeedEscaping[i] = true;
33             gAfterEscaping1[i] = gHexChs[i >> 4];
34             gAfterEscaping2[i] = gHexChs[i & 0xf];
35         }
36         gNeedEscaping[0x7f] = true;
37         gAfterEscaping1[0x7f] = '7';
38         gAfterEscaping2[0x7f] = 'F';
39         char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}',
40                          '|', '\\', '^', '~', '[', ']', '`'};
41         int len = escChs.length;
42         char ch;
43         for (int i = 0; i < len; i++) {
44             ch = escChs[i];
45             gNeedEscaping[ch] = true;
46             gAfterEscaping1[ch] = gHexChs[ch >> 4];
47             gAfterEscaping2[ch] = gHexChs[ch & 0xf];
48         }
49     }
50
51     // To escape a file path to a URI, by using %HH to represent
52
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%'
53
// and '"' and non-ASCII characters (whose value >= 128).
54
public static String JavaDoc filepath2URI(String JavaDoc path){
55         // return null if path is null.
56
if (path == null)
57             return null;
58
59         char separator = java.io.File.separatorChar;
60         path = path.replace(separator, '/');
61
62         int len = path.length(), ch;
63         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(len*3);
64         buffer.append("file://");
65         // change C:/blah to /C:/blah
66
if (len >= 2 && path.charAt(1) == ':') {
67             ch = Character.toUpperCase(path.charAt(0));
68             if (ch >= 'A' && ch <= 'Z') {
69                 buffer.append('/');
70             }
71         }
72
73         // for each character in the path
74
int i = 0;
75         for (; i < len; i++) {
76             ch = path.charAt(i);
77             // if it's not an ASCII character, break here, and use UTF-8 encoding
78
if (ch >= 128)
79                 break;
80             if (gNeedEscaping[ch]) {
81                 buffer.append('%');
82                 buffer.append(gAfterEscaping1[ch]);
83                 buffer.append(gAfterEscaping2[ch]);
84                 // record the fact that it's escaped
85
}
86             else {
87                 buffer.append((char)ch);
88             }
89         }
90         
91         // we saw some non-ascii character
92
if (i < len) {
93             // get UTF-8 bytes for the remaining sub-string
94
byte[] bytes = null;
95             byte b;
96             try {
97                 bytes = path.substring(i).getBytes("UTF-8");
98             } catch (java.io.UnsupportedEncodingException JavaDoc e) {
99                 // should never happen
100
return path;
101             }
102             len = bytes.length;
103
104             // for each byte
105
for (i = 0; i < len; i++) {
106                 b = bytes[i];
107                 // for non-ascii character: make it positive, then escape
108
if (b < 0) {
109                     ch = b + 256;
110                     buffer.append('%');
111                     buffer.append(gHexChs[ch >> 4]);
112                     buffer.append(gHexChs[ch & 0xf]);
113                 }
114                 else if (gNeedEscaping[b]) {
115                     buffer.append('%');
116                     buffer.append(gAfterEscaping1[b]);
117                     buffer.append(gAfterEscaping2[b]);
118                 }
119                 else {
120                     buffer.append((char)b);
121                 }
122             }
123         }
124
125         return buffer.toString();
126     }
127
128 }//FilePathToURI
129
Popular Tags