View Javadoc

1   /*
2    * $Id: NamespaceManager.java,v 1.4 2005/02/28 22:39:40 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    
9   package com.bbn.swede.editor.contentassist;
10  
11  import java.util.ArrayList;
12  import java.util.Collection;
13  import java.util.HashMap;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Map;
17  
18  import com.bbn.swede.core.dom.Namespace;
19  
20  /***
21   * This class helps to manage the mappings from namespaces to URIs and back 
22   * for a particular OWL document.
23   * 
24   * @author aperezlo
25   */
26  class NamespaceManager
27  {
28     /***
29      * <code>DEFAULT_NAMESPACE</code> - used to request the default namespace.
30      * @see #getDefaultNamespaceURI() 
31      */
32     public static final int DEFAULT_NAMESPACE = 1;
33     /***
34      * <code>BASE_URI</code> used to request the base URI of the document.
35      * @see #getBaseURI()
36      */
37     public static final int BASE_URI = 2;
38     
39     private Map _map;
40     private String _baseURI;
41     private boolean _explicitBase;
42     private boolean _explicitDefault;
43     
44     /***
45      * Default constructor.
46      */
47     public NamespaceManager()
48     {
49        _map = new HashMap();
50        _baseURI = "";
51     }
52     
53     /***
54      * Lists all namespaces managed by this instance.
55      * @return A java.util.List of all managed namespace abbreviations
56      */
57     public List getAllNamespaces()
58     {
59         return new ArrayList(_map.keySet());
60     }
61     
62     /***
63      * Lists all namespace URIs managed by this instance.
64      * @return A java.util.List of all managed namespace URIs.
65      */
66     public List getAllURIs()
67     {
68        return new ArrayList(_map.values());
69     }
70  
71     /***
72      * Indicates whether the document has an explicit XML base.
73      * @return <code>true</code> if this document has an explicitly declared 
74      *         base URI, <code>false</code> otherwise
75      */
76     public boolean hasExplicitBase()
77     {
78        return _explicitBase;
79     }
80     
81     /***
82      * Indicates whether the document has an explicit default namespace.
83      * @return <code>true</code> if this document has an explicitly declared 
84      *         default namespace, <code>false</code> otherwise.
85      */
86     public boolean hasExplicitDefault()
87     {
88        return _explicitDefault;
89     }
90     
91     /***
92      * Sets the base URI of the document.  This will also cause 
93      * future calls to hasExplicitBase() to return <code>true</code>.
94      * @param newBase the new base URI 
95      */
96     public synchronized void setBaseURI(String newBase)
97     {
98        _baseURI = newBase;
99        _explicitBase = true;
100    }
101    
102    /***
103     * Retrieves the base URI of this document.
104     * @return the document's base URI 
105     */
106    public String getBaseURI()
107    {
108       return _baseURI;
109    }
110    
111    /***
112     * Adds a namespace to the manager.
113     * @param ns the namespace node for which to add a mapping
114     * @return true if the namespace was sucessfully added, false otherwise
115     */
116    public synchronized boolean addNamespace(Namespace ns)
117    {
118       boolean toReturn = false;
119       String abbreviation = null;
120       String sURI = null;
121       if(ns != null)
122       {
123          abbreviation = ns.getAbbreviation();
124          sURI = ns.getValue().replaceAll("#", "");
125          if(abbreviation.equals(""))
126          {
127             _explicitDefault = true;
128          }
129          
130          _map.put(abbreviation, sURI);
131          toReturn = true;
132       }
133 
134       
135       return toReturn;
136    }
137    
138    /***
139     * Retrieves the URI of the document's default namespace.
140     * @return the URI of the default namespace for this manager.  If no default 
141     * namespace has been explicitly identified, then the base URI is returned.
142     */
143    public String getDefaultNamespaceURI()
144    {
145       String toReturn = "";
146       if(containsNamespace(""))
147       {
148          toReturn = getURI("");
149       }
150       else
151       {
152          toReturn = getBaseURI();
153       }
154       return toReturn;
155    }
156    
157    /***
158     * Adds a namespace to the manager.
159     * @param namespace the abbreviated representation of this URI
160     * @param sURI the URI
161     * @return true if this is the first mapping to this URI, false if this URI has other aliases. 
162     */
163    public synchronized boolean addNamespace(String namespace, String sURI)
164    {
165       boolean toReturn = true;
166       if(_map.values().contains(sURI))
167       {
168          toReturn = false;
169       }
170       
171       _map.put(namespace, sURI);
172       if(namespace.equals(""))
173       {
174          _explicitDefault = true;
175       }
176       
177       return toReturn;
178    }
179 
180    
181    /***
182     * Removes a namespace from the manager.
183     * @param namespace the abbreviated form of the namespace mapping to remove
184     * @return true if the mapping was removed, false otherwise
185     */
186    public synchronized boolean removeNamespace(String namespace)
187    {
188       boolean toReturn = false;
189       if(_map.get(namespace) != null)
190       {
191          toReturn = true;
192          _map.remove(namespace);
193       }
194       return toReturn;
195    }
196    
197    /***
198     * Indicates whether the manager contains a given namespace abbreviation.
199     * @param ns the abbreviated form of a URI
200     * @return true if this manager contains a mapping to a URI from the specified abbreviation, false otherwise
201     */
202    public boolean containsNamespace(String ns)
203    {
204       synchronized (_map)
205       {
206          return _map.keySet().contains(ns);
207       }
208    }
209    /***
210     * Indicates whether the manager contains a given namespace URI.
211     * @param uri a URI
212     * @return true if this manager contains a mapping from a namespace abbreviation to the specified URI
213     */
214    public boolean containsURI(String uri)
215    {
216       synchronized (_map)
217       {
218          return _map.values().contains(uri);
219       }
220    }
221 
222    /***
223     * This method will remove all mappings of namespaces to a particular URI.
224     *  
225     * @param sURI the URI to remove
226     * @return true if at least one namespace was removed, false otherwise
227     */
228    public synchronized boolean removeURI(String sURI)
229    {
230       boolean toReturn = false;
231       Collection toRemove = new ArrayList();
232       Iterator i  = _map.keySet().iterator();
233       String temp = null;
234       while(i.hasNext())
235       {
236          temp = (String) i.next();
237          if(sURI.equals((String) _map.get(temp)))
238          {
239             toReturn = true;
240             toRemove.add(temp);
241          }
242       }
243       i = toRemove.iterator();
244       while(i.hasNext())
245       {
246          _map.remove(i.next());
247       }
248       
249       return toReturn;
250    }
251    
252    /***
253     * The namespace manager can support multiple aliases for a namespace abbreviation 
254     * to a URI.  This method returns all of the abbreviations for a particular URI.
255     * 
256     * @param sURI a URI
257     * @return a list of the namespaces which are abbreviations for the specified URI
258     */
259    public List getNamespacesForURI(String sURI)
260    {
261       List toReturn = new ArrayList();
262       Iterator i = _map.keySet().iterator();
263       String tempKey = null;
264       if(containsURI(sURI))
265       {
266          while(i.hasNext())
267          {
268             tempKey = (String) i.next();
269             if(sURI.equals(_map.get(tempKey)))
270             {
271                toReturn.add(tempKey);
272             }
273          }
274       }
275       return toReturn;
276    }
277    
278    /***
279     * Retrieves the full URI that corresponds to a namespace abbreviation in
280     * this document.
281     * @param namespace the namespace abbreviation for a paricular URI
282     * @return the String representation of the URI for that namespace, or null if no URI exists for that namespace
283     */
284    public String getURI(String namespace)
285    {
286       return (String) _map.get(namespace);
287    }
288    
289    /***
290     * This method retrieves one of two "special" URIs, the URI for the default 
291     * namespace specified by the document, and the base URI of the document.  
292     * The parameter should be one of the static constants defined in this class, 
293     * either BASE_URI or DEFAULT_NAMESPACE.
294     * 
295     * @param special which special URI to retrieve
296     * @return the String representation of the base URI or default namespace, depending   
297     */
298    public String getURI(int special)
299    {
300       String toReturn = "";
301       if(special == BASE_URI)
302       {
303          toReturn = getBaseURI();
304       }
305       if(special == DEFAULT_NAMESPACE)
306       {
307          toReturn = _explicitDefault ? getURI("") : getBaseURI();
308       }
309       return toReturn;
310    }
311    
312 
313 }