1
/*******************************************************************************
2
* Copyright (c) 2005, 2009 Intel Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Intel Corporation - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.cdtvariables;
13
import java.util.List;
14
import java.util.regex.Pattern;
16
import org.eclipse.cdt.core.cdtvariables.CdtVariable;
17
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
18
import org.eclipse.cdt.core.cdtvariables.ICdtVariable;
19
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
20
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
21
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
22
import org.eclipse.cdt.utils.envvar.EnvVarOperationProcessor;
23
import org.eclipse.core.resources.IWorkspace;
26
* This supplier suplies the macros that represent the Managed Build environment variables
30
public class EnvironmentVariableSupplier extends CoreMacroSupplierBase {
31
private static EnvironmentVariableSupplier fInstance;
32
private EnvironmentVariableManager fEnvironmentProvider;
34
public class EnvVarMacro extends CdtVariable{
35
private IEnvironmentVariable fVariable;
36
private EnvVarMacro(IEnvironmentVariable var){
37
fName = var.getName();
41
private void loadValue(IEnvironmentVariable var){
42
String delimiter = var.getDelimiter();
43
String value = var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE ?
44
var.getValue() : null;
46
if(isTextList(value,delimiter)){
47
fType = VALUE_TEXT_LIST;
49
List<String> list = EnvVarOperationProcessor.convertToList(value,delimiter);
50
fStringListValue = list.toArray(new String[list.size()]);
52
fStringListValue = null;
62
public int getValueType() {
63
if(fVariable != null){
66
//we do not need it any more, release clean the reference
69
return super.getValueType();
73
public String getStringValue() throws CdtVariableException {
74
if(fVariable != null){
77
//we do not need it any more, release clean the reference
80
return super.getStringValue();
84
public String[] getStringListValue() throws CdtVariableException {
85
if(fVariable != null){
88
//we do not need it any more, release clean the reference
91
return super.getStringListValue();
95
protected EnvironmentVariableSupplier(){
96
this(EnvironmentVariableManager.getDefault());
99
public EnvironmentVariableSupplier(EnvironmentVariableManager varProvider){
100
fEnvironmentProvider = varProvider;
103
private static boolean isTextList(String str, String delimiter) {
104
if (delimiter == null || "".equals(delimiter)) //$NON-NLS-1$
107
// Regex: ([^:]+:)+[^:]*
108
String patternStr = "([^" + delimiter + "]+" + delimiter + ")+[^" + delimiter + "]*"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
109
return Pattern.matches(patternStr, str);
112
public ICdtVariable createBuildMacro(IEnvironmentVariable var){
114
return new EnvVarMacro(var);
118
public static EnvironmentVariableSupplier getInstance(){
119
if(fInstance == null)
120
fInstance = new EnvironmentVariableSupplier();
125
* @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacro(java.lang.String, int, java.lang.Object)
128
public ICdtVariable getMacro(String macroName, int contextType,
129
Object contextData) {
130
if(macroName == null || "".equals(macroName)) //$NON-NLS-1$
133
IEnvironmentVariable var = null;
135
case ICoreVariableContextInfo.CONTEXT_CONFIGURATION:
136
if(contextData instanceof ICConfigurationDescription){
137
var = fEnvironmentProvider.getVariable(macroName, (ICConfigurationDescription)contextData, false);
140
case ICoreVariableContextInfo.CONTEXT_WORKSPACE:
141
if(contextData == null || contextData instanceof IWorkspace){
142
var = fEnvironmentProvider.getVariable(macroName, (ICConfigurationDescription)null, false);
145
// case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
146
// if(contextData == null){
147
// var = fEnvironmentProvider.getVariable(macroName,fEnvironmentProvider.getContextInfo(contextData),false);
151
if(var != null && var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE)
152
return new EnvVarMacro(var);
158
* @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacros(int, java.lang.Object)
161
public ICdtVariable[] getMacros(int contextType, Object contextData) {
162
IEnvironmentVariable vars[] = null;
165
case ICoreVariableContextInfo.CONTEXT_CONFIGURATION:
166
if(contextData instanceof ICConfigurationDescription){
167
vars = fEnvironmentProvider.getVariables((ICConfigurationDescription)contextData, false);
170
case ICoreVariableContextInfo.CONTEXT_WORKSPACE:
171
if(contextData == null || contextData instanceof IWorkspace){
172
vars = fEnvironmentProvider.getVariables((ICConfigurationDescription)null, false);
175
// case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
176
// if(contextData == null){
177
// vars = fEnvironmentProvider.getVariables(fEnvironmentProvider.getContextInfo(contextData),false).toArray(false);
183
EnvVarMacro macros[] = new EnvVarMacro[vars.length];
184
for(int i = 0; i < macros.length; i++)
185
macros[i] = new EnvVarMacro(vars[i]);
193
public EnvironmentVariableProvider getEnvironmentVariableProvider(){
194
return fEnvironmentProvider;