~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/EnvironmentVariableSupplier.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 * Intel Corporation - Initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.cdt.internal.core.cdtvariables;
 
12
 
 
13
import java.util.List;
 
14
import java.util.regex.Pattern;
 
15
 
 
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;
 
24
 
 
25
/**
 
26
 * This supplier suplies the macros that represent the Managed Build environment variables
 
27
 * 
 
28
 * @since 3.0
 
29
 */
 
30
public class EnvironmentVariableSupplier extends CoreMacroSupplierBase {
 
31
        private static EnvironmentVariableSupplier fInstance;
 
32
        private EnvironmentVariableManager fEnvironmentProvider;
 
33
        
 
34
        public class EnvVarMacro extends CdtVariable{
 
35
                private IEnvironmentVariable fVariable;
 
36
                private EnvVarMacro(IEnvironmentVariable var){
 
37
                        fName = var.getName();
 
38
                        fVariable = var;
 
39
                }
 
40
                
 
41
                private void loadValue(IEnvironmentVariable var){
 
42
                        String delimiter = var.getDelimiter();
 
43
                        String value = var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE ?
 
44
                                        var.getValue() : null;
 
45
                        
 
46
                        if(isTextList(value,delimiter)){
 
47
                                fType = VALUE_TEXT_LIST;
 
48
                                if(value != null){
 
49
                                        List<String> list = EnvVarOperationProcessor.convertToList(value,delimiter);
 
50
                                        fStringListValue = list.toArray(new String[list.size()]);
 
51
                                } else {
 
52
                                        fStringListValue = null;
 
53
                                }
 
54
                        } else {
 
55
                                fType = VALUE_TEXT;
 
56
                                fStringValue = value;
 
57
                        }
 
58
                }
 
59
                
 
60
                
 
61
                @Override
 
62
                public int getValueType() {
 
63
                        if(fVariable != null){
 
64
                                loadValue(fVariable);
 
65
 
 
66
                                //we do not need it any more, release clean the reference
 
67
                                fVariable = null;
 
68
                        }
 
69
                        return super.getValueType();
 
70
                }
 
71
 
 
72
                @Override
 
73
                public String getStringValue() throws CdtVariableException {
 
74
                        if(fVariable != null){
 
75
                                loadValue(fVariable);
 
76
 
 
77
                                //we do not need it any more, release clean the reference
 
78
                                fVariable = null;
 
79
                        }
 
80
                        return super.getStringValue();
 
81
                }
 
82
 
 
83
                @Override
 
84
                public String[] getStringListValue() throws CdtVariableException {
 
85
                        if(fVariable != null){
 
86
                                loadValue(fVariable);
 
87
 
 
88
                                //we do not need it any more, release clean the reference
 
89
                                fVariable = null;
 
90
                        }
 
91
                        return super.getStringListValue();
 
92
                }
 
93
        }
 
94
 
 
95
        protected EnvironmentVariableSupplier(){
 
96
                this(EnvironmentVariableManager.getDefault());
 
97
        }
 
98
        
 
99
        public EnvironmentVariableSupplier(EnvironmentVariableManager varProvider){
 
100
                fEnvironmentProvider = varProvider;
 
101
        }
 
102
        
 
103
        private static boolean isTextList(String str, String delimiter) {
 
104
                if (delimiter == null || "".equals(delimiter)) //$NON-NLS-1$
 
105
                        return false;
 
106
                
 
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);
 
110
        }
 
111
        
 
112
        public ICdtVariable createBuildMacro(IEnvironmentVariable var){
 
113
                if(var != null)
 
114
                        return new EnvVarMacro(var);
 
115
                return null; 
 
116
        }
 
117
 
 
118
        public static EnvironmentVariableSupplier getInstance(){
 
119
                if(fInstance == null)
 
120
                        fInstance = new EnvironmentVariableSupplier();
 
121
                return fInstance;
 
122
        }
 
123
 
 
124
        /* (non-Javadoc)
 
125
         * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacro(java.lang.String, int, java.lang.Object)
 
126
         */
 
127
        @Override
 
128
        public ICdtVariable getMacro(String macroName, int contextType,
 
129
                        Object contextData) {
 
130
                if(macroName == null || "".equals(macroName))   //$NON-NLS-1$
 
131
                return null;
 
132
 
 
133
                IEnvironmentVariable var = null;
 
134
                switch(contextType){
 
135
                case ICoreVariableContextInfo.CONTEXT_CONFIGURATION:
 
136
                        if(contextData instanceof ICConfigurationDescription){
 
137
                                var = fEnvironmentProvider.getVariable(macroName, (ICConfigurationDescription)contextData, false);
 
138
                        }
 
139
                        break;
 
140
                case ICoreVariableContextInfo.CONTEXT_WORKSPACE:
 
141
                        if(contextData == null || contextData instanceof IWorkspace){
 
142
                                var = fEnvironmentProvider.getVariable(macroName, (ICConfigurationDescription)null, false);
 
143
                        }
 
144
                        break;
 
145
//              case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
 
146
//                      if(contextData == null){
 
147
//                              var = fEnvironmentProvider.getVariable(macroName,fEnvironmentProvider.getContextInfo(contextData),false);
 
148
//                      }
 
149
//                      break;
 
150
                }
 
151
                if(var != null && var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE)
 
152
                        return new EnvVarMacro(var);
 
153
 
 
154
                return null;
 
155
        }
 
156
 
 
157
        /* (non-Javadoc)
 
158
         * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacros(int, java.lang.Object)
 
159
         */
 
160
        @Override
 
161
        public ICdtVariable[] getMacros(int contextType, Object contextData) {
 
162
                IEnvironmentVariable vars[] = null;
 
163
 
 
164
                switch(contextType){
 
165
                case ICoreVariableContextInfo.CONTEXT_CONFIGURATION:
 
166
                        if(contextData instanceof ICConfigurationDescription){
 
167
                                vars = fEnvironmentProvider.getVariables((ICConfigurationDescription)contextData, false);
 
168
                        }
 
169
                        break;
 
170
                case ICoreVariableContextInfo.CONTEXT_WORKSPACE:
 
171
                        if(contextData == null || contextData instanceof IWorkspace){
 
172
                                vars = fEnvironmentProvider.getVariables((ICConfigurationDescription)null, false);
 
173
                        }
 
174
                        break;
 
175
//              case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
 
176
//                      if(contextData == null){
 
177
//                              vars = fEnvironmentProvider.getVariables(fEnvironmentProvider.getContextInfo(contextData),false).toArray(false);
 
178
//                      }
 
179
//                      break;
 
180
                }
 
181
                
 
182
                if(vars != null){
 
183
                        EnvVarMacro macros[] = new EnvVarMacro[vars.length];
 
184
                        for(int i = 0; i < macros.length; i++)
 
185
                                macros[i] = new EnvVarMacro(vars[i]);
 
186
                        
 
187
                        return macros;
 
188
                }
 
189
 
 
190
                return null;
 
191
        }
 
192
/*
 
193
        public EnvironmentVariableProvider getEnvironmentVariableProvider(){
 
194
                return fEnvironmentProvider;
 
195
        }
 
196
*/
 
197
}