replace deprecated org.eclipse.jface.text.Assert with org.eclipse.core.runtime.Assert
[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
43         /* package */static final String TAG_TAG = "tag"; //$NON-NLS-1$
44
45         /* package */static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$
46
47         /* package */static final String TAG_COMPONENT_CLASS = "componentClass"; //$NON-NLS-1$
48
49         /* package */static final String TAG_EXPANDER_CLASS = "expanderClass"; //$NON-NLS-1$
50
51         /**
52          * Table of variables where the key is the variable tag and the value is the
53          * corresponding variable.
54          */
55         private HashMap variables;
56
57         /**
58          * The extension point id to read the variables from
59          */
60         private String extensionPointId;
61
62         /**
63          * The /** Creates a new registry and loads the variables.
64          */
65         public ExternalToolVariableRegistry(String extensionPointId) {
66                 super();
67                 this.extensionPointId = extensionPointId;
68                 loadVariables();
69         }
70
71         /**
72          * Returns the variables in the registry
73          */
74         protected final void copyVariables(Object[] results) {
75                 variables.values().toArray(results);
76         }
77
78         /**
79          * Returns the variable for the specified tag, or <code>null</code> if
80          * none found.
81          */
82         protected final ExternalToolVariable findVariable(String tag) {
83                 return (ExternalToolVariable) variables.get(tag);
84         }
85
86         /**
87          * Returns the number of variables in the registry.
88          */
89         public final int getVariableCount() {
90                 return variables.size();
91         }
92
93         /**
94          * Load the available variables
95          */
96         private void loadVariables() {
97                 variables = new HashMap();
98                 IExtensionRegistry registry = Platform.getExtensionRegistry();
99                 // IPluginRegistry registry = Platform.getPluginRegistry();
100                 IExtensionPoint point = registry.getExtensionPoint(
101                                 IExternalToolConstants.PLUGIN_ID, extensionPointId);
102                 if (point != null) {
103                         IExtension[] extensions = point.getExtensions();
104                         for (int i = 0; i < extensions.length; i++) {
105                                 IConfigurationElement[] elements = extensions[i]
106                                                 .getConfigurationElements();
107                                 for (int j = 0; j < elements.length; j++) {
108                                         IConfigurationElement element = elements[j];
109                                         if (element.getName().equals(TAG_VARIABLE)) {
110                                                 String tag = element.getAttribute(TAG_TAG);
111                                                 String description = element
112                                                                 .getAttribute(TAG_DESCRIPTION);
113                                                 String className = element
114                                                                 .getAttribute(TAG_EXPANDER_CLASS);
115
116                                                 boolean valid = true;
117                                                 if (tag == null || tag.length() == 0) {
118                                                         valid = false;
119                                                         ExternalToolsPlugin
120                                                                         .getDefault()
121                                                                         .log(
122                                                                                         "Missing tag attribute value for variable element.", null); //$NON-NLS-1$
123                                                 }
124                                                 if (description == null || description.length() == 0) {
125                                                         valid = false;
126                                                         ExternalToolsPlugin
127                                                                         .getDefault()
128                                                                         .log(
129                                                                                         "Missing description attribute value for variable element.", null); //$NON-NLS-1$
130                                                 }
131                                                 if (className == null || className.length() == 0) {
132                                                         valid = false;
133                                                         ExternalToolsPlugin
134                                                                         .getDefault()
135                                                                         .log(
136                                                                                         "Missing expander class attribute value for variable element.", null); //$NON-NLS-1$
137                                                 }
138
139                                                 if (valid)
140                                                         variables.put(tag, newVariable(tag, description,
141                                                                         element));
142                                         }
143                                 }
144                         }
145                 }
146         }
147
148         /**
149          * Creates a new variable from the specified information.
150          */
151         protected abstract ExternalToolVariable newVariable(String tag,
152                         String description, IConfigurationElement element);
153 }