View Javadoc

1   /*
2    * $Id: OWLElement.java,v 1.4 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 java.net.MalformedURLException;
11  
12  import org.eclipse.core.resources.IResource;
13  import org.eclipse.core.runtime.IPath;
14  
15  import com.bbn.swede.core.IOWLElement;
16  import com.bbn.swede.core.IOWLElementVisitor;
17  import com.bbn.swede.core.OWLCore;
18  import com.bbn.swede.core.OWLElementInfo;
19  
20  /***
21   * Common implementation for OWL elements.
22   * @author jlerner
23   */
24  public abstract class OWLElement implements IOWLElement
25  {
26     /***
27      * The parent of the element within the OWL model.  All elements besides
28      * the singleton instance of IOWLModel have a parent.
29      */
30     protected IOWLElement _elemParent;
31     /*
32      *  (non-Javadoc)
33      * @see com.bbn.swede.core.IOWLElement#getParent()
34      */
35     public IOWLElement getParent()
36     {
37        return _elemParent;
38     }
39     /*
40      *  (non-Javadoc)
41      * @see com.bbn.swede.core.IOWLElement#setParent(com.bbn.swede.core.IOWLElement)
42      */
43     public void setParent(IOWLElement elem)
44     {
45        _elemParent = elem;
46     }
47  
48     /*
49      *  (non-Javadoc)
50      * @see com.bbn.swede.core.IOWLElement#getPath()
51      */
52     public IPath getPath()
53     {
54        IResource resEnclosing = getResource();
55        return resEnclosing.getFullPath();
56     }
57  
58     /***
59      * The Eclipse resource that most closely corresponds to this element.
60      */
61     protected IResource _resource;
62     /*
63      *  (non-Javadoc)
64      * @see com.bbn.swede.core.IOWLElement#getResource()
65      */
66     public IResource getResource()
67     {
68        if (_resource == null)
69        {
70           if (_elemParent == null)
71           {
72              return null;
73           }
74           else
75           {
76              return _elemParent.getResource();
77           }
78        }
79        return _resource;
80     }
81  
82     /*
83      *  (non-Javadoc)
84      * @see com.bbn.swede.core.IOWLElement#getCorrespondingResource()
85      */
86     public IResource getCorrespondingResource()
87     {
88        return _resource;
89     }
90  
91     /***
92      * The name of the element.
93      */
94     protected String _sElementName;
95     /*
96      *  (non-Javadoc)
97      * @see com.bbn.swede.core.IOWLElement#getElementName()
98      */
99     public String getElementName()
100    {
101       return _sElementName;
102    }
103 
104    /***
105     * Sets the name of the element.
106     * @param sNewName The new element name.
107     */
108    public void setElementName(String sNewName)
109    {
110       _sElementName = sNewName;
111    }
112 
113    /*
114     *  (non-Javadoc)
115     * @see com.bbn.swede.core.IOWLElement#isReadOnly()
116     */
117    public boolean isReadOnly()
118    {
119       return false;
120    }
121 
122    /*
123     *  (non-Javadoc)
124     * @see com.bbn.swede.core.IOWLElement#getURI()
125     */
126    public String getURI()
127    {
128       return null;
129    }
130 
131    /*
132     *  (non-Javadoc)
133     * @see com.bbn.swede.core.IOWLElement#getLocalURI()
134     */
135    public String getLocalURI()
136    {
137       String url = null;
138       try
139       {
140          url = getPath().toFile().toURL().toString();
141       }
142       catch (MalformedURLException mfe)
143       {
144          OWLCore.logWarning(OWLCore.getID(),
145             "Cannot set URL - malformed: " + url, mfe);
146       }
147       return url;
148    }
149 
150    /*
151     *  (non-Javadoc)
152     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
153     */
154    public Object getAdapter(Class adapter)
155    {
156       if (adapter.equals(IResource.class))
157       {
158          return this.getResource();
159       }
160       return null;
161    }
162 
163    /***
164     * The element info for the element.  This is created on demand by
165     * <code>getElementInfo()</code>.
166     */
167    protected OWLElementInfo _info;
168    /*
169     *  (non-Javadoc)
170     * @see com.bbn.swede.core.IOWLElement#getElementInfo()
171     */
172    public OWLElementInfo getElementInfo()
173    {
174       if (_info == null)
175       {
176          _info = SWResourceManager.createElementInfo(this);
177       }
178       return _info;
179    }
180 
181    /*
182     *  (non-Javadoc)
183     * @see com.bbn.swede.core.IOWLElement#contains(org.eclipse.core.resources.IResource)
184     */
185    public boolean contains(IResource resource)
186    {
187       return false;
188    }
189 
190    /*
191     *  (non-Javadoc)
192     * @see com.bbn.swede.core.IOWLElement#getCorrespondingElement(org.eclipse.core.resources.IResource)
193     */
194    public IOWLElement getCorrespondingElement(IResource resource)
195    {
196       return null;
197    }
198    
199    /*
200     *  (non-Javadoc)
201     * @see com.bbn.swede.core.IOWLElement#accept(com.bbn.swede.core.IOWLElementVisitor)
202     */
203    public final void accept(IOWLElementVisitor visitor)
204    {
205       if (!visitor.visit(this))
206       {
207          return;
208       }
209       if (getElementInfo() == null)
210       {
211          return;
212       }
213       IOWLElement[] children = _info.getChildren();
214       for (int i = 0; i < children.length; i++)
215       {
216          children[i].accept(visitor);
217       }
218    }
219 }