1 16 19 package org.apache.xml.utils; 20 21 import java.io.File ; 22 23 import javax.xml.transform.TransformerException ; 24 25 import org.apache.xml.utils.URI.MalformedURIException; 26 27 36 public class SystemIDResolver 37 { 38 39 53 public static String getAbsoluteURIFromRelative(String localPath) 54 { 55 if (localPath == null || localPath.length() == 0) 56 return ""; 57 58 String absolutePath = localPath; 61 if (!isAbsolutePath(localPath)) 62 { 63 try 64 { 65 absolutePath = getAbsolutePathFromRelativePath(localPath); 66 } 67 catch (SecurityException se) 69 { 70 return "file:" + localPath; 71 } 72 } 73 74 String urlString; 75 if (null != absolutePath) 76 { 77 if (absolutePath.startsWith(File.separator)) 78 urlString = "file://" + absolutePath; 79 else 80 urlString = "file:///" + absolutePath; 81 } 82 else 83 urlString = "file:" + localPath; 84 85 return replaceChars(urlString); 86 } 87 88 94 private static String getAbsolutePathFromRelativePath(String relativePath) 95 { 96 return new File (relativePath).getAbsolutePath(); 97 } 98 99 105 public static boolean isAbsoluteURI(String systemId) 106 { 107 112 117 if(isWindowsAbsolutePath(systemId)){ 119 return false; 120 } 121 122 final int fragmentIndex = systemId.indexOf('#'); 123 final int queryIndex = systemId.indexOf('?'); 124 final int slashIndex = systemId.indexOf('/'); 125 final int colonIndex = systemId.indexOf(':'); 126 127 int index = systemId.length() -1; 129 if(fragmentIndex > 0) 130 index = fragmentIndex; 131 if((queryIndex > 0) && (queryIndex <index)) 132 index = queryIndex; 133 if((slashIndex > 0) && (slashIndex <index)) 134 index = slashIndex; 135 return ((colonIndex >0) && (colonIndex<index)); 137 138 } 139 140 146 public static boolean isAbsolutePath(String systemId) 147 { 148 if(systemId == null) 149 return false; 150 final File file = new File (systemId); 151 return file.isAbsolute(); 152 153 } 154 155 161 private static boolean isWindowsAbsolutePath(String systemId) 162 { 163 if(!isAbsolutePath(systemId)) 164 return false; 165 if (systemId.length() > 2 167 && systemId.charAt(1) == ':' 168 && Character.isLetter(systemId.charAt(0)) 169 && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/')) 170 return true; 171 else 172 return false; 173 } 174 175 182 private static String replaceChars(String str) 183 { 184 StringBuffer buf = new StringBuffer (str); 185 int length = buf.length(); 186 for (int i = 0; i < length; i++) 187 { 188 char currentChar = buf.charAt(i); 189 if (currentChar == ' ') 191 { 192 buf.setCharAt(i, '%'); 193 buf.insert(i+1, "20"); 194 length = length + 2; 195 i = i + 2; 196 } 197 else if (currentChar == '\\') 199 { 200 buf.setCharAt(i, '/'); 201 } 202 } 203 204 return buf.toString(); 205 } 206 207 214 public static String getAbsoluteURI(String systemId) 215 { 216 String absoluteURI = systemId; 217 if (isAbsoluteURI(systemId)) 218 { 219 if (systemId.startsWith("file:")) 221 { 222 String str = systemId.substring(5); 223 224 if (str != null && str.startsWith("/")) 227 { 228 if (str.startsWith("///") || !str.startsWith("//")) 229 { 230 int secondColonIndex = systemId.indexOf(':', 5); 233 if (secondColonIndex > 0) 234 { 235 String localPath = systemId.substring(secondColonIndex-1); 236 try { 237 if (!isAbsolutePath(localPath)) 238 absoluteURI = systemId.substring(0, secondColonIndex-1) + 239 getAbsolutePathFromRelativePath(localPath); 240 } 241 catch (SecurityException se) { 242 return systemId; 243 } 244 } 245 } 246 } 247 else 248 { 249 return getAbsoluteURIFromRelative(systemId.substring(5)); 250 } 251 252 return replaceChars(absoluteURI); 253 } 254 else 255 return systemId; 256 } 257 else 258 return getAbsoluteURIFromRelative(systemId); 259 260 } 261 262 263 272 public static String getAbsoluteURI(String urlString, String base) 273 throws TransformerException 274 { 275 if (base == null) 276 return getAbsoluteURI(urlString); 277 278 String absoluteBase = getAbsoluteURI(base); 279 URI uri = null; 280 try 281 { 282 URI baseURI = new URI(absoluteBase); 283 uri = new URI(baseURI, urlString); 284 } 285 catch (MalformedURIException mue) 286 { 287 throw new TransformerException (mue); 288 } 289 290 return replaceChars(uri.toString()); 291 } 292 293 } 294 | Popular Tags |