View Javadoc

1   /*
2    * $Id: TranslatorPlugin.java,v 1.2 2005/05/31 20:25:08 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.tools.translator;
10  
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.Collection;
14  import java.util.HashMap;
15  import java.util.HashSet;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import javax.swing.tree.DefaultMutableTreeNode;
22  import javax.swing.tree.MutableTreeNode;
23  import javax.swing.tree.TreeNode;
24  
25  import org.eclipse.core.runtime.IConfigurationElement;
26  import org.eclipse.core.runtime.IExtension;
27  import org.eclipse.core.runtime.IExtensionPoint;
28  import org.eclipse.core.runtime.IExtensionRegistry;
29  import org.eclipse.core.runtime.Platform;
30  import org.eclipse.core.runtime.Plugin;
31  
32  import com.bbn.swede.core.OWLCore;
33  
34  /***
35   * A singleton class representing information about the translators 
36   * extension point, and other translation-related information. 
37   * 
38   * @author aperezlo
39   */
40  public final class TranslatorPlugin extends Plugin
41  {
42     private static final String EXTENSION_POINT_ELEMENT_STR = "translator";
43     private static final String EXTENSION_POINT_ID_STR = "com.bbn.swede.tools.translator";
44     private static final String TRANSLATOR_PLUGIN_ID_STR = "com.bbn.swede.tools.translator";
45     
46     private static TranslatorPlugin _thisPlugin;
47     private List _translators;
48     
49     private TranslatorPlugin()
50     {
51        _translators = new ArrayList();
52        List extensions = null;
53        IExtensionRegistry registry = Platform.getExtensionRegistry();
54        IExtensionPoint point = registry.getExtensionPoint(EXTENSION_POINT_ID_STR);
55        IExtension tempExtension = null;
56        IConfigurationElement[] ices = null;
57        IConfigurationElement ice = null;
58        Iterator i = null;
59        ITranslator tempTranslator = null;
60        TranslatorDescriptor newDescriptor = null;
61        if (point != null)
62        {
63           extensions = Arrays.asList(point.getExtensions());
64        }
65        else
66        {
67           OWLCore.logWarning(getID(), "Extension point not found.", null);
68        }
69  
70        i = extensions.iterator();
71        while(i.hasNext())
72        {
73           tempExtension = (IExtension) i.next();
74           ices = tempExtension.getConfigurationElements();
75           for(int j = 0; j < ices.length; j++)
76           {
77              ice = ices[j];
78              if(ice.getName().equals(EXTENSION_POINT_ELEMENT_STR))
79              {
80                 try
81                 {
82                    newDescriptor = new TranslatorDescriptor(ice);
83                    _translators.add(newDescriptor);
84                 }
85                 catch(Exception e)
86                 {
87                    OWLCore.logError(getID(), "Could not create translator!", e);
88                 }
89              }
90           }
91        }
92  
93     }
94     
95     
96     /***
97      * Gets an unordered set of all of the extensions for which 
98      * a translator exists that has that extension as its input.
99      * 
100     * @return a set of all available translator input extensions
101     */
102    public Collection getInputExtensions()
103    {
104       Set toReturn = new HashSet();
105       Iterator i = _translators.iterator();
106       while(i.hasNext())
107       {
108          toReturn.add(((TranslatorDescriptor) i.next()).getInputExtension());
109       }
110       return toReturn;
111    }
112    
113    /***
114     * Gets an unordered set of all of the extensions for which 
115     * a translator exists that has that extension as its output.
116     * 
117     * @return a set of all available translator output extensions
118     */
119    public Collection getOutputExtensions()
120    {
121       Set toReturn = new HashSet();
122       Iterator i = _translators.iterator();
123       while(i.hasNext())
124       {
125          toReturn.add(((TranslatorDescriptor) i.next()).getOutputExtension());
126       }
127       return toReturn;
128       
129    }
130    
131    /***
132     * Retrieves the singleton instance of TranslatorPlugin.
133     * @return the singleton TranslatorPlugin
134     */
135    public static TranslatorPlugin getPlugin()
136    {
137       if(_thisPlugin == null)
138       {
139          _thisPlugin = new TranslatorPlugin();
140       }
141       
142       return _thisPlugin;
143    }
144    
145    /***
146     * This method returns a list of all of the translators available 
147     * to SWeDE.
148     * 
149     * @see TranslatorDescriptor
150     * @return a List of TranslatorDescriptor objects
151     */
152    public List getTranslators()
153    {
154       return _translators;
155    }
156    
157    /***
158     * A convenience method, which generates a representation of the 
159     * available translators in a tree form.  The leaf nodes of the 
160     * tree are individual translators, while branch nodes are 
161     * menu titles.  
162     * 
163     * @param translatorList The translators to structure as a tree
164     * @return A tree containing available translators, organized by translator
165     *         group
166     */
167    public static TreeNode getTranslatorsAsTree(List translatorList)
168    {
169       TreeNode toReturn = null;
170       MutableTreeNode root = new DefaultMutableTreeNode("Translators");
171       MutableTreeNode newNode = null;
172       MutableTreeNode tempNode = null;
173       TranslatorDescriptor tempTrans = null;
174       Map groupIDtoNode = new HashMap();
175       Iterator i = translatorList.iterator();
176       while(i.hasNext())
177       {
178          tempTrans = (TranslatorDescriptor) i.next();
179          if(tempTrans.getGroupID() != null)
180          {
181             if(groupIDtoNode.get(tempTrans.getGroupID()) == null)
182             {
183                newNode = new DefaultMutableTreeNode(tempTrans.getGroupName());
184                root.insert(newNode, root.getChildCount());
185                groupIDtoNode.put(tempTrans.getGroupID(), newNode);
186             }
187          }
188       }
189       i = translatorList.iterator();
190       while(i.hasNext())
191       {
192          tempTrans = (TranslatorDescriptor) i.next();
193          newNode = new DefaultMutableTreeNode(tempTrans);
194          if(tempTrans.getGroupID() != null)
195          {
196             tempNode = (MutableTreeNode) groupIDtoNode.get(tempTrans.getGroupID());
197          }
198          else
199          {
200             tempNode = root;
201          }
202          tempNode.insert(newNode, tempNode.getChildCount());
203       }
204       
205       toReturn = root;
206       return toReturn;
207    }
208    
209    /***
210     * Retrieves the unique identifier of the plug-in.
211     * @return the TranslatorPlugin 's plugin ID string
212     */
213    public String getID()
214    {
215       return TRANSLATOR_PLUGIN_ID_STR;
216    }
217 
218 }