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.URI ; 34 import java.net.URISyntaxException ; 35 import java.net.UnknownHostException ; 36 import java.util.Map ; 37 import org.netbeans.api.project.FileOwnerQuery; 38 import org.netbeans.api.project.Project; 39 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel; 40 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 44 48 public class RetrieverImpl extends Retriever{ 49 50 51 public RetrieverImpl() { 52 } 53 54 File seedFile = null; 55 RetrieverEngine instance; 56 public boolean startNewThread = false; 57 58 public FileObject retrieveResource(FileObject destinationDir, URI relativePathToCatalogFile, URI resourceToRetrieve) throws UnknownHostException , URISyntaxException , IOException { 59 return retrieveResource(destinationDir, relativePathToCatalogFile, resourceToRetrieve, false); 60 } 61 62 public FileObject retrieveResource(FileObject destinationDir,URI resourceToRetrieve) throws UnknownHostException , URISyntaxException , IOException { 63 return retrieveResource(destinationDir, null, resourceToRetrieve); 64 } 65 66 67 public FileObject retrieveResourceClosureIntoSingleDirectory(FileObject destinationDir, URI resourceToRetrieve) throws UnknownHostException , URISyntaxException , IOException { 68 return retrieveResource(destinationDir, null, resourceToRetrieve, true); 69 } 70 71 public FileObject retrieveResource(FileObject destinationDir, 72 URI relativePathToCatalogFile, URI resourceToRetrieve, 73 boolean save2singleFolder) 74 throws UnknownHostException , URISyntaxException , IOException { 75 Project prj = FileOwnerQuery.getOwner(destinationDir); 76 if(relativePathToCatalogFile == null){ 77 assert(prj != null); 78 XMLCatalogProvider catProvider = (XMLCatalogProvider) prj.getLookup(). 80 lookup(XMLCatalogProvider.class); 81 if(catProvider == null){ 82 return retrieveResourceImpl(destinationDir, resourceToRetrieve, null, save2singleFolder); 84 } 85 relativePathToCatalogFile = catProvider.getProjectWideCatalog(); 86 if(relativePathToCatalogFile == null){ 87 return retrieveResourceImpl(destinationDir, resourceToRetrieve, null, save2singleFolder); 89 } 90 } 92 URI cfuri = null; 93 if(!relativePathToCatalogFile.isAbsolute()){ 94 FileObject prjRtFO = prj.getProjectDirectory(); 95 cfuri = FileUtil.toFile(prjRtFO).toURI().resolve(relativePathToCatalogFile); 96 }else{ 97 cfuri = relativePathToCatalogFile; 98 } 99 File cffile = new File (cfuri); 100 if(!cffile.isFile()) 101 cffile.createNewFile(); 102 FileObject catalogFileObject = FileUtil.toFileObject(FileUtil.normalizeFile(cffile)); 103 return retrieveResourceImpl(destinationDir, resourceToRetrieve, catalogFileObject, save2singleFolder); 104 } 105 106 private FileObject retrieveResourceImpl(FileObject destinationDir, 107 URI resourceToRetrieve, FileObject catalogFileObject, boolean save2singleFolder) 108 throws UnknownHostException , URISyntaxException , IOException { 109 instance = new RetrieverEngine(FileUtil.toFile(destinationDir), startNewThread); 110 instance.setFileOverwrite(overwriteFiles); 111 instance.setSave2SingleFolder(save2singleFolder); 112 if(catalogFileObject != null) 113 instance.setCatalogFile(catalogFileObject); 114 instance.showErrorPopup = false; 115 RetrieveEntry rent = null; 116 rent = new RetrieveEntry(null, resourceToRetrieve.toString(), null, 117 null, DocumentTypesEnum.schema, this.retrieveRecursively); 118 instance.addResourceToRetrieve(rent); 119 instance.start(); 120 File result = instance.getSeedFileLocation(); 121 seedFile = result; 122 if(result == null) 123 return null; 124 return FileUtil.toFileObject(FileUtil.normalizeFile(result)); 126 } 127 128 129 private Project getProject(File result) { 130 FileObject fob = FileUtil.toFileObject(result); 131 return FileOwnerQuery.getOwner(fob); 132 } 133 134 public File getProjectCatalog() { 135 if(seedFile == null) 136 return null; 137 FileObject prjRootFo = getProject(seedFile).getProjectDirectory(); 138 File prjRt = FileUtil.toFile(prjRootFo); 139 File catalogFile = new File (prjRt, CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME+CatalogWriteModel.CATALOG_FILE_EXTENSION); 140 return catalogFile; 141 } 142 143 public Map <RetrieveEntry, Exception > getRetrievedResourceExceptionMap() { 144 if(instance != null) 145 return instance.getRetrievedResourceExceptionMap(); 146 else 147 return null; 148 } 149 150 public File retrieveResource(File targetFolder, URI source) throws UnknownHostException , URISyntaxException , IOException { 151 FileObject fobj = retrieveResource(FileUtil.toFileObject(FileUtil.normalizeFile(targetFolder)), source); 152 if(fobj != null) 153 return FileUtil.toFile(fobj); 154 return null; 155 } 156 157 boolean retrieveRecursively = true; 158 public void setRecursiveRetrieve(boolean retrieveRecursively) { 159 this.retrieveRecursively = retrieveRecursively; 160 } 161 162 boolean overwriteFiles = true; 163 public void setOverwriteFilesWithSameName(boolean overwriteFiles) { 164 this.overwriteFiles = overwriteFiles; 165 } 166 167 } 168 | Popular Tags |