Organized imports.
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / registry / ExternalToolVariableRegistry.java
1 package net.sourceforge.phpdt.externaltools.internal.registry;
2
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8
9 Contributors:
10 **********************************************************************/
11
12 import java.util.HashMap;
13
14 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
15 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.IExtensionPoint;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.Platform;
22
23 /**
24  * General registry reader for external tool variables.
25  */
26 public abstract class ExternalToolVariableRegistry {
27         // Format of the variable extension points
28         // <extension point="org.eclipse.ui.externalTools.***Variables>
29         //              <variable
30         //                      tag={string}
31         //                      description={string}
32         //                      componentClass={string:IVariableComponent}
33         //                      expanderClass={string:IVariable***Expander}>
34         //              </variable>
35         // </extension>
36         //
37         
38         /**
39          * Element and attribute tags of a variable extension.
40          */
41         /*package*/ static final String TAG_VARIABLE = "variable"; //$NON-NLS-1$
42         /*package*/ static final String TAG_TAG = "tag"; //$NON-NLS-1$
43         /*package*/ static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$
44         /*package*/ static final String TAG_COMPONENT_CLASS = "componentClass"; //$NON-NLS-1$
45         /*package*/ static final String TAG_EXPANDER_CLASS = "expanderClass"; //$NON-NLS-1$
46
47
48         /**
49          * Table of variables where the key is the variable tag
50          * and the value is the corresponding variable.
51          */
52         private HashMap variables;
53         
54         /**
55          * The extension point id to read the variables from
56          */
57         private String extensionPointId;
58         
59         /**
60          * The 
61         /**
62          * Creates a new registry and loads the variables.
63          */
64         public ExternalToolVariableRegistry(String extensionPointId) {
65                 super();
66                 this.extensionPointId = extensionPointId;
67                 loadVariables();
68         }
69         
70         /**
71          * Returns the variables in the registry
72          */
73         protected final void copyVariables(Object[] results) {
74                 variables.values().toArray(results);
75         }
76
77         /**
78          * Returns the variable for the specified tag, or
79          * <code>null</code> if none found.
80          */
81         protected final ExternalToolVariable findVariable(String tag) {
82                 return (ExternalToolVariable) variables.get(tag);
83         }
84
85         /**
86          * Returns the number of variables in the registry.
87          */
88         public final int getVariableCount() {
89                 return variables.size();
90         }
91         
92         /**
93          * Load the available variables
94          */
95         private void loadVariables() {
96                 variables = new HashMap();
97                 IExtensionRegistry registry= Platform.getExtensionRegistry();
98 //              IPluginRegistry registry = Platform.getPluginRegistry();
99                 IExtensionPoint point = registry.getExtensionPoint(IExternalToolConstants.PLUGIN_ID, extensionPointId);
100                 if (point != null) {
101                         IExtension[] extensions = point.getExtensions();
102                         for (int i = 0; i < extensions.length; i++) {
103                                 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
104                                 for (int j = 0; j < elements.length; j++) {
105                                         IConfigurationElement element = elements[j];
106                                         if (element.getName().equals(TAG_VARIABLE)) {
107                                                 String tag = element.getAttribute(TAG_TAG);
108                                                 String description = element.getAttribute(TAG_DESCRIPTION);
109                                                 String className = element.getAttribute(TAG_EXPANDER_CLASS);
110                                                 
111                                                 boolean valid = true;
112                                                 if (tag == null || tag.length() == 0) {
113                                                         valid = false;
114                                                         ExternalToolsPlugin.getDefault().log("Missing tag attribute value for variable element.", null); //$NON-NLS-1$
115                                                 }
116                                                 if (description == null || description.length() == 0) {
117                                                         valid = false;
118                                                         ExternalToolsPlugin.getDefault().log("Missing description attribute value for variable element.", null); //$NON-NLS-1$
119                                                 }
120                                                 if (className == null || className.length() == 0) {
121                                                         valid = false;
122                                                         ExternalToolsPlugin.getDefault().log("Missing expander class attribute value for variable element.", null); //$NON-NLS-1$
123                                                 }
124
125                                                 if (valid)
126                                                         variables.put(tag, newVariable(tag, description, element));
127                                         }
128                                 }
129                         }
130                 }
131         }
132         
133         /**
134          * Creates a new variable from the specified information.
135          */
136         protected abstract ExternalToolVariable newVariable(String tag, String description, IConfigurationElement element);
137 }