View Javadoc

1   /*
2    * $Id: OASTNameResolver.java,v 1.3 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    
9   package com.bbn.swede.editor.visualeditors.restriction;
10  
11  import java.util.HashMap;
12  
13  import com.bbn.swede.core.dom.IOASTChangeListener;
14  import com.bbn.swede.core.dom.IOASTDelta;
15  import com.bbn.swede.core.dom.IOASTDeltaVisitor;
16  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
17  import com.bbn.swede.core.dom.Namespace;
18  import com.bbn.swede.core.dom.OASTEvent;
19  import com.bbn.swede.core.dom.OASTNode;
20  
21  /***
22   * Resolves URIs against the document's OAST.
23   * @author aperezlo
24   */
25  public class OASTNameResolver implements IOASTChangeListener
26  {
27     /***
28      * The OAST to resolve against.
29      */
30     protected IOWLAbstractSyntaxTree _oast;
31     
32     /***
33      * Map of namespace abbreviations to URIs.
34      */
35     protected HashMap _namespaceToURI;
36     /***
37      * Map of URIs to namespace abbreviations.
38      */
39     protected HashMap _uriToNamespace;
40     /***
41      * The base URI of the document.
42      */
43     protected String _baseURI; 
44     
45     private boolean _hasBeenLoaded;
46     
47     /***
48      * Creates a resolver for the specified OAST.
49      * @param oast the OAST
50      */
51     public OASTNameResolver(IOWLAbstractSyntaxTree oast)
52     {
53        _namespaceToURI = new HashMap();
54        _uriToNamespace = new HashMap();
55        _oast = oast;
56        loadNamespaces();
57     }
58     
59     /***
60      * Intializes the resolver.
61      */
62     public void init()
63     {
64        loadNamespaces();
65        _oast.addOASTChangeListener(this);
66     }
67  
68     /***
69      * Searches the OAST for namespace nodes.
70      */
71     protected void loadNamespaces()
72     {
73        if(!_hasBeenLoaded)
74        {
75           _baseURI = _oast.getBaseURI();
76           OASTNode[] namespaceNodes = _oast.getRoot().getNodesOfType(OASTNode.NAMESPACE);
77           for (int i = 0; i < namespaceNodes.length; i++)
78           {
79              Namespace ns = (Namespace)namespaceNodes[i];
80              String abbrv = ns.getAbbreviation();
81              String sUri = ns.getValue();
82              
83              _namespaceToURI.put(abbrv, sUri);
84              _uriToNamespace.put(sUri, abbrv);
85           }
86              
87           _hasBeenLoaded = true;         
88        }
89     }
90     
91     /***
92      * Cleans up resources used by the resolver.
93      */
94     public void cleanUp()
95     {
96        _oast.removeOASTChangeListener(this);
97     }
98     
99     /***
100     * Translates a qualified name into a full URI.
101     * @param qName The qualified name
102     * @return The full URI of the qname, or <code>null</code> if it does not
103     *         match a namespace abbreviation in the document.
104     */
105    public String getURIForQName(String qName)
106    {
107       String toReturn = qName;
108       int colon = qName.lastIndexOf(":");
109       if(colon > -1)
110       {
111          String qualifier = qName.substring(0, colon);
112          String fragment = qName.substring(colon);
113          String baseURI = (String) _namespaceToURI.get(qualifier);
114          if(baseURI != null)
115          {
116             toReturn = baseURI + "#" + fragment;
117          }
118       }
119       return toReturn;
120    }
121    /***
122     * Translates a URI into a qualified name.
123     * @param sUri The URI
124     * @return A qualified name corresponding to the URI, or <code>null</code>
125     *         if there is no namespace abbreviation for the URI.
126     */
127    public String getQNameForURI(String sUri)
128    {
129       String toReturn = sUri;
130       int pound = sUri.lastIndexOf("#");
131       if(pound > -1)
132       {
133          String baseURI = sUri.substring(0, pound);
134          String fragment = sUri.substring(pound);
135          String qualifier = (String) _namespaceToURI.get(baseURI);
136          if(qualifier != null)
137          {
138             toReturn = qualifier + ":" + fragment;
139          }
140       }
141       
142       return toReturn;
143    }
144    
145    /*
146     *  (non-Javadoc)
147     * @see com.bbn.swede.core.dom.IOASTChangeListener#oastChanged(com.bbn.swede.core.dom.OASTEvent)
148     */
149    public void oastChanged(OASTEvent event)
150    {
151       IOASTDelta delta = event.getDelta();
152       delta.accept(new IOASTDeltaVisitor()
153       {
154          public boolean visit(IOASTDelta delta)
155          {
156             boolean toReturn = true;
157             if((delta.getType() & IOASTDelta.INSERTED) != 0)
158             {
159                OASTNode node = delta.getNodeAfter();
160                if(node instanceof Namespace)
161                {
162                   Namespace ns = (Namespace) node;
163                   String abbrv = ns.getAbbreviation();
164                   String sUri = ns.getValue();
165                   
166                   _namespaceToURI.put(abbrv, sUri);
167                   _uriToNamespace.put(sUri, abbrv);
168                }
169             }
170             if((delta.getType() & IOASTDelta.CHANGED) != 0)
171             {
172                OASTNode nodeAfter = delta.getNodeAfter();
173                OASTNode nodeBefore = delta.getNodeBefore();
174                if(nodeAfter instanceof Namespace)
175                {
176                   Namespace newNS = (Namespace) nodeAfter;
177                   Namespace oldNS = (Namespace) nodeBefore;
178                   String oldAbbrv = oldNS.getAbbreviation();
179                   String oldURI = oldNS.getValue();
180                   String abbrv = newNS.getAbbreviation();
181                   String sUri = newNS.getValue();
182                   
183                   _namespaceToURI.remove(oldAbbrv);
184                   _uriToNamespace.remove(oldURI);
185                   _namespaceToURI.put(abbrv, sUri);
186                   _uriToNamespace.put(sUri, abbrv);
187                }
188             }
189             return toReturn;
190          }
191       });
192    }
193 
194 }