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