1 package net.sourceforge.phpdt.internal.launching;
3 import java.util.Iterator;
6 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.builder.ExternalEditorInput;
9 import net.sourceforge.phpeclipse.builder.FileStorage;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.model.IPersistableSourceLocator;
19 import org.eclipse.debug.core.model.IStackFrame;
20 import org.eclipse.debug.ui.ISourcePresentation;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.part.FileEditorInput;
28 public class PHPSourceLocator implements IPersistableSourceLocator, ISourcePresentation {
29 private String absoluteWorkingDirectory;
30 private Map pathMap = null;
31 private boolean remoteDebug;
32 private IPath remoteSourcePath;
33 private String projectName;
35 public PHPSourceLocator() {
39 public String getAbsoluteWorkingDirectory() {
40 return absoluteWorkingDirectory;
44 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
46 public String getMemento() throws CoreException {
51 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
53 public void initializeFromMemento(String memento) throws CoreException {
57 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
59 public void initializeDefaults (ILaunchConfiguration configuration) throws CoreException {
60 this.absoluteWorkingDirectory = configuration.getAttribute (PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
61 this.remoteDebug = configuration.getAttribute (PHPLaunchConfigurationAttribute.REMOTE_DEBUG,false);
62 this.pathMap = configuration.getAttribute (PHPLaunchConfigurationAttribute.FILE_MAP, (Map)null);
63 this.projectName = configuration.getAttribute (PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
65 this.remoteSourcePath = new Path (configuration.getAttribute (PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
69 * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
71 * Return the client side source filename for the server side source file.
72 * E.g. when cross debugging, the server side filename could be /var/www/index.php
73 * on the client side it is either a Eclipse_PHP_projectname\index.php (when it is a linked file)
76 * @param stackFrame The stackframe for which we want the client side source file name
77 * @return The filename as it appears on the client side
79 public Object getSourceElement (IStackFrame stackFrame) {
89 fileName = ((PHPStackFrame) stackFrame).getFileName (); // Get the filename as it is submitted by DBG
92 if (remoteDebug) { // Is it a remote debugging session
93 path = new Path (fileName); // Create a IPath object for the server side filename
95 if (!remoteSourcePath.isEmpty()) {
96 if (remoteSourcePath.isPrefixOf (path)) { // Is the server side filename with the remote source path
97 path = path.removeFirstSegments (remoteSourcePath.matchingFirstSegments (path)); // Remove the remote source path
98 file = path.toString (); // The filename without the remote source path
99 projectPath = (PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation()); // Get the absolute project path
101 return (projectPath.append (path)).toOSString (); // Return the filename as absolute client side path
105 if (pathMap == null) { // Do we have path mapping (e.g. for cross platform debugging)
106 return fileName; // No, then return the filename as it given by DBG (the full server side path)
109 iterator = pathMap.keySet().iterator();
111 while (iterator.hasNext ()) {
112 local = (String) iterator.next (); // Get the local/client side path of the mapping
113 remotePath = new Path ((String) pathMap.get (local)); // Get the remote/server side path of the mapping
115 if (remotePath.isPrefixOf (path)) { // Starts the remote/server side file path with the remote/server side mapping path
116 path = path.removeFirstSegments (remotePath.matchingFirstSegments (path)); // Remove the absolute path from filename
117 localPath = new Path (local); // Create new IPath object for the local/client side path
118 path = localPath.append (path); // Prepend the project relative path to filename
120 projectPath = (PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation()); // Get the absolute project path
122 return (projectPath.append (path)).toOSString (); // Return the filename as absolute client side path
127 if (pathMap == null) { // Do we have path mapping (e.g. for cross platform debugging)
128 return fileName; // No, then return the filename as it given by DBG (the full server side path)
131 iterator = pathMap.keySet().iterator();
133 while (iterator.hasNext ()) {
134 local = (String) iterator.next (); // Get the local/client side path of the mapping
135 remotePath = new Path ((String) pathMap.get (local)); // Get the remote/server side path of the mapping
137 if (remotePath.isPrefixOf (path)) { // Starts the remote/server side file path with the remote/server side mapping path
138 path = path.removeFirstSegments (remotePath.matchingFirstSegments (path)); // Remove the absolute path from filename
139 localPath = new Path (local); // Create new IPath object for the local/client side path
141 return localPath.append (path).toOSString (); // Append the remote filename to the client side path (So we return the absolute path
142 // to the source file as the client side sees it.
153 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
155 public String getEditorId(IEditorInput input, Object element) {
156 return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
160 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
162 * @param element The absolute local/client side file path
164 public IEditorInput getEditorInput (Object element) {
166 IWorkbench workbench;
167 IWorkbenchWindow window;
172 filename = (String) element;
173 workbench = PlatformUI.getWorkbench ();
174 window = workbench.getWorkbenchWindows ()[0];
175 page = window.getActivePage ();
176 path = new Path (filename); // Create an IPath object of the absolute local/client side file name
178 // If the file exists in the workspace, open it
179 eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation (path);
181 // IFile eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
182 // if (eclipseFile == null) {
183 // filename = this.getAbsoluteWorkingDirectory() + "/" + filename;
184 // eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation(new Path(filename));
185 // if (eclipseFile == null) {
186 // PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
191 if (eclipseFile == null || !eclipseFile.exists ()) {
192 // Otherwise open the stream directly
195 PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
199 FileStorage storage = new FileStorage (path);
200 storage.setReadOnly ();
202 // IEditorRegistry registry = workbench.getEditorRegistry();
203 // IEditorDescriptor desc = registry.getDefaultEditor(filename);
204 // if (desc == null) {
205 // desc = registry.getDefaultEditor();
207 return new ExternalEditorInput(storage);
210 return new FileEditorInput (eclipseFile);