Changes:
[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 net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
15 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
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.IPluginRegistry;
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                 IPluginRegistry registry = Platform.getPluginRegistry();
98                 IExtensionPoint point = registry.getExtensionPoint(IExternalToolConstants.PLUGIN_ID, extensionPointId);
99                 if (point != null) {
100                         IExtension[] extensions = point.getExtensions();
101                         for (int i = 0; i < extensions.length; i++) {
102                                 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
103                                 for (int j = 0; j < elements.length; j++) {
104                                         IConfigurationElement element = elements[j];
105                                         if (element.getName().equals(TAG_VARIABLE)) {
106                                                 String tag = element.getAttribute(TAG_TAG);
107                                                 String description = element.getAttribute(TAG_DESCRIPTION);
108                                                 String className = element.getAttribute(TAG_EXPANDER_CLASS);
109                                                 
110                                                 boolean valid = true;
111                                                 if (tag == null || tag.length() == 0) {
112                                                         valid = false;
113                                                         ExternalToolsPlugin.getDefault().log("Missing tag attribute value for variable element.", null); //$NON-NLS-1$
114                                                 }
115                                                 if (description == null || description.length() == 0) {
116                                                         valid = false;
117                                                         ExternalToolsPlugin.getDefault().log("Missing description attribute value for variable element.", null); //$NON-NLS-1$
118                                                 }
119                                                 if (className == null || className.length() == 0) {
120                                                         valid = false;
121                                                         ExternalToolsPlugin.getDefault().log("Missing expander class attribute value for variable element.", null); //$NON-NLS-1$
122                                                 }
123
124                                                 if (valid)
125                                                         variables.put(tag, newVariable(tag, description, element));
126                                         }
127                                 }
128                         }
129                 }
130         }
131         
132         /**
133          * Creates a new variable from the specified information.
134          */
135         protected abstract ExternalToolVariable newVariable(String tag, String description, IConfigurationElement element);
136 }