Patches from Martin K�r:
[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.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
20 public class InterpreterRunnerConfiguration {
21         protected ILaunchConfiguration configuration;
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(PHPLaunchConfigurationAttribute.FILE_NAME, "No file specified in configuration");
41                 } catch(CoreException e) {}
42                 
43                 return fileName.replace('\\', '/');
44         } 
45         
46         public JavaProject getProject() {
47                 String projectName = "";
48                 
49                 try {
50                         projectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
51                 } catch(CoreException e) {
52                         PHPLaunchingPlugin.log(e);
53                 }
54
55                 IProject project = PHPLaunchingPlugin.getWorkspace().getRoot().getProject(projectName);
56
57                 JavaProject phpProject = new JavaProject();
58                 phpProject.setProject(project);
59                 return phpProject;
60         }
61         
62         public File getAbsoluteWorkingDirectory() {
63                 String file = null;
64                 try {
65                         file = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
66                 } catch(CoreException e) {
67                         PHPLaunchingPlugin.log(e);
68                 }
69                 return new File(file);
70         }
71         
72         public String getInterpreterArguments() {
73                 try {
74                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.INTERPRETER_ARGUMENTS, "");
75                 } catch(CoreException e) {}
76                 
77                 return "";
78         }
79         
80         public String getProgramArguments() {
81                 try {
82                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.PROGRAM_ARGUMENTS, "");
83                 } catch (CoreException e) {}
84                 
85                 return "";
86         }
87
88         public PHPInterpreter getInterpreter() {
89                 String selectedInterpreter = null;
90                 try {
91                         selectedInterpreter = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
92                 } catch(CoreException e) {}
93
94                 return PHPRuntime.getDefault().getInterpreter(selectedInterpreter);
95         }
96         
97         public boolean useRemoteDebugger() {
98                 try {
99                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG, false);
100                 } catch(CoreException e) {
101                         PHPLaunchingPlugin.log(e);
102                 }
103                 return false;
104         }
105         
106         public boolean usePathTranslation() {
107                 try {
108                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_DEBUG_TRANSLATE, false);
109                 } catch(CoreException e) {
110                         PHPLaunchingPlugin.log(e);
111                 }
112                 return false;
113         }
114         
115         public Map getPathMap() {
116                 try {
117                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_MAP, (Map) null);
118                 } catch(CoreException e) {
119                 PHPLaunchingPlugin.log(e);
120                 }
121                 return (Map) null;
122         }
123         
124         public boolean useDBGSessionInBrowser() {
125                 try {
126                         return configuration.getAttribute(PHPLaunchConfigurationAttribute.OPEN_DBGSESSION_IN_BROWSER, true);
127                 } catch(CoreException e) {
128                         PHPLaunchingPlugin.log(e);
129                 }
130                 return false;
131         }
132         
133         public void setEnvironment(String[] envp)
134         {
135                 if (envp== null)
136                         return;
137                 for (int i = 0; i<envp.length; i++ ) {
138                         addEnvironmentValue(envp[i],true);
139                 }
140         }
141         
142         public void addEnvironmentValue(String env,boolean replace)
143         {
144                 String value = env.substring(env.indexOf('=')+1);
145                 String key = env.substring(0,env.indexOf('='));
146                 addEnvironmentValue(key,value,replace);
147         }
148         
149         public void addEnvironmentValue(String key, String value,boolean replace)
150         {
151                 if (!replace && fEnvironment.containsKey(key)) {
152                         EnvironmentVariable ev = (EnvironmentVariable) fEnvironment.get(key);
153                         ev.setValue(ev.getValue()+";"+value);
154                         fEnvironment.put(key,ev);
155                 } else
156                         this.fEnvironment.put(key, new EnvironmentVariable(key, value));
157         }
158         
159         public String[] getEnvironment()
160         {
161
162                 Iterator iter= fEnvironment.entrySet().iterator();
163                 List strings= new ArrayList(fEnvironment.size());
164                 while (iter.hasNext()) {
165                         Map.Entry entry = (Map.Entry) iter.next();
166                         StringBuffer buffer= new StringBuffer((String) entry.getKey());
167                         buffer.append('=').append(((EnvironmentVariable) entry.getValue()).getValue());
168                         strings.add(buffer.toString());
169                 }
170                 return (String[]) strings.toArray(new String[strings.size()]);
171
172         }       
173         
174         public String getRemoteSourcePath() {
175                 
176                 IProject project = getProject().getProject();
177                 if (!useRemoteDebugger())
178                         return project.getLocation().toOSString();
179                 else
180                 {               
181                         try {
182                                 return configuration.getAttribute(PHPLaunchConfigurationAttribute.REMOTE_PATH, "");
183                         } catch(CoreException e) {
184                                 PHPLaunchingPlugin.log(e);
185                         }
186                 }       
187
188                 return "";
189         }
190
191 }