View Javadoc

1   /*
2    * $Id: OWLModel.java,v 1.11 2005/05/31 16:26:41 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.core.resources;
9   
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.StringTokenizer;
14  
15  import org.eclipse.core.resources.ICommand;
16  import org.eclipse.core.resources.IContainer;
17  import org.eclipse.core.resources.IFolder;
18  import org.eclipse.core.resources.IProject;
19  import org.eclipse.core.resources.IProjectDescription;
20  import org.eclipse.core.resources.IResource;
21  import org.eclipse.core.resources.IWorkspace;
22  import org.eclipse.core.resources.IWorkspaceRoot;
23  import org.eclipse.core.resources.ResourcesPlugin;
24  import org.eclipse.core.runtime.CoreException;
25  import org.eclipse.core.runtime.QualifiedName;
26  import org.eclipse.jface.dialogs.MessageDialog;
27  
28  import com.bbn.swede.core.IOWLClass;
29  import com.bbn.swede.core.IOWLElement;
30  import com.bbn.swede.core.IOWLExternalDocument;
31  import com.bbn.swede.core.IOWLFolder;
32  import com.bbn.swede.core.IOWLModel;
33  import com.bbn.swede.core.IOWLProject;
34  import com.bbn.swede.core.IOWLProperty;
35  import com.bbn.swede.core.OWLCore;
36  import com.bbn.swede.core.OWLElementInfo;
37  import com.bbn.swede.core.OWLNature;
38  import com.bbn.swede.core.config.SWProjectInfo;
39  import com.bbn.swede.core.libraries.Libraries;
40  
41  
42  /***
43   * Implementation for the OWL model element.
44   * Creates and manages the OWL element hierarchy based on the current Eclipse
45   * workspace.  Only projects with the OWL nature and OWL documents within those
46   * projects are included in this hierarchy.
47   * @author jlerner
48   */
49  public class OWLModel extends OWLElement implements IOWLModel
50  {
51     /***
52      * The singleton instance of the model.
53      */
54     protected static OWLModel _model; // = new OWLModel();
55     
56     /***
57      * Creates an OWL model.
58      */
59     protected OWLModel()
60     {
61        this._elemParent = null;
62        this._sElementName = "";
63     }
64  
65     /***
66      * Returns the one and only instance of OWLModel.
67      * @return The singleton instance
68      */
69     static OWLModel getModel()
70     {
71        if (_model == null)
72        {
73           _model = new OWLModel();
74           _model.build();
75        }
76        return _model;
77     }
78  
79     /***
80      * Searches the OWL model for a project.
81      * @param sProjectName The name of the project to locate
82      * @return The OWL project named <code>sProjectName</code>, or 
83      *         <code>null</code> if the model does not contain a project by that
84      *         name.
85      */
86     public IOWLProject getOWLProject(String sProjectName)
87     {
88        IOWLProject[] projects = getOWLProjects();
89        for (int i = 0; i < projects.length; i++)
90        {
91           if (projects[i].getElementName().equals(sProjectName))
92           {
93              return projects[i];
94           }
95        }
96        return null;
97     }
98  
99     /***
100     * Retrieves all the OWL projects in the model.
101     * @return An array of all OWL projects in the model.
102     */
103    public IOWLProject[] getOWLProjects()
104    {
105       List lProjects = new ArrayList();
106       IOWLElement[] elems = getElementInfo().getChildren();
107       for (int i = 0; i < elems.length; i++)
108       {
109          if (elems[i] instanceof IOWLProject)
110          {
111             lProjects.add(elems[i]);
112          }
113       }
114       IOWLProject[] projects = new IOWLProject[lProjects.size()];
115       System.arraycopy(lProjects.toArray(), 0, projects, 0, projects.length);
116       return projects;
117    }
118    
119    /***
120     * Returns all IOWLExternalDocuments within a project.
121     * @param proj Project to search
122     * @return IOWLExternalDocument[] array of OWLExternalDocuments
123     */
124    public IOWLExternalDocument[] getImportedDocuments(IOWLProject proj)
125    {
126       IOWLElement[] elems =
127          SWResourceManager.findChildrenOfType(proj, IOWLExternalDocument.class);
128       IOWLExternalDocument[] docs = new IOWLExternalDocument[elems.length];
129       System.arraycopy(elems, 0, docs, 0, docs.length);
130       return docs;
131    }
132    
133    /***
134     * Finds the IOWLExternalDocument within a project with the given URI.
135     * @param proj IOWLProject to search
136     * @param uri URI String of specific URI
137     * @return IOWLExternalDocument requested OWLExternalDocument
138     */
139    public IOWLExternalDocument getImportedDocument(
140       IOWLProject proj,
141       String uri)
142    {
143       IOWLExternalDocument[] docs = getImportedDocuments(proj);
144       for (int i = 0; i < docs.length; i++)
145       {
146          if (docs[i].getURI().equals(uri))
147          {
148             return docs[i];
149          }
150       }
151       return null;
152    }
153 
154    /***
155     * Retrieves the Eclipose workspace the OWL model is based on.
156     * @return The workspace
157     */
158    public IWorkspace getWorkspace()
159    {
160       return _workspaceRoot.getWorkspace();
161    }
162 
163    /*
164     *  (non-Javadoc)
165     * @see com.bbn.swede.core.IOWLElement#contains(org.eclipse.core.resources.IResource)
166     */
167    public boolean contains(IResource resource)
168    {
169       IOWLProject[] owlProjects = getOWLProjects();
170       for (int i = 0; i < owlProjects.length; i++)
171       {
172          IOWLProject proj = (IOWLProject) owlProjects[i];
173          if (proj.contains(resource))
174          {
175             return true;
176          }
177       }
178       return false;
179    }
180 
181    /***
182     * A reference to the root of the Eclipse workspace, purely for convenience.
183     */
184    protected IWorkspaceRoot _workspaceRoot;
185 
186    /***
187     * Builds the OWL model.  Only the first level of the model (i.e. the 
188     * projects) is determined, as further levels of detail are not needed until
189     * element info is requested for a project.
190     */
191    public void build()
192    {
193       IWorkspace workspace = ResourcesPlugin.getWorkspace();
194       _workspaceRoot = workspace.getRoot();
195       _info = new OWLElementInfo(this);
196 
197       IProject[] projects = _workspaceRoot.getProjects();
198       for (int i = 0; i < projects.length; i++)
199       {
200          try
201          {
202             //projects[i].build(IncrementalProjectBuilder.FULL_BUILD, null);
203             if (projects[i].hasNature(OWLCore.OWL_NATURE))
204             {
205                //checkBuilders(projects[i]);
206                _info.addChild((IOWLElement)SWResourceManager.create(projects[i]));
207             }
208          }
209          catch (CoreException e)
210          {
211             OWLCore.logWarning(OWLCore.getID(), "Unable to check natures of "
212                + projects[i].getName(), e);
213          }
214       }
215       
216       ResourcesPlugin.getWorkspace().addResourceChangeListener(new ModelUpdater());
217    }
218 
219    /*
220     *  (non-Javadoc)
221     * @see com.bbn.swede.core.IOWLModel#addLibrary(com.bbn.swede.core.IOWLProject, java.lang.String)
222     */
223    public boolean addLibrary(IOWLProject project, String libraryNameToAdd)
224    {
225       boolean toReturn = false;
226       IProject eclipseProject = project.getProject();
227       String currentLibraries = null;
228       StringTokenizer st = null;
229       QualifiedName qName = new QualifiedName(Libraries.SESSION_PROPERTY_QUALIFIER, eclipseProject.getName());
230       String newLibraryCollection = "";
231       try
232       {
233          currentLibraries = (String) eclipseProject.getSessionProperty(qName);
234          if(currentLibraries != null)
235          {
236             if(Libraries.getLibrary(libraryNameToAdd) != null)
237             {
238                toReturn = true;
239                newLibraryCollection += libraryNameToAdd + Libraries.SESSION_PROPERTY_DELIMETER;
240                st = new StringTokenizer(currentLibraries, Libraries.SESSION_PROPERTY_DELIMETER);
241                while(st.hasMoreTokens())
242                {
243                   newLibraryCollection += st.nextToken() + Libraries.SESSION_PROPERTY_DELIMETER;
244                }
245                eclipseProject.setSessionProperty(qName, newLibraryCollection);
246             }
247          }
248          else
249          {
250             toReturn = true;
251             currentLibraries = newLibraryCollection += libraryNameToAdd + Libraries.SESSION_PROPERTY_DELIMETER;
252             eclipseProject.setSessionProperty(qName, newLibraryCollection);
253          }
254       }
255       catch (CoreException e)
256       {
257          e.printStackTrace();
258       }
259       if(toReturn)
260       {
261          SWProjectInfo.refreshInfoFile(eclipseProject);
262       }
263       return toReturn;
264    }
265    /*
266     *  (non-Javadoc)
267     * @see com.bbn.swede.core.IOWLModel#removeLibrary(com.bbn.swede.core.IOWLProject, java.lang.String)
268     */
269    public boolean removeLibrary(IOWLProject project, String libraryNameToRemove)
270    {
271       boolean toReturn = false;
272       IProject eclipseProject = project.getProject();
273       String currentLibraries = null;
274       StringTokenizer st = null;
275       QualifiedName qName = new QualifiedName(Libraries.SESSION_PROPERTY_QUALIFIER, eclipseProject.getName());
276       String newLibraryCollection = "";
277       String temp = null;
278       try
279       {
280          currentLibraries = (String) eclipseProject.getSessionProperty(qName);
281          if(currentLibraries != null)
282          {
283             
284             st = new StringTokenizer(currentLibraries, Libraries.SESSION_PROPERTY_DELIMETER);
285             while(st.hasMoreTokens())
286             {
287                temp = st.nextToken();
288                if(!temp.equals(libraryNameToRemove))
289                {
290                   newLibraryCollection += st.nextToken() + Libraries.SESSION_PROPERTY_DELIMETER;
291                }
292                else
293                {
294                   toReturn = true;
295                }
296             }
297             eclipseProject.setSessionProperty(qName, newLibraryCollection);
298          }
299       }
300       catch (CoreException e)
301       {
302          e.printStackTrace();
303       }
304       
305       if(toReturn)
306       {
307          SWProjectInfo.refreshInfoFile(eclipseProject);
308       }      
309       
310       return toReturn;
311    }
312    /***
313     * Checks a SWeDE project to ensure that it has the syntax and config 
314     * builders.  If either builder is missing, the user is prompted to give
315     * permission to add it.
316     * @param project The project to check
317     */
318    private void checkBuilders(IProject project) throws CoreException
319    {
320       IProjectDescription desc = project.getDescription();
321       ICommand[] commands = desc.getBuildSpec();
322       
323       boolean hasSyntax = false;
324       boolean hasConfig = false;
325       
326       for(int i = 0; (i < commands.length) && (!hasSyntax); i++)
327       {
328          if(commands[i].getBuilderName().equals(OWLCore.SYNTAX_BUILDER))
329          {
330             hasSyntax = true;
331          }
332       }
333       
334       for(int i = 0; (i < commands.length) && (!hasConfig); i++)
335       {          
336          if(commands[i].getBuilderName().equals(OWLCore.CONFIG_BUILDER))
337          {
338             hasConfig = true;
339          }
340       }
341       
342       if(!hasSyntax)
343       {
344          // didn't find the builder. Request permission to add
345          if(MessageDialog.openQuestion(
346                null, 
347                "Add Syntax Validation?", 
348                "The SWeDE Project, " + project.getName() + ", does not "
349                      + "currently have OWL Syntax Validation activated.\n"
350                      + "Do you want to activate it?"))
351          {
352             OWLNature.addOWLSyntaxBuilder(project);
353          }
354       }
355       
356       if(!hasConfig)
357       {
358          // didn't find the builder. request permission to add
359          if(MessageDialog.openConfirm(
360                null, 
361                "Migrate Project Configuration...", 
362                "The SWeDE Project, " + project.getName() + ", does not "
363                      + "currently support persistent project information.  "
364                      + "This may be because you created this project with a "
365                      + "previous version of SWeDE."
366                      + " You may need to re-import any external documents in "
367                      + "your project."))
368          {
369             OWLNature.addSWProjectInfoBuilder(project);
370          }
371       }      
372    }
373 
374    /*
375     *  (non-Javadoc)
376     * @see com.bbn.swede.core.IOWLElement#getCorrespondingElement(org.eclipse.core.resources.IResource)
377     */
378    public IOWLElement getCorrespondingElement(IResource resource)
379    {
380       IOWLProject[] owlProjects = getOWLProjects();
381       for (int i = 0; i < owlProjects.length; i++)
382       {
383          IOWLProject proj = (IOWLProject) owlProjects[i];
384          IOWLElement elem = proj.getCorrespondingElement(resource);
385          if (elem != null)
386          {
387             return elem;
388          }
389       }
390       
391       return null;
392    }
393    /***
394     * Adds an OWL Class into model.
395     * @param c IOWLClass object
396     * @return IOWLElement representing the class
397     */
398    private IOWLElement add(IOWLClass c)
399    {
400       return null;
401    }
402    /***
403     * Adds an OWL Property into model.
404     * @param p IOWLProperty object
405     * @return IOWLelement representing the property
406     */
407    private IOWLElement add(IOWLProperty p)
408    {
409       return null;
410    }
411    /***
412     * Adds any IOWLElement (except IOWLProject) to the model.
413     * @param e IOWLElement object
414     * @return IOWLElement representing the object added 
415     */
416    public IOWLElement add(IOWLElement e)
417    {
418       if (e == null)
419       {
420          return null;
421       }
422       if (e instanceof IOWLProject)
423       {
424          return null;
425       }
426       if (e instanceof IOWLClass)
427       {
428          return add((IOWLClass) e);
429       }
430       if (e instanceof IOWLProperty)
431       {
432          return add((IOWLProperty) e);
433       }
434       // if we're here, this must be an OWLFolder or OWLDocument
435       IResource res = e.getResource();
436       if (contains(res))
437       {
438          return e;
439       }
440       IContainer parent = res.getParent();
441       if (!contains(parent)) // might be a parent folder we need to add first
442       {
443          if (parent instanceof IProject)
444             // parent is a project but not an OWL project
445          {
446             return null;
447          }
448          // okay, parent must be an IFolder, but just in case...
449          if (!(parent instanceof IFolder))
450          {
451             return null;
452          }
453          IOWLFolder f = SWResourceManager.create((IFolder) parent);
454          if (f != null) // folders automatically add children
455          {
456             add((IOWLElement) f);
457             f.getElementInfo(); // forces children to be read
458             return e;
459          }
460          else
461          {
462             return null;
463          }
464       }
465       IOWLElement parentElement = getCorrespondingElement(parent);
466       parentElement.getElementInfo().addChild((IOWLElement) e);
467       return e;
468    }
469    
470    /*
471     *  (non-Javadoc)
472     * @see com.bbn.swede.core.IOWLModel#makeOWLFolder(org.eclipse.core.resources.IFolder)
473     */
474    public IOWLFolder makeOWLFolder(IFolder folder)
475    {
476       return null;
477    }
478    private void trace(String s)
479    {
480       trace(s, false);
481    }
482    private void trace(String s, boolean error)
483    {
484       OWLCore.trace("OWLModel", s, error);
485    }
486 }