View Javadoc

1   /*
2    * $Id: OWLDocument.java,v 1.8 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   import java.util.HashMap;
10  import java.util.Map;
11  
12  import org.eclipse.core.resources.IFile;
13  import org.eclipse.core.resources.IResource;
14  import org.eclipse.core.runtime.CoreException;
15  import org.eclipse.core.runtime.IPath;
16  import org.eclipse.ui.IActionFilter;
17  
18  import com.bbn.swede.core.IOWLDocument;
19  import com.bbn.swede.core.IOWLElement;
20  import com.bbn.swede.core.OWLCore;
21  import com.bbn.swede.core.OWLDocumentInfo;
22  import com.hp.hpl.jena.rdf.arp.MalformedURIException;
23  import com.hp.hpl.jena.rdf.arp.URI;
24  /***
25   * Implementation for OWL file elements.
26   * @author jlerner
27   */
28  public class OWLDocument extends OWLElement implements IOWLDocument
29  {
30     /***
31      * The base URI of the document.
32      */
33     protected String _uri;
34     /***
35      * A map containing the location of the output for any code generator run on
36      * this document.  The keys are the generator IDs from the plug-in manifest.
37      */
38     protected Map _mapGenLocs;
39     
40     /***
41      * Creates an OWL document element based on an Eclipse file resource.
42      * @param file The Eclipse file
43      */
44     OWLDocument(IFile file)
45     {
46        _resource = file;
47        setElementName(file.getName());
48        String uri = null;
49        try
50        {
51           uri = file.getPersistentProperty(OWLCore.PERSISTENT_URI);
52        }
53        catch (CoreException ce)
54        {
55           OWLCore.trace("OWLDocument()", "Error reading file metadata", true);
56        }
57        if (uri != null && OWLCore.isValidURI(uri)) // existing file
58        {
59           _uri = uri;
60        }
61        _mapGenLocs = new HashMap();
62     }
63  
64     /*
65      *  (non-Javadoc)
66      * @see com.bbn.swede.core.IOWLDocument#getDocumentInfo()
67      */
68     public OWLDocumentInfo getDocumentInfo()
69     {
70        return (OWLDocumentInfo) getElementInfo();
71     }
72     
73     /*
74      *  (non-Javadoc)
75      * @see com.bbn.swede.core.IOWLElement#getCorrespondingElement(org.eclipse.core.resources.IResource)
76      */
77     public IOWLElement getCorrespondingElement(IResource resource)
78     {
79        if (_resource.equals(resource))
80        {
81           return this;
82        }
83        return null;
84     }
85     
86     /*
87      *  (non-Javadoc)
88      * @see com.bbn.swede.core.IOWLDocument#getFile()
89      */
90     public IFile getFile()
91     {
92        if (getResource() instanceof IFile)
93        {
94           return (IFile) getResource();
95        }
96        return null;
97     }
98     
99     /*
100     *  (non-Javadoc)
101     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
102     */
103    public Object getAdapter(Class adapter)
104    {
105       if (adapter.equals(IFile.class))
106       {
107          return getFile();
108       }
109       if (adapter.equals(IActionFilter.class))
110       {
111          return _actionFilter;
112       }
113       return super.getAdapter(adapter);
114    }
115    
116    private static IActionFilter _actionFilter = new IActionFilter()
117    {
118       private static final String S_READ_ONLY_ATTRIBUTE = "read-only";
119       private static final String S_TRUE = "true";
120       private static final String S_FALSE = "false";
121 
122       public boolean testAttribute(Object target, String name, String value)
123       {
124          if (!(target instanceof IOWLDocument)
125             || !name.equals(S_READ_ONLY_ATTRIBUTE)
126             || (!value.equals(S_TRUE) && !value.equals(S_FALSE)))
127          {
128             return false;
129          }
130 
131          IOWLDocument doc = (IOWLDocument)target;
132          if (doc.getFile().isReadOnly())
133          {
134             return S_TRUE.equals(value);
135          }
136          else
137          {
138             return S_FALSE.equals(value);
139          }
140       }
141    };
142    
143    /*
144     *  (non-Javadoc)
145     * @see com.bbn.swede.core.IOWLElement#getURI()
146     */
147    public String getURI()
148    {
149       if (_uri == null)
150       {
151          return getLocalURI();
152       }
153       return _uri;
154    }
155    
156    /*
157     *  (non-Javadoc)
158     * @see com.bbn.swede.core.IOWLDocument#setURI(java.lang.String)
159     */
160    public void setURI(String uri) throws MalformedURIException
161    {
162       URI u = null;
163       if(uri != null)
164       {
165          try
166          {
167             u = new URI(uri);
168          }
169          catch (MalformedURIException me)
170          {
171             OWLCore.logWarning(OWLCore.getID(), "Cannot set URI. Malformed: " + uri, me);
172          }
173       }
174       try
175       {
176          getResource().setPersistentProperty(OWLCore.PERSISTENT_URI, uri);
177       }
178       catch (CoreException ce)
179       {
180          OWLCore.logWarning(
181             OWLCore.getID(),
182             "Unable to set persistent property",
183             ce);
184       }
185       _uri = uri;
186    }
187    
188    /*
189     *  (non-Javadoc)
190     * @see com.bbn.swede.core.IOWLElement#getLocalURI()
191     */
192    public String getLocalURI()
193    {
194       return _resource.getRawLocation().toFile().toURI().toString();
195    }
196    /* (non-Javadoc)
197     * @see com.bbn.swede.core.IOWLDocument#getGeneratedCodeLocation(java.lang.String)
198     */
199    public IPath getGeneratedCodeLocation(String sGeneratorID)
200    {
201       return (IPath)_mapGenLocs.get(sGeneratorID);
202    }
203    /* (non-Javadoc)
204     * @see com.bbn.swede.core.IOWLDocument#setGeneratedCodeLocation(java.lang.String, org.eclipse.core.runtime.IPath)
205     */
206    public void setGeneratedCodeLocation(String sGeneratorID, IPath path)
207    {
208       if (path != null)
209       {
210          _mapGenLocs.put(sGeneratorID, path);
211       }
212       else
213       {
214          _mapGenLocs.remove(sGeneratorID);
215       }
216    }
217    /* (non-Javadoc)
218     * @see com.bbn.swede.core.IOWLDocument#getGeneratedCodeMap()
219     */
220    public Map getGeneratedCodeMap()
221    {
222       return _mapGenLocs;
223    }
224 }