1 19 20 28 29 package org.netbeans.modules.xml.retriever; 30 31 import java.io.BufferedInputStream ; 32 import java.io.BufferedOutputStream ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.net.URI ; 38 import java.net.URISyntaxException ; 39 import java.util.HashMap ; 40 import org.netbeans.modules.xml.retriever.catalog.Utilities; 41 import org.openide.filesystems.FileLock; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.util.NbBundle; 45 46 50 public class RetrieverTask { 51 52 53 54 private File saveFile; 55 56 private String baseAddress; 57 58 private String sourceToBeGot; 59 60 private RetrieveEntry rent; 61 62 private RetrieverEngine retEngine = null; 63 64 65 public RetrieverTask(RetrieveEntry rent, RetrieverEngine retEngine){ 66 this.retEngine = retEngine; 67 this.sourceToBeGot = rent.getCurrentAddress(); 68 this.baseAddress = rent.getBaseAddress(); 69 this.saveFile = rent.getSaveFile(); 70 this.rent = rent; 71 } 72 73 74 public HashMap <String ,File > goGetIt() throws IOException , URISyntaxException { 75 synchronized(RetrieverTask.class){ 76 if(( saveFile != null ) && (saveFile.isFile()) && (saveFile.length() != 0)){ 77 80 throw new IOException ("File already exists"); } 83 ResourceRetriever rr = ResourceRetrieverFactory.getResourceRetriever(baseAddress, sourceToBeGot); 84 if(rr == null ) 85 throw new RuntimeException ("No Retriever for this Resource address :"+sourceToBeGot); 87 if(isAlreadyDownloadedInThisSession(rr.getEffectiveAddress(baseAddress , sourceToBeGot))){ 88 String fileExists = NbBundle.getMessage(RetrieverTask.class, 89 IConstants.EXCEPTION_CYCLIC_REFERENCE_INDICATOR); 90 throw new IOException (fileExists); 91 } 92 93 HashMap <String , InputStream > srcAddrNContent = rr.retrieveDocument(baseAddress , sourceToBeGot); 94 if(srcAddrNContent == null) 95 return null; 96 String effectiveSrcAddr = srcAddrNContent.keySet().iterator().next(); 97 InputStream is = srcAddrNContent.get(effectiveSrcAddr); 98 rent.setEffectiveAddress(effectiveSrcAddr); 99 if(saveFile == null) 100 saveFile = guessSaveFile(rent); 101 if(saveFile == null) 102 throw new IOException ("Could not determine the save file."); 104 checkForCycle(saveFile, rr.getStreamLength(), is); 105 106 if(retEngine.isSave2SingleFolder() && !retEngine.getFileOverwrite() ){ 107 int i = 0; 109 File curFile = saveFile; 110 String fileName = saveFile.getName(); 111 while(curFile.isFile()) 112 curFile = new File (retEngine.getCurrentSaveRootFile()+File.separator+"new"+i+++fileName); 113 saveFile = curFile; 114 } 115 116 BufferedInputStream bis = new BufferedInputStream (is, 1024); 117 FileObject saveFileObject = FileUtil.toFileObject(FileUtil.normalizeFile(saveFile)); 118 saveFile.getParentFile().mkdirs(); 120 FileObject parent = FileUtil.toFileObject(FileUtil.normalizeFile(saveFile.getParentFile())); 121 saveFileObject = FileUtil.createData(parent, saveFile.getName()); 122 FileLock saveFileLock = saveFileObject.lock(); 123 try{ 124 OutputStream saveFileOutputStream = saveFileObject.getOutputStream(saveFileLock); 125 BufferedOutputStream bos = new BufferedOutputStream (saveFileOutputStream, 1024); 126 byte[] buffer = new byte[1024]; 127 int len = 0; 128 while((len = bis.read(buffer)) != -1){ 129 bos.write(buffer, 0, len); 130 } 131 bos.close(); 132 bis.close(); 133 }finally{ 134 saveFileLock.releaseLock(); 136 } 137 HashMap <String , File > result = new HashMap <String , File >(); 138 String modifiedFileExtn = null; 139 try { 140 modifiedFileExtn = new DocumentTypeSchemaWsdlParser().getFileExtensionByParsing(saveFile); 141 if(modifiedFileExtn != null){ 142 if(!saveFileObject.getNameExt().endsWith("."+modifiedFileExtn)){ 143 String fileName = saveFileObject.getNameExt(); 144 if(saveFileObject.getNameExt().endsWith("_"+modifiedFileExtn)){ 145 fileName = fileName.substring(0, fileName.length() - 146 ("_"+modifiedFileExtn).length()); 147 } 148 149 File newFile = new File (saveFile.getParent()+File.separator+fileName+"."+modifiedFileExtn); 150 if(newFile.isFile() && retEngine.getFileOverwrite()) 151 newFile.delete(); 152 153 FileObject newsaveFileObject = FileUtil.copyFile(saveFileObject, saveFileObject.getParent(), 154 fileName, modifiedFileExtn); 155 saveFileObject.delete(); 156 saveFileObject = newsaveFileObject; 157 } 158 } 159 } catch (Exception ex) { 160 } 162 163 result.put(effectiveSrcAddr, FileUtil.toFile(saveFileObject)); 164 return result; 167 } 168 } 169 170 171 private File guessSaveFile(RetrieveEntry rent) throws URISyntaxException , IOException { 172 if(rent.getSaveFile() != null) 173 return rent.getSaveFile(); 174 URI curUri = new URI (rent.getEffectiveAddress()); 175 String curAddr = rent.getEffectiveAddress(); 177 String curFileName = null; 178 int index = curAddr.lastIndexOf('/'); if(index != -1){ 180 curFileName = curAddr.substring(index+1); 181 }else{ 182 curFileName = curAddr; 183 } 184 185 if(retEngine.isSave2SingleFolder()){ 186 curFileName = convertAllSpecialChars(curFileName); 187 return new File (retEngine.getCurrentSaveRootFile()+File.separator+curFileName); 188 } 189 190 File result = null; 191 192 if(curUri.isAbsolute()){ 194 if("http".equalsIgnoreCase(curUri.getScheme()) || "https".equalsIgnoreCase(curUri.getScheme())) { result = getSaveFileForURL(curUri); 197 } else{ 198 result = new File (new URI (retEngine.getFixedSaveRootFolder().toURI().toString()+"/"+curFileName)); 199 } 200 }else{ 201 File newFile = new File (new URI (rent.getLocalBaseFile().getParentFile().toURI().normalize().toString()+"/"+rent.getCurrentAddress())).getCanonicalFile(); 202 File newParentFile = getModifiedParentFile(rent.getLocalBaseFile(), newFile); 203 if(rent.getLocalBaseFile() != newParentFile) 204 result = new File (new URI (newParentFile.getParentFile().toURI().toString()+"/"+rent.getCurrentAddress())); 205 else 206 result = newFile; 207 } 208 return result; 209 } 210 211 private File getModifiedParentFile(File parentFile, File curSaveFile) { 212 File result = parentFile; 213 214 String curSaveStr = curSaveFile.toURI().toString(); 215 String saveRootStr = retEngine.getFixedSaveRootFolder().toURI().toString(); 216 217 if(curSaveStr.startsWith(saveRootStr)){ 218 return result; 219 } 220 int pushCount = Utilities.countPushdownFolders(curSaveFile.toURI(), retEngine.getFixedSaveRootFolder().toURI()); 221 retEngine.pushDownRoot(pushCount); 222 result = retEngine.getNewFileForOld(parentFile, pushCount); 223 return result; 224 } 225 226 private File getSaveFileForURL(URI absURI) { 227 String rootFolderStr = retEngine.getFixedSaveRootFolder().toURI().toString(); 228 String resultStr = absURI.getSchemeSpecificPart().replace(':','_'); 230 resultStr = resultStr.replace('?', '.'); 231 if(resultStr.contains(".")){ 232 String fileExtension = resultStr.substring(resultStr.lastIndexOf('.'), resultStr.length()); 233 234 if(!fileExtension.equals(fileExtension.toLowerCase())){ 235 resultStr = resultStr.substring(0, resultStr.lastIndexOf('.'))+fileExtension.toLowerCase(); 236 } 237 } 238 resultStr = convertAllSpecialChars(resultStr); 239 resultStr = rootFolderStr+"/"+resultStr; 240 try { 241 return new File (new URI (resultStr).normalize()); 242 } catch (URISyntaxException ex) { 243 return null; 244 } 245 } 246 247 248 public String convertAllSpecialChars(String resultStr){ 249 StringBuffer sb = new StringBuffer (resultStr); 250 for(int i = 0; i < sb.length(); i++){ 251 char c = sb.charAt(i); 252 if( Character.isLetterOrDigit(c) || 253 (c == '/') || 254 (c == '.') || 255 (c == '_') || 256 (c == ' ') || 257 (c == '-')){ 258 continue; 259 }else{ 260 sb.setCharAt(i, '_'); 261 } 262 } 263 return sb.toString(); 264 } 265 266 267 private void checkForCycle(File saveFile, long l, InputStream is) throws IOException { 268 String fileExists = NbBundle.getMessage(RetrieverTask.class, 269 IConstants.EXCEPTION_CYCLIC_REFERENCE_INDICATOR); 270 if(saveFile.isFile()){ 271 if( (isAlreadyDownloadedInThisSession(saveFile)) || 272 ((saveFile.length() == l) && !retEngine.getFileOverwrite()) ) { 273 is.close(); 275 throw new IOException (fileExists+" : "+saveFile); 276 } 277 if(retEngine.getFileOverwrite()){ 278 return; 280 } else{ 281 is.close(); 283 throw new IOException (fileExists+" : "+saveFile); 284 } 285 } 286 if(saveFile.isDirectory()){ 287 is.close(); 288 String dirExists = NbBundle.getMessage(RetrieverTask.class, 289 IConstants.EXCEPTION_DIRECTORY_ALREADY_EXISTS); 290 throw new IOException (dirExists + " : "+saveFile.getCanonicalPath()); } 292 } 293 294 private boolean isAlreadyDownloadedInThisSession(File thisFile){ 295 for(RetrieveEntry rent : retEngine.retrievedList){ 296 if(rent.getSaveFile().equals(thisFile)) 297 return true; 298 } 299 return false; 300 } 301 302 private boolean isAlreadyDownloadedInThisSession(String uri){ 303 for(RetrieveEntry rent : retEngine.retrievedList){ 304 if(rent.getEffectiveAddress().equals(uri)) 305 return true; 306 } 307 return false; 308 } 309 } 310 | Popular Tags |