KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > RetrieverImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * RetrieverImpl.java
22  *
23  * Created on February 21, 2006, 9:30 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.URI JavaDoc;
34 import java.net.URISyntaxException JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36 import java.util.Map JavaDoc;
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 /**
45  *
46  * @author girix
47  */

48 public class RetrieverImpl extends Retriever{
49     
50     /** Creates a new instance of RetrieverImpl */
51     public RetrieverImpl() {
52     }
53     
54     File JavaDoc seedFile = null;
55     RetrieverEngine instance;
56     public boolean startNewThread = false;
57     
58     public FileObject retrieveResource(FileObject destinationDir, URI JavaDoc relativePathToCatalogFile, URI JavaDoc resourceToRetrieve) throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
59         return retrieveResource(destinationDir, relativePathToCatalogFile, resourceToRetrieve, false);
60     }
61     
62     public FileObject retrieveResource(FileObject destinationDir,URI JavaDoc resourceToRetrieve) throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
63         return retrieveResource(destinationDir, null, resourceToRetrieve);
64     }
65     
66     
67     public FileObject retrieveResourceClosureIntoSingleDirectory(FileObject destinationDir, URI JavaDoc resourceToRetrieve) throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
68         return retrieveResource(destinationDir, null, resourceToRetrieve, true);
69     }
70     
71     public FileObject retrieveResource(FileObject destinationDir,
72             URI JavaDoc relativePathToCatalogFile, URI JavaDoc resourceToRetrieve,
73             boolean save2singleFolder)
74             throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
75         Project prj = FileOwnerQuery.getOwner(destinationDir);
76         if(relativePathToCatalogFile == null){
77             assert(prj != null);
78             //check if this project has XMLCatalogProvider in its lookup
79
XMLCatalogProvider catProvider = (XMLCatalogProvider) prj.getLookup().
80                     lookup(XMLCatalogProvider.class);
81             if(catProvider == null){
82                 //there is no catalog provider so just use the legacy projectwide catalog approach
83
return retrieveResourceImpl(destinationDir, resourceToRetrieve, null, save2singleFolder);
84             }
85             relativePathToCatalogFile = catProvider.getProjectWideCatalog();
86             if(relativePathToCatalogFile == null){
87                 //somehow this provider does not give me this info. So follow legacy.
88
return retrieveResourceImpl(destinationDir, resourceToRetrieve, null, save2singleFolder);
89             }
90             //use this relativePathToCatalogFile for the new catalog file.
91
}
92         URI JavaDoc 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 JavaDoc cffile = new File JavaDoc(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 JavaDoc resourceToRetrieve, FileObject catalogFileObject, boolean save2singleFolder)
108             throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
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 JavaDoc result = instance.getSeedFileLocation();
121         seedFile = result;
122         if(result == null)
123             return null;
124         //createCatalog(result);
125
return FileUtil.toFileObject(FileUtil.normalizeFile(result));
126     }
127     
128     
129     private Project getProject(File JavaDoc result) {
130         FileObject fob = FileUtil.toFileObject(result);
131         return FileOwnerQuery.getOwner(fob);
132     }
133     
134     public File JavaDoc getProjectCatalog() {
135         if(seedFile == null)
136             return null;
137         FileObject prjRootFo = getProject(seedFile).getProjectDirectory();
138         File JavaDoc prjRt = FileUtil.toFile(prjRootFo);
139         File JavaDoc catalogFile = new File JavaDoc(prjRt, CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME+CatalogWriteModel.CATALOG_FILE_EXTENSION);
140         return catalogFile;
141     }
142     
143     public Map JavaDoc<RetrieveEntry, Exception JavaDoc> getRetrievedResourceExceptionMap() {
144         if(instance != null)
145             return instance.getRetrievedResourceExceptionMap();
146         else
147             return null;
148     }
149     
150     public File JavaDoc retrieveResource(File JavaDoc targetFolder, URI JavaDoc source) throws UnknownHostException JavaDoc, URISyntaxException JavaDoc, IOException JavaDoc {
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