1 38 39 40 package com.sun.xml.fastinfoset.sax; 41 42 import java.io.*; 43 44 public class SystemIdResolver { 45 46 public SystemIdResolver() { 47 } 48 49 public static String getAbsoluteURIFromRelative(String localPath) { 50 if (localPath == null || localPath.length() == 0) { 51 return ""; 52 } 53 54 String absolutePath = localPath; 55 if (!isAbsolutePath(localPath)) { 56 try { 57 absolutePath = getAbsolutePathFromRelativePath(localPath); 58 } 59 catch (SecurityException se) { 60 return "file:" + localPath; 61 } 62 } 63 64 String urlString; 65 if (null != absolutePath) { 66 urlString = absolutePath.startsWith(File.separator) ? 67 ("file://" + absolutePath) : 68 ("file:///" + absolutePath); 69 } 70 else { 71 urlString = "file:" + localPath; 72 } 73 74 return replaceChars(urlString); 75 } 76 77 private static String getAbsolutePathFromRelativePath(String relativePath) { 78 return new File(relativePath).getAbsolutePath(); 79 } 80 81 public static boolean isAbsoluteURI(String systemId) { 82 if (systemId == null) { 83 return false; 84 } 85 86 if (isWindowsAbsolutePath(systemId)) { 87 return false; 88 } 89 90 final int fragmentIndex = systemId.indexOf('#'); 91 final int queryIndex = systemId.indexOf('?'); 92 final int slashIndex = systemId.indexOf('/'); 93 final int colonIndex = systemId.indexOf(':'); 94 95 int index = systemId.length() -1; 96 if (fragmentIndex > 0) { 97 index = fragmentIndex; 98 } 99 if (queryIndex > 0 && queryIndex < index) { 100 index = queryIndex; 101 } 102 if (slashIndex > 0 && slashIndex <index) { 103 index = slashIndex; 104 } 105 return (colonIndex > 0) && (colonIndex < index); 106 } 107 108 public static boolean isAbsolutePath(String systemId) { 109 if(systemId == null) 110 return false; 111 final File file = new File(systemId); 112 return file.isAbsolute(); 113 114 } 115 116 private static boolean isWindowsAbsolutePath(String systemId) { 117 if(!isAbsolutePath(systemId)) 118 return false; 119 if (systemId.length() > 2 120 && systemId.charAt(1) == ':' 121 && Character.isLetter(systemId.charAt(0)) 122 && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/')) 123 return true; 124 else 125 return false; 126 } 127 128 private static String replaceChars(String str) { 129 StringBuffer buf = new StringBuffer (str); 130 int length = buf.length(); 131 for (int i = 0; i < length; i++) { 132 char currentChar = buf.charAt(i); 133 if (currentChar == ' ') { 135 buf.setCharAt(i, '%'); 136 buf.insert(i+1, "20"); 137 length = length + 2; 138 i = i + 2; 139 } 140 else if (currentChar == '\\') { 142 buf.setCharAt(i, '/'); 143 } 144 } 145 146 return buf.toString(); 147 } 148 149 public static String getAbsoluteURI(String systemId) { 150 String absoluteURI = systemId; 151 if (isAbsoluteURI(systemId)) { 152 if (systemId.startsWith("file:")) { 153 String str = systemId.substring(5); 154 155 if (str != null && str.startsWith("/")) { 156 if (str.startsWith("///") || !str.startsWith("//")) { 157 int secondColonIndex = systemId.indexOf(':', 5); 158 if (secondColonIndex > 0) { 159 String localPath = systemId.substring(secondColonIndex-1); 160 try { 161 if (!isAbsolutePath(localPath)) 162 absoluteURI = systemId.substring(0, secondColonIndex-1) + 163 getAbsolutePathFromRelativePath(localPath); 164 } 165 catch (SecurityException se) { 166 return systemId; 167 } 168 } 169 } 170 } 171 else { 172 return getAbsoluteURIFromRelative(systemId.substring(5)); 173 } 174 175 return replaceChars(absoluteURI); 176 } 177 else { 178 return systemId; 179 } 180 } 181 else { 182 return getAbsoluteURIFromRelative(systemId); 183 } 184 } 185 } 186 | Popular Tags |