View Javadoc

1   /*
2    * $Id: OWLExternalDocument.java,v 1.3 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  import org.eclipse.core.resources.IFile;
11  import org.eclipse.core.runtime.CoreException;
12  import java.net.URL;
13  import java.net.MalformedURLException;
14  
15  import com.bbn.swede.core.IOWLExternalDocument;
16  import com.bbn.swede.core.OWLCore;
17  import com.hp.hpl.jena.rdf.arp.MalformedURIException;
18  
19  /***
20   * Implementation for external OWL documents.  An external document is a local
21   * copy of an OWL file downloaded from the web.
22   * @author tself
23   */
24  public class OWLExternalDocument
25     extends OWLDocument
26     implements IOWLExternalDocument
27  {
28     /***
29      * The URL from which the document was originally downloaded.
30      */
31     protected String _sourceURL;
32     /***
33      * Creates an OWL external document based on an Eclipse file resource and
34      * source URL.  The source URL will also be used as the document's XML base.
35      * @param file The Eclipse file
36      * @param sourceURL The URL the document was imported from
37      */
38     OWLExternalDocument(IFile file, String sourceURL)
39     {
40        this(file, sourceURL, sourceURL);
41     }
42     /***
43      * Creates an OWL external document based on an Eclipse file resource and
44      * source URL.  The XML base is set according to the <code>uri</code>
45      * parameter.
46      * @param file The Eclipse file
47      * @param sourceURL The URL the document was imported from
48      * @param uri The document's XML base
49      */
50     OWLExternalDocument(IFile file, String sourceURL, String uri)
51     {
52        super(file);
53        if (_uri == null) // if file already had a URI, don't overwrite
54        {
55           try
56           {
57              setURI(uri);
58           }
59           catch (MalformedURIException me)
60           {
61              OWLCore.logWarning(OWLCore.getID(),
62                 "Cannot set URI - malformed: " + uri, me);
63           }
64        }
65        try
66        {
67           setSourceURL(sourceURL);
68        }
69        catch (MalformedURLException mue)
70        {
71           OWLCore.logWarning(OWLCore.getID(),
72              "Cannot set source URL - malformed: " + sourceURL, mue);
73        }
74        file.setReadOnly(true);
75     }
76     
77     /***
78      * Retrieves the URL the document was imported from.  This may be different
79      * from its XML base.
80      * @return The source URL
81      */
82     public String getSourceURL()
83     {
84        return _sourceURL;
85     }
86     
87     /***
88      * Sets the document's external location.  This may be different from the
89      * XML base, but should be resolvable so the document can be refreshed from
90      * the "official" copy online.
91      * @param url The new source URL
92      * @throws MalformedURLException if <code>url</code> is malformed.
93      */
94     public void setSourceURL(String url) throws MalformedURLException
95     {
96        URL u = null;
97        try
98        {
99           u = new URL(url);
100       }
101       catch (MalformedURLException me)
102       {
103          OWLCore.logWarning(OWLCore.getID(),
104             "Cannot set source URL - malformed: " + url, me);
105          throw me;
106       }
107       try
108       {
109          getResource().setPersistentProperty(
110             OWLCore.PERSISTENT_SOURCE_URL,
111             url);
112       }
113       catch (CoreException ce)
114       {
115          OWLCore.logWarning(
116             OWLCore.getID(),
117             "Unable to set persistent property",
118             ce);
119       }
120       _sourceURL = url;
121    }
122 }