1 19 20 28 29 package org.netbeans.modules.xml.retriever; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.net.MalformedURLException ; 34 import java.net.URI ; 35 import java.net.URISyntaxException ; 36 import java.net.URL ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 import org.netbeans.api.progress.ProgressHandle; 41 import org.netbeans.api.progress.ProgressHandleFactory; 42 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum; 43 import org.openide.DialogDisplayer; 44 import org.openide.NotifyDescriptor; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.NbBundle; 48 import org.openide.util.RequestProcessor; 49 import org.openide.windows.IOProvider; 50 import org.openide.windows.InputOutput; 51 import org.openide.windows.OutputWriter; 52 53 57 public final class ImportDirectory implements Runnable { 58 59 private File importRoot; 60 61 private File toDir; 62 63 private Thread myThread; 64 65 private boolean overWriteFiles = false; 66 67 InfoCollector infoCollector = null; 68 69 private void initOPTab(){ 70 InputOutput io = IOProvider.getDefault().getIO(opTabTitle, false); 71 OutputWriter optab = io.getOut(); 72 io.select(); 73 try{ 74 optab.reset(); 75 }catch (Exception e){ 76 } 78 } 79 80 81 public ImportDirectory(File importRoot, File toDir) { 82 this(importRoot, toDir, false); 83 } 84 85 86 public ImportDirectory(File importRoot, File toDir, boolean overWriteFiles) { 87 this.importRoot = importRoot; 88 this.toDir = toDir; 89 this.overWriteFiles = overWriteFiles; 90 initOPTab(); 91 start(); 92 } 93 94 95 public void setOverwriteFiles(boolean overWriteFiles){ 96 this.overWriteFiles = overWriteFiles; 97 } 98 99 public void start(){ 100 RequestProcessor.getDefault().post(this); 101 } 102 103 public void run() { 104 ProgressHandle ph = ProgressHandleFactory.createHandle( 105 NbBundle.getMessage(ImportDirectory.class, 106 "LBL_PROGRESSBAR_Retrieve_XML")); ph.start(); 108 ph.switchToIndeterminate(); 109 try{ 110 infoCollector = new InfoCollector(importRoot); 111 if(infoCollector.hasReports()){ 112 if(infoCollector.hasErrors()){ 113 String errorMess = NbBundle.getMessage(ImportDirectory.class, 115 "MSG_directory_closure_error"); NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE); 117 opErrors(); 118 DialogDisplayer.getDefault().notify(ndm); 119 return; 121 } 122 if(infoCollector.hasWarnings()){ 123 String warningMess = NbBundle.getMessage(ImportDirectory.class, 127 "MSG_absolute_resource_warning"); String warningTitle = NbBundle.getMessage(ImportDirectory.class, 129 "TITLE_absolute_resource_warning"); NotifyDescriptor.Confirmation ndc = 131 new NotifyDescriptor.Confirmation(warningMess, warningTitle, 132 NotifyDescriptor.Confirmation.YES_NO_OPTION, 133 NotifyDescriptor.Confirmation.WARNING_MESSAGE); 134 opWarnings(); 135 DialogDisplayer.getDefault().notify(ndc); 136 if(ndc.getValue() == ndc.NO_OPTION) 137 return; 138 } 141 } copyFiles(); 143 showCopiedFiles(); 145 }finally{ 146 ph.finish(); 147 } 148 invokeRetrieverEngineIfRequired(); 149 } 150 151 Map <File , File > copiedFiles = new HashMap <File , File >(); 152 Map <File , File > errorsWhileCopyFiles = new HashMap <File , File >(); 153 154 private void copyFiles() { 155 List <File > copyList = this.infoCollector.getCopyableFileList(); 156 for(File srcFile: copyList){ 157 FileObject source = FileUtil.toFileObject(FileUtil.normalizeFile(srcFile)); 159 160 String impRootStr = this.importRoot.toURI().toString(); 161 String toDirStr = this.toDir.toURI().toString(); 162 StringBuffer strBuff = new StringBuffer (srcFile.toURI().toString()); 163 String destStr = strBuff.replace(0, impRootStr.length()-1, toDirStr).toString(); 164 165 File destFile = null; 166 try { 167 destFile = new File (new URI (destStr)); 168 } catch (URISyntaxException ex) { 169 continue; 170 } 171 172 if(source == null){ 173 errorsWhileCopyFiles.put(srcFile, destFile); 175 continue; 176 } 177 178 if(destFile.isFile()){ 179 if(this.overWriteFiles){ 180 destFile.delete(); 181 } 182 } 183 184 String fileName = destFile.getName(); 185 fileName = fileName.substring(0, fileName.lastIndexOf(".")); 186 File destParent = destFile.getParentFile(); 187 188 destParent.mkdirs(); 189 190 FileObject destParentFOB = FileUtil.toFileObject(FileUtil.normalizeFile(destParent)); 191 try { 192 FileUtil.copyFile(source, destParentFOB, fileName); 193 } catch (IOException ex) { 194 errorsWhileCopyFiles.put(srcFile, destFile); 195 continue; 196 } 197 copiedFiles.put(srcFile, destFile); 198 } 199 } 200 private void showCopiedFiles() { 201 InputOutput io = IOProvider.getDefault().getIO(opTabTitle, false); 202 io.setErrSeparated(true); 203 OutputWriter error = io.getErr(); 204 OutputWriter output = io.getOut(); 205 206 if(errorsWhileCopyFiles.size() > 0){ 207 error.println(NbBundle.getMessage(ImportDirectory.class, 208 "MSG_OUTPUT_errors_while_copy")); for(File file: errorsWhileCopyFiles.keySet()){ 210 error.println(NbBundle.getMessage(ImportDirectory.class, 211 "MSG_OUTPUT_from_target", file.toString(), 212 errorsWhileCopyFiles.get(file).toString())); 213 } 214 } 215 error.close(); 216 if(copiedFiles.size() > 0){ 217 output.println(NbBundle.getMessage(ImportDirectory.class, 218 "MSG_OUTPUT_list_of_files_ret")); for(File file: copiedFiles.keySet()){ 220 output.println(NbBundle.getMessage(ImportDirectory.class, 221 "MSG_OUTPUT_from_copied", file.toString(), 222 copiedFiles.get(file).toString())); 223 } 224 } 225 output.close(); 226 } 227 static final String opTabTitle = NbBundle.getMessage(ImportDirectory.class, 228 "TITLE_retriever_output_tab_title"); private void opErrors() { 230 InputOutput io = IOProvider.getDefault().getIO(opTabTitle, false); 231 io.setErrSeparated(true); 232 OutputWriter error = io.getErr(); 233 String errorMess = 234 NbBundle.getMessage(ImportDirectory.class,"MSG_OUTPUT_directory_closure"); Map <File , List <InfoCollector.InfoEntry>> errors = this.infoCollector.getErrors(); 236 error.printf("\n%s (%d):\n", errorMess, errors.size()); for(File file : errors.keySet()){ 238 String msgFileStr = NbBundle.getMessage(ImportDirectory.class, 239 "MSG_OUTPUT_file"); error.printf("%s %s\n", msgFileStr, file.toString()); 241 List <InfoCollector.InfoEntry> entList =errors.get(file); 242 String msgOverfloLoc = 243 NbBundle.getMessage(ImportDirectory.class, 244 "MSG_OUTPUT_overflowing_location"); for(InfoCollector.InfoEntry ent : entList){ 246 error.printf(" %s %s\n", msgOverfloLoc, ent.getChildStr()); } 248 } 249 error.close(); 250 } 251 252 private void opWarnings() { 253 InputOutput io = IOProvider.getDefault().getIO(opTabTitle, false); 254 io.setErrSeparated(true); 255 OutputWriter error = io.getErr(); 256 String errorMess = 257 NbBundle.getMessage(ImportDirectory.class, 258 "MSG_OUTPUT_absolute_resource"); Map <File , List <InfoCollector.InfoEntry>> errors = this.infoCollector.getWarnings(); 260 error.printf("\n%s (%d):\n", errorMess, errors.size()); for(File file : errors.keySet()){ 262 String msgFileStr = 263 NbBundle.getMessage(ImportDirectory.class, "MSG_OUTPUT_file"); error.printf("%s %s\n", msgFileStr, file.toString()); 265 List <InfoCollector.InfoEntry> entList =errors.get(file); 266 String msgAbsLoc = NbBundle.getMessage(ImportDirectory.class, 267 "MSG_OUTPUT_absolute_location"); for(InfoCollector.InfoEntry ent : entList){ 269 error.printf(" %s %s\n", msgAbsLoc, ent.getChildStr()); 270 } 271 } 272 error.close(); 273 } 274 275 private void invokeRetrieverEngineIfRequired() { 276 Map <File , List <InfoCollector.InfoEntry>> errors = this.infoCollector.getAbsURL2Info(); 277 for(File file : errors.keySet()){ 278 List <InfoCollector.InfoEntry> entList =errors.get(file); 279 for(InfoCollector.InfoEntry ent : entList){ 280 if(ent.getInfoType() == ent.infoType.url){ 281 String urlStr = ent.getChildStr(); 282 URL url = null; 283 try { 284 url = new URL (urlStr); 285 } catch (MalformedURLException ex) { 286 continue; 287 } 288 RetrieverEngine instance = new RetrieverEngine(toDir); 289 RetrieveEntry rent = null; 290 rent = new RetrieveEntry(null, url.toString(), file, null, DocumentTypesEnum.schema, true); 291 instance.addResourceToRetrieve(rent); 292 instance.start(); 293 } 294 } 295 } 296 } 297 298 299 } 300 | Popular Tags |