52fa78a3652f611f633f5fb2cb033e16e547ac58
[phpeclipse.git] / net.sourceforge.phpeclipse.32.compatibility / src / net / sourceforge / phpdt / internal / launching / InterpreterRunnerConfiguration.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import net.sourceforge.phpdt.internal.core.JavaProject;
11
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.internal.ui.launchConfigurations.EnvironmentVariable;
18
19 public class InterpreterRunnerConfiguration {
20         protected ILaunchConfiguration configuration;
21
22         private HashMap fEnvironment;
23
24         public InterpreterRunnerConfiguration(ILaunchConfiguration aConfiguration) {
25                 configuration = aConfiguration;
26                 fEnvironment = new HashMap();
27         }
28
29         public String getAbsoluteFileName() {
30                 IPath path = new Path(getFileName());
31                 IProject project = getProject().getProject();
32
33                 return project.getLocation().toOSString() + "/" + getFileName();
34         }
35
36         public String getFileName() {
37                 String fileName = "";
38
39                 try {
40                         fileName = configuration.getAttribute(
41                                         PHPLaunchConfigurationAttribute.FILE_NAME,
42                                         "No file specified in configuration");
43                 } catch (CoreException e) {
44                 }
45
46                 return fileName.replace('\\', '/');
47         }
48
49         public JavaProject getProject() {
50                 String projectName = "";
51
52                 try {
53                         projectName = configuration.getAttribute(
54                                         PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
55                 } catch (CoreException e) {
56                         PHPLaunchingPlugin.log(e);
57                 }
58
59                 IProject project = PHPLaunchingPlugin.getWorkspace().getRoot()
60                                 .getProject(projectName);
61
62                 JavaProject phpProject = new JavaProject();
63                 phpProject.setProject(project);
64                 return phpProject;
65         }
66
67         public File getAbsoluteWorkingDirectory() {
68                 String file = null;
69                 try {
70                         file = configuration.getAttribute(
71                                         PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
72                 } catch (CoreException e) {
73                         PHPLaunchingPlugin.log(e);
74                 }
75                 return new File(file);
76         }
77
78         public String getInterpreterArguments() {
79                 try {
80                         return configuration.getAttribute(
81                                         PHPLaunchConfigurationAttribute.INTERPRETER_ARGUMENTS, "");
82                 } catch (CoreException e) {
83                 }
84
85                 return "";
86         }
87
88         public String getProgramArguments() {
89                 try {
90                         return configuration.getAttribute(
91                                         PHPLaunchConfigurationAttribute.PROGRAM_ARGUMENTS, "");
92                 } catch (CoreException e) {
93                 }
94
95                 return "";
96         }
97
98         public PHPInterpreter getInterpreter() {
99                 String selectedInterpreter = null;
100                 try {
101                         selectedInterpreter = configuration.getAttribute(
102                                         PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
103                 } catch (CoreException e) {
104                 }
105
106                 return PHPRuntime.getDefault().getInterpreter(selectedInterpreter);
107         }
108
109         public boolean useRemoteDebugger() {
110                 try {
111                         return configuration.getAttribute(
112                                         PHPLaunchConfigurationAttribute.REMOTE_DEBUG, false);
113                 } catch (CoreException e) {
114                         PHPLaunchingPlugin.log(e);
115                 }
116                 return false;
117         }
118
119         public boolean usePathTranslation() {
120                 try {
121                         return configuration.getAttribute(
122                                         PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE,
123                                         false);
124                 } catch (CoreException e) {
125                         PHPLaunchingPlugin.log(e);
126                 }
127                 return false;
128         }
129
130         public Map getPathMap() {
131                 try {
132                         return configuration.getAttribute(
133                                         PHPLaunchConfigurationAttribute.FILE_MAP, (Map) null);
134                 } catch (CoreException e) {
135                         PHPLaunchingPlugin.log(e);
136                 }
137                 return (Map) null;
138         }
139
140         public boolean useDBGSessionInBrowser() {
141                 try {
142                         return configuration.getAttribute(
143                                         PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER,
144                                         true);
145                 } catch (CoreException e) {
146                         PHPLaunchingPlugin.log(e);
147                 }
148                 return false;
149         }
150
151         public void setEnvironment(String[] envp) {
152                 if (envp == null)
153                         return;
154                 for (int i = 0; i < envp.length; i++) {
155                         addEnvironmentValue(envp[i], true);
156                 }
157         }
158
159         public void addEnvironmentValue(String env, boolean replace) {
160                 String value = env.substring(env.indexOf('=') + 1);
161                 String key = env.substring(0, env.indexOf('='));
162                 addEnvironmentValue(key, value, replace);
163         }
164
165         public void addEnvironmentValue(String key, String value, boolean replace) {
166                 if (!replace && fEnvironment.containsKey(key)) {
167                         EnvironmentVariable ev = (EnvironmentVariable) fEnvironment
168                                         .get(key);
169                         ev.setValue(ev.getValue() + ";" + value);
170                         fEnvironment.put(key, ev);
171                 } else
172                         this.fEnvironment.put(key, new EnvironmentVariable(key, value));
173         }
174
175         public String[] getEnvironment() {
176
177                 Iterator iter = fEnvironment.entrySet().iterator();
178                 List strings = new ArrayList(fEnvironment.size());
179                 while (iter.hasNext()) {
180                         Map.Entry entry = (Map.Entry) iter.next();
181                         StringBuffer buffer = new StringBuffer((String) entry.getKey());
182                         buffer.append('=').append(
183                                         ((EnvironmentVariable) entry.getValue()).getValue());
184                         strings.add(buffer.toString());
185                 }
186                 return (String[]) strings.toArray(new String[strings.size()]);
187
188         }
189
190         public String getRemoteSourcePath() {
191
192                 IProject project = getProject().getProject();
193                 if (!useRemoteDebugger())
194                         return project.getLocation().toOSString();
195                 else {
196                         try {
197                                 return configuration.getAttribute(
198                                                 PHPLaunchConfigurationAttribute.REMOTE_PATH, "");
199                         } catch (CoreException e) {
200                                 PHPLaunchingPlugin.log(e);
201                         }
202                 }
203
204                 return "";
205         }
206
207 }