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