View Javadoc

1   /*
2    * $Id: SemanticWebTranslationExportWizard.java,v 1.3 2005/06/01 17:47:51 aperezlo Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.tools.translator;
10  
11  import java.io.File;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.eclipse.core.resources.IFile;
16  import org.eclipse.core.runtime.IPath;
17  import org.eclipse.core.runtime.IProgressMonitor;
18  import org.eclipse.core.runtime.IStatus;
19  import org.eclipse.core.runtime.Path;
20  import org.eclipse.core.runtime.Status;
21  import org.eclipse.jface.dialogs.ErrorDialog;
22  import org.eclipse.jface.dialogs.MessageDialog;
23  import org.eclipse.jface.viewers.IStructuredSelection;
24  import org.eclipse.jface.wizard.IWizardPage;
25  import org.eclipse.jface.wizard.Wizard;
26  import org.eclipse.ui.IExportWizard;
27  import org.eclipse.ui.IWorkbench;
28  import org.eclipse.ui.actions.WorkspaceModifyOperation;
29  
30  import com.bbn.swede.core.OWLCore;
31  
32  /***
33   * A wizard that allows users to export documents in a given format from a SWeDE
34   * project to the filesystem in a different format. For instance, this wizard
35   * could allow a user to generate an OWL N-Triples representation of the
36   * documents in a project that are in OWL RDF/XML. <br>
37   * <br>
38   * This class is intended to be instantiated and used, but not to be subclassed.
39   * 
40   * @author aperezlo
41   */
42  public class SemanticWebTranslationExportWizard extends Wizard implements
43     IExportWizard
44  {
45     /***
46      * The <code>_helper</code> is a utility class that performs much of the
47      * work of this wizard.
48      */
49     protected SemanticWebTranslatorWizardHelper _helper;
50  
51     /*
52      * @see org.eclipse.ui.IWorkbenchWizard#init(
53      *    org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
54      */
55     public void init(IWorkbench workbench, IStructuredSelection selection)
56     {
57        _helper = new SemanticWebTranslatorWizardHelper(
58           SemanticWebTranslatorWizardHelper.EXPORT, workbench, selection);
59        Iterator i = null;
60        setWindowTitle("Export Semantic Web Documents");
61  
62        i = _helper.getPages().iterator();
63        while (i.hasNext())
64        {
65           addPage((IWizardPage)i.next());
66        }
67     }
68  
69     /***
70      * Performs a translation and export - uses a WorkspaceModifyOperation 
71      * so as to conform with Eclipse best-practices.
72      * 
73      * @see org.eclipse.jface.wizard.IWizard#performFinish()
74      */
75     public boolean performFinish()
76     {
77        try
78        {
79           (new WorkspaceModifyOperation()
80           {
81              protected void execute(IProgressMonitor progMonitor)
82              {
83                 List inputFiles = _helper.getInputFiles();
84                 IPath outputPath = _helper.getOutputPath();
85                 List relevantTranslators = _helper.getTranslatorDescriptors();
86                 TranslatorDescriptor translator = null;
87                 Iterator i = inputFiles.iterator();
88                 Iterator translatorIterator = null;
89                 IFile inFile = null;
90                 IStatus status = null;
91                 File outputDirectory = null;
92                 /*
93                  * 
94                  * Take an input file, get a project relative path, prepend the
95                  * output path location, use the input file's extension to get
96                  * the proper translator, then replace the input file's extension
97                  * w/ the output extension from the translator. The resulting
98                  * path should be IPath.toOSString'ed to create a java.io.File.
99                  * 
100                 * Use File.createNewFile() to create the file (and maybe the
101                 * directories above it!? (if not, we'll have to go through and
102                 * do those manually...)
103                 * 
104                 * Once the file's finished, toss the infile, outpath and
105                 * translator to the _helper, who'll do the rest...
106                 * 
107                 * 
108                 * Don't forget to put a progress monitor in on the wizard page,
109                 * and then to get it passed to this guy, and used properly...
110                 */
111                progMonitor.beginTask("Exporting " + inputFiles.size()
112                   + " resource" + (inputFiles.size() > 1 ? "s" : ""),
113                   inputFiles.size());
114                TranslationRunner result = null;
115                while (i.hasNext())
116                {
117                   inFile = (IFile)i.next();
118                   translator = null;
119                   translatorIterator = relevantTranslators.iterator();
120                   while (translatorIterator.hasNext())
121                   {
122                      translator = (TranslatorDescriptor)translatorIterator
123                         .next();
124                      if(translator.getInputExtension().equals(
125                         inFile.getFileExtension()))
126                      {
127                         break;
128                      }
129                   }
130                   outputDirectory = new File(outputPath.append(
131                      inFile.getProjectRelativePath()).removeLastSegments(1)
132                         .toOSString());
133                   try
134                   {
135                      outputDirectory.mkdirs();
136                      result = _helper.performTranslation(inFile, new Path(
137                         outputDirectory.toString()), translator);
138                   }
139                   catch (Exception e)
140                   {
141                      status = new Status(IStatus.ERROR, TranslatorPlugin
142                         .getPlugin().getID(), IStatus.OK, e.getMessage(), e);
143                      ErrorDialog.openError(getShell(),
144                         "SWeDE Translation Error",
145                         "An error was encountered while trying to translate '"
146                            + inFile.getName() + "'.", status);
147                      OWLCore.logWarning(TranslatorPlugin.getPlugin().getID(),
148                         "Translation operation failed.", e);
149                   }
150                   if(result != null)
151                   {
152                      if(result.getErrorString() != null)
153                      {
154                         MessageDialog.openError(getShell(),
155                            "SWeDE Translation Error",
156                            "An error was encountered while trying to translate '"
157                               + inFile.getName()
158                               + "'.\n  The translator reported:\n"
159                               + result.getErrorString());
160                      }
161                      if(result.getException() != null)
162                      {
163                         status = new Status(IStatus.ERROR, TranslatorPlugin
164                            .getPlugin().getID(), IStatus.OK,
165                            "Unexpected Translation Error", result
166                               .getException());
167                         ErrorDialog.openError(getShell(),
168                            "SWeDE Translation Error",
169                            "An unexpected error was encountered while trying to translate '"
170                               + inFile.getName() + "'.", status);
171                      }
172                   }
173                   progMonitor.worked(1);
174                }
175                progMonitor.done();
176             }
177          }).run(_helper.getProgressMonitor());
178       }
179       catch (Exception e)
180       {
181          e.printStackTrace();
182       }
183 
184       return true;
185    }
186 }