1 16 19 package com.sun.org.apache.xml.internal.serializer.utils; 20 21 import java.io.File ; 22 23 import javax.xml.transform.TransformerException ; 24 25 import com.sun.org.apache.xml.internal.serializer.utils.URI.MalformedURIException; 26 27 43 public final class SystemIDResolver 44 { 45 46 60 public static String getAbsoluteURIFromRelative(String localPath) 61 { 62 if (localPath == null || localPath.length() == 0) 63 return ""; 64 65 String absolutePath = localPath; 68 if (!isAbsolutePath(localPath)) 69 { 70 try 71 { 72 absolutePath = getAbsolutePathFromRelativePath(localPath); 73 } 74 catch (SecurityException se) 76 { 77 return "file:" + localPath; 78 } 79 } 80 81 String urlString; 82 if (null != absolutePath) 83 { 84 if (absolutePath.startsWith(File.separator)) 85 urlString = "file://" + absolutePath; 86 else 87 urlString = "file:///" + absolutePath; 88 } 89 else 90 urlString = "file:" + localPath; 91 92 return replaceChars(urlString); 93 } 94 95 101 private static String getAbsolutePathFromRelativePath(String relativePath) 102 { 103 return new File (relativePath).getAbsolutePath(); 104 } 105 106 112 public static boolean isAbsoluteURI(String systemId) 113 { 114 119 124 if(isWindowsAbsolutePath(systemId)){ 126 return false; 127 } 128 129 final int fragmentIndex = systemId.indexOf('#'); 130 final int queryIndex = systemId.indexOf('?'); 131 final int slashIndex = systemId.indexOf('/'); 132 final int colonIndex = systemId.indexOf(':'); 133 134 int index = systemId.length() -1; 136 if(fragmentIndex > 0) 137 index = fragmentIndex; 138 if((queryIndex > 0) && (queryIndex <index)) 139 index = queryIndex; 140 if((slashIndex > 0) && (slashIndex <index)) 141 index = slashIndex; 142 return ((colonIndex >0) && (colonIndex<index)); 144 145 } 146 147 153 public static boolean isAbsolutePath(String systemId) 154 { 155 if(systemId == null) 156 return false; 157 final File file = new File (systemId); 158 return file.isAbsolute(); 159 160 } 161 162 168 private static boolean isWindowsAbsolutePath(String systemId) 169 { 170 if(!isAbsolutePath(systemId)) 171 return false; 172 if (systemId.length() > 2 174 && systemId.charAt(1) == ':' 175 && Character.isLetter(systemId.charAt(0)) 176 && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/')) 177 return true; 178 else 179 return false; 180 } 181 182 189 private static String replaceChars(String str) 190 { 191 StringBuffer buf = new StringBuffer (str); 192 int length = buf.length(); 193 for (int i = 0; i < length; i++) 194 { 195 char currentChar = buf.charAt(i); 196 if (currentChar == ' ') 198 { 199 buf.setCharAt(i, '%'); 200 buf.insert(i+1, "20"); 201 length = length + 2; 202 i = i + 2; 203 } 204 else if (currentChar == '\\') 206 { 207 buf.setCharAt(i, '/'); 208 } 209 } 210 211 return buf.toString(); 212 } 213 214 221 public static String getAbsoluteURI(String systemId) 222 { 223 String absoluteURI = systemId; 224 if (isAbsoluteURI(systemId)) 225 { 226 if (systemId.startsWith("file:")) 228 { 229 String str = systemId.substring(5); 230 231 if (str != null && str.startsWith("/")) 234 { 235 if (str.startsWith("///") || !str.startsWith("//")) 236 { 237 int secondColonIndex = systemId.indexOf(':', 5); 240 if (secondColonIndex > 0) 241 { 242 String localPath = systemId.substring(secondColonIndex-1); 243 try { 244 if (!isAbsolutePath(localPath)) 245 absoluteURI = systemId.substring(0, secondColonIndex-1) + 246 getAbsolutePathFromRelativePath(localPath); 247 } 248 catch (SecurityException se) { 249 return systemId; 250 } 251 } 252 } 253 } 254 else 255 { 256 return getAbsoluteURIFromRelative(systemId.substring(5)); 257 } 258 259 return replaceChars(absoluteURI); 260 } 261 else 262 return systemId; 263 } 264 else 265 return getAbsoluteURIFromRelative(systemId); 266 267 } 268 269 270 279 public static String getAbsoluteURI(String urlString, String base) 280 throws TransformerException 281 { 282 if (base == null) 283 return getAbsoluteURI(urlString); 284 285 String absoluteBase = getAbsoluteURI(base); 286 URI uri = null; 287 try 288 { 289 URI baseURI = new URI(absoluteBase); 290 uri = new URI(baseURI, urlString); 291 } 292 catch (MalformedURIException mue) 293 { 294 throw new TransformerException (mue); 295 } 296 297 return replaceChars(uri.toString()); 298 } 299 300 } 301 | Popular Tags |