Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9369736a4c9039cafbba470bb19ce3d3255517b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.externaltools.internal.registry;


import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPluginRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin;
import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;

/**
 * General registry reader for external tool variables.
 */
public class ExternalToolVariableRegistry {
	// Format of the variable extension points
	// <extension point="org.eclipse.ui.externalTools.***Variables>
	//		<variable
	//			tag={string}
	//			description={string}
	//			componentClass={string:IVariableComponent}
	//			expanderClass={string:IVariable***Expander}>
	//		</variable>
	// </extension>
	//
	
	/**
	 * Element and attribute tags of a variable extension.
	 */
	/*package*/ static final String TAG_VARIABLE = "variable"; //$NON-NLS-1$
	/*package*/ static final String TAG_TAG = "tag"; //$NON-NLS-1$
	/*package*/ static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$
	/*package*/ static final String TAG_COMPONENT_CLASS = "componentClass"; //$NON-NLS-1$
	/*package*/ static final String TAG_EXPANDER_CLASS = "expanderClass"; //$NON-NLS-1$


	/**
	 * Sorted map of variables where the key is the variable tag
	 * and the value is the corresponding variable.
	 */
	private SortedMap variables;
	
	/**
	 * The extension point id to read the variables from
	 */
	protected String extensionPointId;
	
	public ExternalToolVariableRegistry() {
		this(IExternalToolConstants.EXTENSION_POINT_TOOL_VARIABLES);
	}
	
	/**
	 * Creates a new registry and loads the variables.
	 */
	protected ExternalToolVariableRegistry(String extensionPointId) {
		this.extensionPointId = extensionPointId;
		loadVariables();
	}

	/**
	 * Returns the variable for the specified tag, or
	 * <code>null</code> if none found.
	 */
	protected final ExternalToolVariable findVariable(String tag) {
		return (ExternalToolVariable) variables.get(tag);
	}

	/**
	 * Returns the number of variables in the registry.
	 */
	public final int getVariableCount() {
		return variables.size();
	}
	
	
	/**
	 * Returns the variable for the given tag or <code>null</code> if none.
	 */
	public ExternalToolVariable getVariable(String tag) {
		return findVariable(tag);
	}
	
	/**
	 * Returns the list of argument variables in the registry.
	 */
	public ExternalToolVariable[] getVariables() {
		ExternalToolVariable[] results = new ExternalToolVariable[getVariableCount()];
		variables.values().toArray(results);
		return results;
	}
	
	/**
	 * Load the available variables
	 */
	private void loadVariables() {
		variables = new TreeMap();
		IPluginRegistry registry = Platform.getPluginRegistry();
		IExtensionPoint point = registry.getExtensionPoint(IExternalToolConstants.PLUGIN_ID, extensionPointId);
		if (point != null) {
			IExtension[] extensions = point.getExtensions();
			for (int i = 0; i < extensions.length; i++) {
				IConfigurationElement[] elements = extensions[i].getConfigurationElements();
				for (int j = 0; j < elements.length; j++) {
					IConfigurationElement element = elements[j];
					if (element.getName().equals(TAG_VARIABLE)) {
						String tag = element.getAttribute(TAG_TAG);
						String description = element.getAttribute(TAG_DESCRIPTION);
						String className = element.getAttribute(TAG_EXPANDER_CLASS);
						
						boolean valid = true;
						if (tag == null || tag.length() == 0) {
							valid = false;
							ExternalToolsPlugin.getDefault().log("Missing tag attribute value for variable element.", null); //$NON-NLS-1$
						}
						if (description == null || description.length() == 0) {
							valid = false;
							ExternalToolsPlugin.getDefault().log("Missing description attribute value for variable element.", null); //$NON-NLS-1$
						}
						if (className == null || className.length() == 0) {
							valid = false;
							ExternalToolsPlugin.getDefault().log("Missing expander class attribute value for variable element.", null); //$NON-NLS-1$
						}

						if (valid)
							variables.put(tag, newVariable(tag, description, element));
					}
				}
			}
		}
	}
	
	/**
	 * Creates a new variable from the specified information.
	 */
	protected ExternalToolVariable newVariable(String tag, String description, IConfigurationElement element) {
		return new ExternalToolVariable(tag, description, element);
	}
	
}

Back to the top