KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ImportDirectory.java
22  *
23  * Created on January 26, 2006, 9:02 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.MalformedURLException JavaDoc;
34 import java.net.URI JavaDoc;
35 import java.net.URISyntaxException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
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 /**
54  *
55  * @author girix
56  */

57 public final class ImportDirectory implements Runnable JavaDoc{
58     
59     private File JavaDoc importRoot;
60     
61     private File JavaDoc toDir;
62     
63     private Thread JavaDoc 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 JavaDoc e){
76             //don't care
77
}
78     }
79     
80     /** Creates a new instance of ImportDirectory */
81     public ImportDirectory(File JavaDoc importRoot, File JavaDoc toDir) {
82         this(importRoot, toDir, false);
83     }
84     
85     /** Creates a new instance of ImportDirectory */
86     public ImportDirectory(File JavaDoc importRoot, File JavaDoc 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")); //NOI18N
107
ph.start();
108         ph.switchToIndeterminate();
109         try{
110             infoCollector = new InfoCollector(importRoot);
111             if(infoCollector.hasReports()){
112                 if(infoCollector.hasErrors()){
113                     //show error and exit
114
String JavaDoc errorMess = NbBundle.getMessage(ImportDirectory.class,
115                             "MSG_directory_closure_error"); //NOI18N
116
NotifyDescriptor.Message ndm = new NotifyDescriptor.Message(errorMess, NotifyDescriptor.Message.ERROR_MESSAGE);
117                     opErrors();
118                     DialogDisplayer.getDefault().notify(ndm);
119                     //System.out.printf("\n\nErrors(%d): %s\n\n",infoCollector.getErrors().size(), infoCollector.getErrors());
120
return;
121                 }
122                 if(infoCollector.hasWarnings()){
123                     //show warning message and ask the user to quit or copy
124
//if quit return
125
//else continue copy
126
String JavaDoc warningMess = NbBundle.getMessage(ImportDirectory.class,
127                             "MSG_absolute_resource_warning"); //NOI18N
128
String JavaDoc warningTitle = NbBundle.getMessage(ImportDirectory.class,
129                             "TITLE_absolute_resource_warning"); //NOI18N
130
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                     //System.out.printf("\n\nWarnings(%d): %s\n\n",infoCollector.getWarnings().size(),infoCollector.getWarnings());
139
//return;
140
}
141             } //start copying files
142
copyFiles();
143             //optionally show in o/p window
144
showCopiedFiles();
145         }finally{
146             ph.finish();
147         }
148         invokeRetrieverEngineIfRequired();
149     }
150     
151     Map JavaDoc<File JavaDoc, File JavaDoc> copiedFiles = new HashMap JavaDoc<File JavaDoc, File JavaDoc>();
152     Map JavaDoc<File JavaDoc, File JavaDoc> errorsWhileCopyFiles = new HashMap JavaDoc<File JavaDoc, File JavaDoc>();
153     
154     private void copyFiles() {
155         List JavaDoc<File JavaDoc> copyList = this.infoCollector.getCopyableFileList();
156         for(File JavaDoc srcFile: copyList){
157             //construct a file object
158
FileObject source = FileUtil.toFileObject(FileUtil.normalizeFile(srcFile));
159             
160             String JavaDoc impRootStr = this.importRoot.toURI().toString();
161             String JavaDoc toDirStr = this.toDir.toURI().toString();
162             StringBuffer JavaDoc strBuff = new StringBuffer JavaDoc(srcFile.toURI().toString());
163             String JavaDoc destStr = strBuff.replace(0, impRootStr.length()-1, toDirStr).toString();
164             
165             File JavaDoc destFile = null;
166             try {
167                 destFile = new File JavaDoc(new URI JavaDoc(destStr));
168             } catch (URISyntaxException JavaDoc ex) {
169                 continue;
170             }
171             
172             if(source == null){
173                 //gracefully continue with other files if the source is null
174
errorsWhileCopyFiles.put(srcFile, destFile);
175                 continue;
176             }
177             
178             if(destFile.isFile()){
179                 if(this.overWriteFiles){
180                     destFile.delete();
181                 }
182             }
183             
184             String JavaDoc fileName = destFile.getName();
185             fileName = fileName.substring(0, fileName.lastIndexOf("."));
186             File JavaDoc 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 JavaDoc 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")); //NOI18N
209
for(File JavaDoc 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")); //NOI18N
219
for(File JavaDoc 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 JavaDoc opTabTitle = NbBundle.getMessage(ImportDirectory.class,
228             "TITLE_retriever_output_tab_title");//NOI18N
229
private void opErrors() {
230         InputOutput io = IOProvider.getDefault().getIO(opTabTitle, false);
231         io.setErrSeparated(true);
232         OutputWriter error = io.getErr();
233         String JavaDoc errorMess =
234                 NbBundle.getMessage(ImportDirectory.class,"MSG_OUTPUT_directory_closure"); //NOI18N
235
Map JavaDoc<File JavaDoc, List JavaDoc<InfoCollector.InfoEntry>> errors = this.infoCollector.getErrors();
236         error.printf("\n%s (%d):\n", errorMess, errors.size()); //NOI18N
237
for(File JavaDoc file : errors.keySet()){
238             String JavaDoc msgFileStr = NbBundle.getMessage(ImportDirectory.class,
239                     "MSG_OUTPUT_file"); //NOI18N
240
error.printf("%s %s\n", msgFileStr, file.toString());
241             List JavaDoc<InfoCollector.InfoEntry> entList =errors.get(file);
242             String JavaDoc msgOverfloLoc =
243                     NbBundle.getMessage(ImportDirectory.class,
244                     "MSG_OUTPUT_overflowing_location"); //NOI18N
245
for(InfoCollector.InfoEntry ent : entList){
246                 error.printf(" %s %s\n", msgOverfloLoc, ent.getChildStr()); //NOI18N
247
}
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 JavaDoc errorMess =
257                 NbBundle.getMessage(ImportDirectory.class,
258                 "MSG_OUTPUT_absolute_resource"); //NOI18N
259
Map JavaDoc<File JavaDoc, List JavaDoc<InfoCollector.InfoEntry>> errors = this.infoCollector.getWarnings();
260         error.printf("\n%s (%d):\n", errorMess, errors.size()); //NOI18N
261
for(File JavaDoc file : errors.keySet()){
262             String JavaDoc msgFileStr =
263                     NbBundle.getMessage(ImportDirectory.class, "MSG_OUTPUT_file"); //NOI18N
264
error.printf("%s %s\n", msgFileStr, file.toString());
265             List JavaDoc<InfoCollector.InfoEntry> entList =errors.get(file);
266             String JavaDoc msgAbsLoc = NbBundle.getMessage(ImportDirectory.class,
267                     "MSG_OUTPUT_absolute_location"); //NOI18N
268
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 JavaDoc<File JavaDoc, List JavaDoc<InfoCollector.InfoEntry>> errors = this.infoCollector.getAbsURL2Info();
277         for(File JavaDoc file : errors.keySet()){
278             List JavaDoc<InfoCollector.InfoEntry> entList =errors.get(file);
279             for(InfoCollector.InfoEntry ent : entList){
280                 if(ent.getInfoType() == ent.infoType.url){
281                     String JavaDoc urlStr = ent.getChildStr();
282                     URL JavaDoc url = null;
283                     try {
284                         url = new URL JavaDoc(urlStr);
285                     } catch (MalformedURLException JavaDoc 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