View Javadoc

1   /*
2    * $Id: OWLEditorInput.java,v 1.12 2005/05/31 19:37:20 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.editor;
9   
10  import java.io.InputStream;
11  
12  import org.eclipse.core.resources.IFile;
13  import org.eclipse.core.runtime.CoreException;
14  import org.eclipse.jface.text.IRegion;
15  import org.eclipse.ui.part.FileEditorInput;
16  
17  import com.bbn.swede.core.IOWLDocument;
18  import com.bbn.swede.core.IOWLElement;
19  import com.bbn.swede.core.IOWLModel;
20  import com.bbn.swede.core.OWLCore;
21  import com.bbn.swede.core.dom.OAST;
22  import com.bbn.swede.core.dom.UnparseableFilterStream;
23  import com.bbn.swede.core.resources.SWResourceManager;
24  import com.hp.hpl.jena.rdf.model.Model;
25  
26  /***
27   * Customization of FileEditorInput that provides easy access to 
28   * SWeDE-specific information about the associated IFile.
29   * @author jlerner
30   */
31  public class OWLEditorInput extends FileEditorInput
32  {
33     /***
34      * The OWL document element represented by the editor input.
35      */
36     protected IOWLDocument _doc;
37     /***
38      * Creates an editor input based off the given file resources.
39      * @param file The file resource
40      */
41     public OWLEditorInput(IFile file)
42     {
43        super(file);
44        //If the file's project doesn't already have the SW nature, add it.
45        //Otherwise Eclipse will crash spectacularly due to lack of a
46        //.swprojectinfo file.
47        IOWLModel model = SWResourceManager.getModel();
48        try
49        {
50           if (!file.getProject().hasNature(OWLCore.OWL_NATURE))
51           {
52              OWLCore.addOWLNature(file.getProject());
53              model.build();
54           }
55        }
56        catch (CoreException e)
57        {
58           OWLCore.logWarning(EditorPlugin.getID(),
59              "Error verifying Semantic Web nature", e);
60        }
61        IOWLElement elem = model.getCorrespondingElement(file);
62        if (elem != null && elem instanceof IOWLDocument)
63        {
64           _doc = (IOWLDocument) elem;
65        }
66     }
67  
68     /***
69      * Provides access to the IOWLDocument that represents this editor input's
70      * file in the OWLModel.
71      * @return The IOWLDocument that corresponds to the editor input's file
72      */
73     public IOWLDocument getDocument()
74     {
75        return _doc;
76     }
77  
78     /***
79      * Provides access to the OAST for the IOWLDocument represented by this
80      * editor input.
81      * @return The OAST for this editor input's IOWLDocument
82      */
83     public OAST getOAST()
84     {
85        if (_doc == null)
86        {
87           return null;
88        }
89        return (OAST)_doc.getDocumentInfo().getOAST();
90     }
91  
92     /***
93      * Provides access to the Jena model for the IOWLDocument represented by this
94      * editor input.
95      * @return The com.hp.hpl.jena.rdf.model.Model for this editor input's
96      *         IOWLDocument
97      */
98     public Model getModel()
99     {
100       if (_doc == null)
101       {
102          return null;
103       }
104       return _doc.getDocumentInfo().getModel();
105    }
106 
107    /***
108     * Creates a clean input stream for the IOWLDocument represented by this
109     * editor input.  Since the resulting stream has unparseable regions blanked
110     * out, it is guarunteed to be parseable XML, but it may contain RDF
111     * errors.  
112     * @return An input stream for this editor input's file resource with the
113     *         unparseable regions blanked out.  The user must close the stream
114     *         when they are done with it.
115     * @throws CoreException if the file's contents cannot be read.
116     */
117    public InputStream getInputStream() throws CoreException
118    {
119       InputStream input = getFile().getContents();
120       IRegion[] regions = getOAST().getUnparseableRegions();
121       UnparseableFilterStream ufs = new UnparseableFilterStream(input, regions);
122       return ufs;
123    }
124 }