1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / launching / PHPLaunchConfigurationDelegate.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. and others.
3  All rights reserved. This program and the accompanying materials
4  are made available under the terms of the Common Public License v1.0
5  which accompanies this distribution, and is available at
6  http://www.eclipse.org/legal/cpl-v10.html
7  
8  Contributors:
9  IBM Corporation - Initial implementation
10  Vicente Fernando - www.alfersoft.com.ar
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.xdebug.php.launching;
13
14 import java.io.File;
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
22 import net.sourceforge.phpeclipse.xdebug.core.IProxyEventListener;
23 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
24 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
25 import net.sourceforge.phpeclipse.xdebug.core.XDebugProxy;
26 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.debug.core.DebugPlugin;
36 import org.eclipse.debug.core.ILaunch;
37 import org.eclipse.debug.core.ILaunchConfiguration;
38 import org.eclipse.debug.core.ILaunchManager;
39 //import org.eclipse.debug.core.model.IDebugTarget;
40 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
41 import org.eclipse.debug.core.model.IProcess;
42 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
43
44
45 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
46         
47         /**
48          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
49          */
50         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
51                 List commandList = new ArrayList();
52                 
53                 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
54                 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
55
56                 if (useDefaultInterpreter)       {      
57                         phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
58                         if (phpInterpreter=="") {
59                                 phpInterpreter=ExternalToolsPlugin.getDefault().getPreferenceStore().getString(ExternalToolsPlugin.PHP_RUN_PREF);
60                         }
61                 }
62                 File exe = new File(phpInterpreter);
63                 // Just to get sure that the interpreter exists
64                 if (!exe.exists()) {
65                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
66                 }
67                 commandList.add(phpInterpreter);
68                 
69                 // Project name
70                 String projectName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
71                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
72 //               Just to get sure that the project exists
73                 if (project == null) {
74                         abort("Project does not exist.", null);
75                 }
76                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
77
78                 IFile file = project.getFile(fileName);
79                 // Just to get sure that the script exists
80                 if (!file.exists()) {
81                         abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
82                 }
83                 
84                 commandList.add(file.getLocation().toOSString());
85
86                 // Get the Debugport  from the preferences
87                 int debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
88                 
89                 // check for default port
90                 if (debugPort==0)
91                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
92
93                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
94                 // appends the environment to the native environment
95                 if (envp==null) {
96                         Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
97                         int idx=0;
98                         envp= new String[stringVars.size()];
99                         for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
100                                 String key = (String) i.next();
101                                 String value = (String) stringVars.get(key);
102                                 envp[idx++]=key+"="+value;
103                         }
104                 }
105                 String idekey=fileName+"-"+(int)(Math.random()*100000);
106                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
107                         String[] env = new String[envp.length+1];
108                         for(int i=0;i<envp.length;i++)
109                                         env[i+1]=envp[i];
110                         env[0]="XDEBUG_CONFIG=idekey="+idekey+" remote_enable=1 remote_port="+debugPort;
111                         envp=env;
112                 }
113                 System.out.println("ideKey= "+idekey);
114                 
115                 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
116
117                 XDebugProxy proxy = XDebugCorePlugin.getDefault().getXDebugProxy();
118                 proxy.start();
119                 
120                 XDebugTarget target = null;
121                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
122                         target = new XDebugTarget(launch, null, idekey);
123                         proxy.addProxyEventListener((IProxyEventListener) target, idekey);
124                 }
125
126                 Process process = DebugPlugin.exec(commandLine, null,envp);
127                 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
128         
129                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
130                         target.addProcess(p);
131                         launch.addDebugTarget(target);
132                 }
133
134         }
135         
136         /**
137          * Throws an exception with a new status containing the given
138          * message and optional exception.
139          * 
140          * @param message error message
141          * @param e underlying exception
142          * @throws CoreException
143          */
144         private void abort(String message, Throwable e) throws CoreException {
145                 // TODO: the plug-in code should be the example plug-in, not Perl debug model id
146                 throw new CoreException(new Status(IStatus.ERROR, IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));
147         }
148
149 }