author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
* |
|
25 |
* THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 |
*/ |
|
27 |
||
28 |
package com.sun.xml.internal.fastinfoset.sax; |
|
29 |
||
30 |
import java.io.*; |
|
31 |
||
32 |
public class SystemIdResolver { |
|
33 |
||
34 |
public SystemIdResolver() { |
|
35 |
} |
|
36 |
||
37 |
public static String getAbsoluteURIFromRelative(String localPath) { |
|
38 |
if (localPath == null || localPath.length() == 0) { |
|
39 |
return ""; |
|
40 |
} |
|
41 |
||
42 |
String absolutePath = localPath; |
|
43 |
if (!isAbsolutePath(localPath)) { |
|
44 |
try { |
|
45 |
absolutePath = getAbsolutePathFromRelativePath(localPath); |
|
46 |
} |
|
47 |
catch (SecurityException se) { |
|
48 |
return "file:" + localPath; |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
String urlString; |
|
53 |
if (null != absolutePath) { |
|
54 |
urlString = absolutePath.startsWith(File.separator) ? |
|
55 |
("file://" + absolutePath) : |
|
56 |
("file:///" + absolutePath); |
|
57 |
} |
|
58 |
else { |
|
59 |
urlString = "file:" + localPath; |
|
60 |
} |
|
61 |
||
62 |
return replaceChars(urlString); |
|
63 |
} |
|
64 |
||
65 |
private static String getAbsolutePathFromRelativePath(String relativePath) { |
|
66 |
return new File(relativePath).getAbsolutePath(); |
|
67 |
} |
|
68 |
||
69 |
public static boolean isAbsoluteURI(String systemId) { |
|
70 |
if (systemId == null) { |
|
71 |
return false; |
|
72 |
} |
|
73 |
||
74 |
if (isWindowsAbsolutePath(systemId)) { |
|
75 |
return false; |
|
76 |
} |
|
77 |
||
78 |
final int fragmentIndex = systemId.indexOf('#'); |
|
79 |
final int queryIndex = systemId.indexOf('?'); |
|
80 |
final int slashIndex = systemId.indexOf('/'); |
|
81 |
final int colonIndex = systemId.indexOf(':'); |
|
82 |
||
83 |
int index = systemId.length() -1; |
|
84 |
if (fragmentIndex > 0) { |
|
85 |
index = fragmentIndex; |
|
86 |
} |
|
87 |
if (queryIndex > 0 && queryIndex < index) { |
|
88 |
index = queryIndex; |
|
89 |
} |
|
90 |
if (slashIndex > 0 && slashIndex <index) { |
|
91 |
index = slashIndex; |
|
92 |
} |
|
93 |
return (colonIndex > 0) && (colonIndex < index); |
|
94 |
} |
|
95 |
||
96 |
public static boolean isAbsolutePath(String systemId) { |
|
97 |
if(systemId == null) |
|
98 |
return false; |
|
99 |
final File file = new File(systemId); |
|
100 |
return file.isAbsolute(); |
|
101 |
||
102 |
} |
|
103 |
||
104 |
private static boolean isWindowsAbsolutePath(String systemId) { |
|
105 |
if(!isAbsolutePath(systemId)) |
|
106 |
return false; |
|
107 |
if (systemId.length() > 2 |
|
108 |
&& systemId.charAt(1) == ':' |
|
109 |
&& Character.isLetter(systemId.charAt(0)) |
|
110 |
&& (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/')) |
|
111 |
return true; |
|
112 |
else |
|
113 |
return false; |
|
114 |
} |
|
115 |
||
116 |
private static String replaceChars(String str) { |
|
117 |
StringBuffer buf = new StringBuffer(str); |
|
118 |
int length = buf.length(); |
|
119 |
for (int i = 0; i < length; i++) { |
|
120 |
char currentChar = buf.charAt(i); |
|
121 |
// Replace space with "%20" |
|
122 |
if (currentChar == ' ') { |
|
123 |
buf.setCharAt(i, '%'); |
|
124 |
buf.insert(i+1, "20"); |
|
125 |
length = length + 2; |
|
126 |
i = i + 2; |
|
127 |
} |
|
128 |
// Replace backslash with forward slash |
|
129 |
else if (currentChar == '\\') { |
|
130 |
buf.setCharAt(i, '/'); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
return buf.toString(); |
|
135 |
} |
|
136 |
||
137 |
public static String getAbsoluteURI(String systemId) { |
|
138 |
String absoluteURI = systemId; |
|
139 |
if (isAbsoluteURI(systemId)) { |
|
140 |
if (systemId.startsWith("file:")) { |
|
141 |
String str = systemId.substring(5); |
|
142 |
||
143 |
if (str != null && str.startsWith("/")) { |
|
144 |
if (str.startsWith("///") || !str.startsWith("//")) { |
|
145 |
int secondColonIndex = systemId.indexOf(':', 5); |
|
146 |
if (secondColonIndex > 0) { |
|
147 |
String localPath = systemId.substring(secondColonIndex-1); |
|
148 |
try { |
|
149 |
if (!isAbsolutePath(localPath)) |
|
150 |
absoluteURI = systemId.substring(0, secondColonIndex-1) + |
|
151 |
getAbsolutePathFromRelativePath(localPath); |
|
152 |
} |
|
153 |
catch (SecurityException se) { |
|
154 |
return systemId; |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
else { |
|
160 |
return getAbsoluteURIFromRelative(systemId.substring(5)); |
|
161 |
} |
|
162 |
||
163 |
return replaceChars(absoluteURI); |
|
164 |
} |
|
165 |
else { |
|
166 |
return systemId; |
|
167 |
} |
|
168 |
} |
|
169 |
else { |
|
170 |
return getAbsoluteURIFromRelative(systemId); |
|
171 |
} |
|
172 |
} |
|
173 |
} |