View Javadoc

1   /*
2    * $Id: VocabularyGenerator.java,v 1.4 2005/06/01 14:36:07 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   package com.bbn.swede.tools.codegenerator;
9   
10  import java.io.File;
11  import java.net.MalformedURLException;
12  import java.util.ArrayList;
13  
14  import jena.schemagen;
15  
16  import org.eclipse.core.resources.IFolder;
17  import org.eclipse.core.resources.IProject;
18  import org.eclipse.core.resources.IResource;
19  import org.eclipse.core.resources.IncrementalProjectBuilder;
20  import org.eclipse.core.runtime.CoreException;
21  import org.eclipse.core.runtime.IPath;
22  import org.eclipse.core.runtime.IProgressMonitor;
23  import org.eclipse.core.runtime.Path;
24  import org.eclipse.core.runtime.SubProgressMonitor;
25  import org.eclipse.jdt.core.IClasspathEntry;
26  import org.eclipse.jdt.core.IJavaProject;
27  import org.eclipse.jdt.core.JavaCore;
28  import org.eclipse.jdt.core.JavaModelException;
29  
30  import com.bbn.jena.JenaPlugin;
31  import com.bbn.swede.core.IOWLDocument;
32  import com.bbn.swede.core.IOWLLibrary;
33  import com.bbn.swede.core.OWLCore;
34  import com.bbn.swede.core.OWLDocumentInfo;
35  import com.bbn.swede.core.libraries.ILibraryDescriptor;
36  import com.bbn.swede.core.libraries.LibraryOWLDocument;
37  import com.bbn.swede.tools.ToolsPlugin;
38  import com.bbn.swede.ui.UIPlugin;
39  import com.hp.hpl.jena.rdf.model.Model;
40  
41  /***
42   * Generates Schemagen vocabularies for OWL documents.  Generated vocabulary 
43   * classes will be placed in the <code>vocab</code> package with a class name 
44   * that matches the ontology's file name, converted to a valid Java identifier 
45   * (i.e. my-ont.owl becomes the my_ont).
46   */
47  public class VocabularyGenerator extends AbstractCodeGenerator
48  {
49     /*
50      *  (non-Javadoc)
51      * @see com.bbn.swede.ui.AbstractCodeGenerator#configureProject(org.eclipse.core.resources.IProject)
52      */
53     public void configureProject(IProject project)
54     {
55        OWLCore.addJavaNature(project);
56        IJavaProject proj = JavaCore.create(project);
57  
58        try
59        {
60           JenaPlugin.addJenaToClasspath(proj);
61        }
62        catch (JavaModelException e)
63        {
64           OWLCore.logError(UIPlugin.getID(), "Classpath update failed", e);
65        }
66     }
67     
68     /*
69      *  (non-Javadoc)
70      * @see com.bbn.swede.ui.AbstractCodeGenerator#performGeneration(
71      *       com.bbn.swede.core.IOWLDocument, org.eclipse.core.resources.IProject, 
72      *       org.eclipse.core.runtime.IProgressMonitor)
73      */
74     public IPath performGeneration(IOWLDocument document, IProject project,
75        IProgressMonitor monitor) throws CoreException, InterruptedException
76     {
77        monitor.beginTask("Generating Jena vocabulary", 100);
78        OWLDocumentInfo info = document.getDocumentInfo();
79        Model model = info.getModel();
80        String sBase = document.getURI();
81        String localURI = document.getLocalURI();
82        
83        String sName = null;
84        
85        File tempDirectory = null;
86        if(document instanceof LibraryOWLDocument)
87        {
88           LibraryOWLDocument libraryDocument = (LibraryOWLDocument) document;
89           ILibraryDescriptor library = ((IOWLLibrary)libraryDocument.getParent()).getLibrary();
90           
91  
92           tempDirectory = new File(
93              new File(System.getProperty("java.io.tmpdir")), Long
94                 .toString(System.currentTimeMillis()));
95           tempDirectory.mkdir();
96           tempDirectory.deleteOnExit();
97           library.export(new Path(tempDirectory.toString()), library
98              .getDescriptorFor(libraryDocument.getElementName()),
99              new SubProgressMonitor(monitor, 5));
100          try
101          {
102             localURI = tempDirectory.toURL().toExternalForm() + libraryDocument.getElementName();
103          }
104          catch (MalformedURLException e)
105          {
106             OWLCore.logWarning(ToolsPlugin.getID(),
107                "Unable to construct local URI for " + tempDirectory, e);
108          }
109       }
110       
111       
112       sName = localURI;
113       int iPos = sName.lastIndexOf('/');
114       //TODO: allow user to set output filename
115       sName = sName.substring(iPos + 1);
116       iPos = sName.lastIndexOf(".owl");
117       sName = sName.substring(0, iPos).replace('-', '_');
118       String sUri = localURI;
119 
120       monitor.worked(10); // 10/100
121       if(monitor.isCanceled())
122       {
123          throw new InterruptedException();
124       }
125 
126       //TODO: allow user to select desired source folder
127       //      and possibly create a new one
128       IJavaProject proj = JavaCore.create(project);
129       IClasspathEntry[] entries = proj.getRawClasspath();
130       IClasspathEntry srcEntry = null;
131       for(int i = 0; i < entries.length; i++)
132       {
133          if(entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
134          {
135             srcEntry = entries[i];
136             break;
137          }
138       }
139       IFolder srcFolder = project.getFolder(srcEntry.getPath()
140          .removeFirstSegments(1));
141 
142       monitor.worked(10); // 20/100
143       if(monitor.isCanceled())
144       {
145          throw new InterruptedException();
146       }
147 
148       //TODO: allow user to select package name
149       IFolder folder = srcFolder.getFolder("/vocab");
150       if(!folder.exists())
151       {
152          folder.create(false, true, null);
153       }
154 
155       ArrayList args = new ArrayList();
156       args.add("-a");
157       args.add(sBase);
158       args.add("-n");
159       args.add(sName);
160       args.add("-i");
161       args.add(sUri);
162       args.add("-o");
163       args.add(folder.getRawLocation().toString());
164       args.add("--package");
165       args.add("vocab");
166       args.add("--ontology");
167       args.add("--owl");
168 
169       monitor.worked(10); // 30/100
170       if(monitor.isCanceled())
171       {
172          throw new InterruptedException();
173       }
174 
175       schemagen.main((String[])args.toArray(new String[0]));
176       monitor.worked(40); // 70/100
177       if(monitor.isCanceled())
178       {
179          throw new InterruptedException();
180       }
181       project.refreshLocal(IResource.DEPTH_INFINITE, null);
182       project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
183       monitor.worked(20); // 90/100
184       if(monitor.isCanceled())
185       {
186          throw new InterruptedException();
187       }
188       project.refreshLocal(IResource.DEPTH_INFINITE, null);
189       monitor.worked(10); // 100/100
190       if(monitor.isCanceled())
191       {
192          throw new InterruptedException();
193       }
194       
195       if(tempDirectory != null)
196       {
197          tempDirectory.listFiles()[0].delete();
198          tempDirectory.delete();
199       }
200       
201       monitor.done();
202       return folder.getFile(sName + ".java").getProjectRelativePath();
203    }
204 
205 }