View Javadoc

1   /*
2    * $Id: AbstractTranslator.java,v 1.4 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.io.OutputStream;
13  
14  import org.eclipse.core.resources.IFile;
15  import org.eclipse.core.resources.ResourcesPlugin;
16  import org.eclipse.core.runtime.IPath;
17  import org.eclipse.core.runtime.Path;
18  
19  import com.bbn.swede.core.IOWLDocument;
20  import com.bbn.swede.core.resources.SWResourceManager;
21  
22  /***
23   * This is the default implementation of the ITranslator interface.  
24   * It provides some utility methods to make it easier to implement 
25   * translators, and is intended to be subclassed - clients must 
26   * implement:
27   * <ul>
28   *    <li>doTranslation(File inputFile, OutputStream outputStream, 
29   *       String inputFormatExtension, String outputFormatExtension)</li>
30   * </ul> 
31   * 
32   * To access the prexisting OWL representation of the file to translate, 
33   * use the method #getOWLDocumentFor(File) or #getOWLDocumentFor(IFile)  
34   * 
35   * @author aperezlo
36   * @see com.bbn.swede.core.IOWLDocument
37   */
38  public abstract class AbstractTranslator implements ITranslator
39  {
40  
41     /***
42      * Gets a SWeDE IOWLDocument if one exists for the given IFile.
43      * This method returns an IOWLDocument if one exists, and null 
44      * otherwise. 
45      * 
46      * @param file the IFile
47      * @return an IOWLDocument associated with <code>file</code> if one exists, or null otherwise
48      */
49     protected IOWLDocument getOWLDocumentFor(IFile file)
50     {
51        IOWLDocument owlDocument = null;
52        if(file != null)
53        {
54           owlDocument = (IOWLDocument) SWResourceManager.getModel().getCorrespondingElement(file);
55        }
56        return owlDocument;
57     }
58     /***
59      * Gets a SWeDE IOWLDocument if one exists for the given File.
60      * This method returns an IOWLDocument if one exists, and null 
61      * otherwise. 
62      * 
63      * @param file the IFile
64      * @return an IOWLDocument associated with <code>file</code> if one exists, or null otherwise
65      */
66     protected IOWLDocument getOWLDocumentFor(File file)
67     {
68        IOWLDocument toReturn = null;
69        IFile inputFile =  getIFileFor(file);
70        toReturn = getOWLDocumentFor(inputFile);
71        return toReturn;
72     }
73     
74     
75     /***
76      * Gets an IFile for the particular File object.
77      * 
78      * This method will only return an IFile if it can be said to 
79      * <code>exist()</code>, which means that an open Eclipse project 
80      * owns a matching resource.  If the IFile can be created for the 
81      * file, but the file does not exist, this method returns <code>null</code>. 
82      * 
83      * @param javaFile a java.io.File for which an IFile is desired.
84      * @return an IFile for the particular <code>javaFile</code> if one exists, <code>null</code> otherwise
85      */
86     protected IFile getIFileFor(File javaFile)
87     {
88        IPath inputPath = null;
89        IFile toReturn = null;
90        IFile file = null;
91        if(javaFile != null)
92        {
93           inputPath = new Path(javaFile.toString());
94           file = ResourcesPlugin.getWorkspace().getRoot().getFile(inputPath);
95           if(file.exists())
96           {
97              toReturn = file;
98           }
99        }
100       return toReturn;
101    }
102    
103    /***
104     * Convenience method to transform an IFile into its matching java.io.File
105     * representation. This method returns null if there is no file in the
106     * filesystem that matches the absolute full path of the submitted
107     * <code>iFile</code>.
108     * 
109     * @param iFile the IFile for which the local java.io.File is desired
110     * @return the corresponding java.io.File for <code>iFile</code> if it
111     *         exists on the filesystem, <code>null</code> otherwise
112     */
113    protected File getFileFor(IFile iFile)
114    {
115       File toReturn = null;
116 
117       try
118       {
119          toReturn = new File(iFile.getFullPath().makeAbsolute().toOSString());
120       }
121       catch(Exception e)
122       {
123          //do nothing
124       }
125       return toReturn;
126    }
127    
128    /*
129     * @see com.bbn.swede.tools.translator.ITranslator#doTranslation(
130     *    org.eclipse.core.resources.IFile, java.io.OutputStream, java.lang.String, java.lang.String)
131     */
132    public String doTranslation(IFile inputFile, OutputStream outputStream,
133       String inputFormatExtension, String outputFormatExtension)
134       throws Exception
135    {
136       return doTranslation(getFileFor(inputFile), outputStream, inputFormatExtension, outputFormatExtension);
137    }
138 
139    /*
140     * @see com.bbn.swede.tools.translator.ITranslator#doTranslation(
141     *    java.io.File, java.io.OutputStream, java.lang.String, java.lang.String)
142     */
143    public abstract String doTranslation(File inputFile, OutputStream outputStream,
144       String inputFormatExtension, String outputFormatExtension)
145       throws Exception;
146 
147 }