1) Removed the to lower case conversion of the remote source path on Win systems...
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / PHPSourceLocator.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.util.Iterator;
4 import java.util.Map;
5
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;
10
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;
27
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;
34
35   public PHPSourceLocator() {
36
37   }
38
39   public String getAbsoluteWorkingDirectory() {
40     return absoluteWorkingDirectory;
41   }
42
43   /**
44    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
45    */
46   public String getMemento() throws CoreException {
47     return null;
48   }
49
50   /**
51    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
52    */
53   public void initializeFromMemento(String memento) throws CoreException {
54   }
55
56   /**
57    * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
58    */
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, "");
64
65         this.remoteSourcePath = new Path (configuration.getAttribute (PHPLaunchConfigurationAttribute.REMOTE_PATH, ""));
66   }
67
68   /**
69    * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
70    *
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)
74    *
75    *
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
78    */
79   public Object getSourceElement (IStackFrame stackFrame) {
80         IPath    projectPath;
81         IPath    remotePath;
82         IPath    path;
83         IPath    localPath;
84         Iterator iterator;
85         String   fileName;
86         String   file;
87         String   local;
88
89         fileName = ((PHPStackFrame) stackFrame).getFileName ();                 // Get the filename as it is submitted by DBG
90         file     = "";
91
92     if (remoteDebug) {                                              // Is it a remote debugging session
93                 path = new Path (fileName);                                 // Create a IPath object for the server side filename
94
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
100
101                                 return (projectPath.append (path)).toOSString ();       // Return the filename as absolute client side path
102                         }
103                 }
104                 else {
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)
107                         }
108
109                         iterator = pathMap.keySet().iterator();
110
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
114
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
119
120                                         projectPath = (PHPeclipsePlugin.getWorkspace().getRoot().getProject(projectName).getLocation()); // Get the absolute project path
121
122                                         return (projectPath.append (path)).toOSString ();       // Return the filename as absolute client side path
123                                 }
124                         }
125                 }
126
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)
129                 }
130
131                 iterator = pathMap.keySet().iterator();
132
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
136
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
140
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.
143                         }
144                 }
145
146                 return fileName;
147         } else {
148                 return fileName;
149     }
150   }
151
152   /**
153    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(IEditorInput, Object)
154    */
155   public String getEditorId(IEditorInput input, Object element) {
156     return PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor((String) element).getId();
157   }
158
159   /**
160    * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(Object)
161    *
162    * @param element The absolute local/client side file path
163    */
164   public IEditorInput getEditorInput (Object element) {
165         String           filename;
166         IWorkbench       workbench;
167         IWorkbenchWindow window;
168     IWorkbenchPage   page;
169         IPath            path;
170         IFile            eclipseFile;
171
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
177
178     // If the file exists in the workspace, open it
179     eclipseFile = PHPeclipsePlugin.getWorkspace().getRoot().getFileForLocation (path);
180
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 + "\".");
187 //        return null;
188 //      }
189 //    } else
190
191     if (eclipseFile == null || !eclipseFile.exists ()) {
192       //                Otherwise open the stream directly
193           //
194       if (page == null) {
195         PHPeclipsePlugin.log(IStatus.INFO, "Could not find file \"" + element + "\".");
196         return null;
197       }
198
199       FileStorage storage = new FileStorage (path);
200       storage.setReadOnly ();
201
202       //      IEditorRegistry registry = workbench.getEditorRegistry();
203       //      IEditorDescriptor desc = registry.getDefaultEditor(filename);
204       //      if (desc == null) {
205       //        desc = registry.getDefaultEditor();
206       //      }
207       return new ExternalEditorInput(storage);
208     }
209
210     return new FileEditorInput (eclipseFile);
211   }
212 }