View Javadoc

1   /*
2    * $Id: JenaTranslator.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.FileInputStream;
13  import java.io.InputStream;
14  import java.io.OutputStream;
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import org.eclipse.core.resources.IFile;
19  
20  import com.bbn.swede.core.IOWLDocument;
21  import com.hp.hpl.jena.rdf.model.Model;
22  import com.hp.hpl.jena.rdf.model.ModelFactory;
23  
24  /***
25   * Initial example implementation of a SWeDE translator. <br>
26   * <br>
27   * This translator uses the Jena libraries to support translations in both
28   * directions among RDF/XML, N3 and N-Triples.
29   * 
30   * 
31   * @author aperezlo
32   */
33  public class JenaTranslator extends AbstractTranslator implements ITranslator
34  {
35  
36     /***
37      * A Jena specific string specifying that
38      * the input (or output) should be in RDF/XML.
39      */
40     private static final String RDF_XML_JENA_STR = "RDF/XML-ABBREV";
41  
42     /***
43      * A Jena specific string specifying that the
44      * input (or output) should be in N3.
45      */
46     private static final String N3_JENA_STR = "N3";
47  
48     /***
49      * A Jena specific string specifying that
50      * the input (or output) should be in N-Triples.
51      */
52     private static final String NTRIPLE_JENA_STR = "N-TRIPLE";
53  
54     /***
55      * <code>__extensionsToJenaNames</code>- a map relating the file
56      * extensions that this translator has agreed to recognize (as specified in
57      * the plugin.xml file) to Jena specific names. <br>
58      * <br>
59      * In this case, owl -> RDF/XML, n3 -> N3, nt -> N-TRIPLE
60      */
61     private Map _extensionsToJenaNames;
62  
63     /***
64      * Default constructor.
65      */
66     public JenaTranslator()
67     {
68        _extensionsToJenaNames = new HashMap();
69        _extensionsToJenaNames.put("owl", RDF_XML_JENA_STR);
70        _extensionsToJenaNames.put("n3", N3_JENA_STR);
71        _extensionsToJenaNames.put("nt", NTRIPLE_JENA_STR);
72     }
73  
74     /***
75      * This method is specified by the ITranslator interface, and 
76      * it is the method that actually does the translation.  There 
77      * is a lot more explanation in the interface itself, so 
78      * please refer there for mor information.
79      * 
80      * @see com.bbn.swede.tools.translator.ITranslator#doTranslation(
81      *    java.io.File, java.io.OutputStream, java.lang.String, java.lang.String)
82      */
83     public String doTranslation(File inputFile, OutputStream outputStream,
84        String inputFormatExtension, String outputFormatExtension)
85        throws Exception
86     {
87        String errorString = null;
88        Model mod = null;
89        InputStream inputStream = null;
90  
91        // Try to get a pre-parsed IOWLDocument if one already exists
92        IOWLDocument owlDocument = getOWLDocumentFor(inputFile);
93        IFile iFile = null;
94        try
95        {
96           // If an IOWLDocument already exists for the input file
97           if(owlDocument != null)
98           {
99              // Use this model for translation...
100             mod = owlDocument.getDocumentInfo().getModel();
101          }
102          else
103          {
104             /*
105              * If no IOWLDocument exists, we must create a new Jena model. An
106              * IOWLDocument will not exist for a given file in two situations:
107              * 
108              * 1) the requested file is a valid Eclipse resource, but does not
109              * have an associated IOWLDocument, or 2) the requested file does
110              * not exist in this Eclipse workspace
111              */
112             iFile = getIFileFor(inputFile);
113             if(iFile != null)
114             {
115                /*
116                 * if this is a valid Eclipse file, we can ask for its
117                 * contents...
118                 */
119                inputStream = iFile.getContents();
120             }
121             else
122             {
123                /*
124                 * we must read this file directly from the filesystem...
125                 */
126                inputStream = new FileInputStream(inputFile);
127             }
128             mod = ModelFactory.createDefaultModel();
129             mod.read(inputStream, "", (String)_extensionsToJenaNames
130                .get(inputFormatExtension));
131             inputStream.close();
132          }
133 
134          mod.write(outputStream, (String)_extensionsToJenaNames
135             .get(outputFormatExtension));
136          outputStream.close();
137       }
138       catch (Exception e)
139       {
140          /*
141           * A message describing the problem, which will be displayed to the
142           * user. If everything executed without issue, this should be null.
143           */
144          errorString = e.getMessage();
145       }
146       return errorString;
147    }
148 }