View Javadoc

1   /*
2    * $Id: TranslationRunner.java,v 1.3 2005/05/31 20:25:08 jlerner 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.io.OutputStream;
13  
14  import org.eclipse.core.resources.IFile;
15  
16  import com.bbn.swede.core.OWLCore;
17  
18  /***
19   * This is a utility class designed to wrap the run of a translation operation.
20   * It can be used by clients, but is not intended to be subclassed.
21   * 
22   * @author aperezlo
23   */
24  public class TranslationRunner extends Thread
25  {
26     private ITranslator _translator;
27     private OutputStream _output;
28     private String _inFormat;
29     private String _outFormat;
30     private File _input;
31     private Exception _exception;
32     private String _errorString;
33     
34     /***
35      * Create a new TranslationRunner for the given translator.
36      * 
37      * @param trans the translator that this runner will use
38      */
39     public TranslationRunner(ITranslator trans)
40     {
41        _translator = trans;
42     }
43     
44     /***
45      * Configure this translation runner with the arguments for a 
46      * single run of the given translator.
47      * 
48      * These arguments are passed directly to the {@link ITranslator#doTranslation(File, OutputStream, String, String)} 
49      * method, and have the same significance as do the parameters to that method.
50      * 
51      * @param input 
52      * @param output
53      * @param inputFormat
54      * @param outputFormat
55      * @see com.bbn.swede.tools.translator.ITranslator#doTranslation(File, OutputStream, String, String)
56      */
57     public void configure(File input, OutputStream output, String inputFormat, String outputFormat)
58     {
59        _input = input;
60        _output = output;
61        _inFormat = inputFormat;
62        _outFormat = outputFormat;
63        _errorString = null;
64        _exception = null;
65     }
66     
67     /***
68      * Configure this translation runner with the arguments for a 
69      * single run of the given translator.
70      * 
71      * These arguments are passed directly to the {@link ITranslator#doTranslation(IFile, OutputStream, String, String)} 
72      * method, and have the same significance as do the parameters to that method.
73      * 
74      * @param input 
75      * @param output
76      * @param inputFormat
77      * @param outputFormat
78      * @see com.bbn.swede.tools.translator.ITranslator#doTranslation(IFile, OutputStream, String, String)
79      */
80     public void configure(IFile input, OutputStream output, String inputFormat, String outputFormat)
81     {
82        File inputFile = null;
83        try
84        {
85           inputFile = new File(input.getFullPath().makeAbsolute().toOSString());
86           configure(inputFile, output, inputFormat, outputFormat);
87        }
88        catch(Exception e)
89        {
90           OWLCore.logWarning(TranslatorPlugin.getPlugin().getID(), "Problem with input file.", e);
91        }
92     }
93     
94     /***
95      * Retrieves the error string generated by the translator during the last 
96      * run.
97      * @return The error string, if any error was generated
98      */
99     public String getErrorString()
100    {
101       return _errorString;
102    }
103    
104    /***
105     * Retrieves the uncaught exception generated by the translator during the
106     * last run.
107     * @return The uncaught exception, if any was thrown.
108     */
109    public Exception getException()
110    {
111       return _exception;
112    }
113    
114    /***
115     * Run this translation as a separate thread.
116     * 
117     * @see java.lang.Runnable#run()
118     */
119    public void run()
120    {
121       runBlocked();
122    }
123    
124    /***
125     *  Run this translation and block the invoking thread until the 
126     *  translation is finished. 
127     */
128    public void runBlocked()
129    {
130       _exception = null;
131       _errorString = null;
132       
133       try
134       {
135          _errorString = _translator.doTranslation(_input, _output, _inFormat, _outFormat);
136          _output.flush();
137          _output.close();
138       }
139       catch(Exception e)
140       {
141          _exception = e;
142       }
143    }
144 }