1 package net.sourceforge.phpeclipse.xdebug.php.launching;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import net.sourceforge.phpeclipse.xdebug.core.PathMapItem;
7 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugStackFrame;
8 
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.MultiStatus;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupParticipant;
17 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
18 import org.eclipse.debug.internal.core.sourcelookup.SourceLookupMessages;
19 
20 public class PHPSourceLookupParticipant extends AbstractSourceLookupParticipant {
21 
22 	/* (non-Javadoc)
23 	 * @see org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant#getSourceName(Object)
24 	 */
getSourceName(Object object)25 	public String getSourceName(Object object) throws CoreException {
26 		if (object instanceof XDebugStackFrame) {
27 			return ((XDebugStackFrame) object).getSourceName();
28 		}
29 		return null;
30 	}
31 
32 	/* (non-Javadoc)
33 	 * @see org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant#findSourceElements(java.lang.Object)
34 	 */
findSourceElements(Object object)35 	public Object[] findSourceElements(Object object) throws CoreException {
36 		if (object == null) {
37 			return new Object[] {};
38 		}
39 		XDebugStackFrame stackFrame = null;
40 		if (object instanceof XDebugStackFrame) {
41 			stackFrame = (XDebugStackFrame) object;
42 		} else {
43 			return new Object[] {};
44 		}
45 
46 		List results = null;
47 		CoreException single = null;
48 		MultiStatus multiStatus = null;
49 
50 		if (isFindDuplicates()) {
51 			results = new ArrayList();
52 		}
53 
54 		String name = getSourceName(object);
55 		if (name == null || name.length() == 0) {
56 			return new Object[] {};
57 		}
58 
59 		// here our stackframe is guaranteed not to be null
60 		IPath sLocalPath = null;
61 
62 		if (((XDebugStackFrame) object).getThread() == null) {
63 			IPath sPath = new Path(stackFrame.getFullName().getPath());
64 			List pathMap = getDirector().getLaunchConfiguration()
65 					.getAttribute(IXDebugConstants.ATTR_PHP_PATHMAP, (List) null);
66 
67 			PathMapItem pmi = null;
68 			for (int k = 0; k < pathMap.size(); k++) {
69 				pmi = new PathMapItem((String) pathMap.get(k));
70 
71 				IPath local = new Path(pmi.getLocalPath().toString());
72 				IPath remote = new Path(pmi.getRemotePath().toString());
73 
74 				if (remote.matchingFirstSegments(sPath) == remote.segmentCount()) {
75 					sLocalPath = local;
76 				}
77 			}
78 		} else {
79 
80 		}
81 
82 		String Type = stackFrame.getType();
83 
84 		if (Type.equals("eval")) {
85 			results.add("pippo");
86 			return results.toArray();
87 		}
88 
89 		ISourceContainer[] containers = getSourceContainers();
90 		for (int i = 0; i < containers.length; i++) {
91 			ISourceContainer container = getDelegateContainer(containers[i]);
92 			if (container == null) {
93 				continue;
94 			}
95 
96 			try {
97 				Object[] objects = container.findSourceElements(name);
98 				if (objects.length > 0) {
99 					if (isFindDuplicates()) {
100 						if (((XDebugStackFrame) object).getThread() == null) {
101 							addMatching(results, sLocalPath, objects);
102 						} else {
103 							return objects;
104 						}
105 					} else {
106 						if (objects.length == 1) {
107 							return objects;
108 						}
109 						return new Object[] { objects[0] };
110 					}
111 				}
112 			} catch (CoreException e) {
113 				if (single == null) {
114 					single = e;
115 				} else if (multiStatus == null) {
116 					multiStatus = new MultiStatus(DebugPlugin
117 							.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR,
118 							new IStatus[] { single.getStatus() },
119 							SourceLookupMessages.DefaultSourceContainer_0/*CompositeSourceContainer_0*/,
120 							null);
121 					multiStatus.add(e.getStatus());
122 				} else {
123 					multiStatus.add(e.getStatus());
124 				}
125 			}
126 		}
127 		if (results == null) {
128 			if (multiStatus != null) {
129 				throw new CoreException(multiStatus);
130 			} else if (single != null) {
131 				throw single;
132 			}
133 			return EMPTY;
134 		}
135 		return results.toArray();
136 	}
137 
addMatching(List results, IPath localPath, Object[] objects)138 	static void addMatching(List results, IPath localPath, Object[] objects) {
139 		if (results == null || localPath == null || objects == null) {
140 			return;
141 		}
142 		for (int j = 0; j < objects.length; j++) {
143 			if (objects[j] == null || !(objects[j] instanceof IFile)) {
144 				continue;
145 			}
146 			IFile file = (IFile) objects[j];
147 
148 			IPath path = new Path(file.getLocation().toString());
149 			if (localPath.matchingFirstSegments(path) == localPath
150 					.segmentCount()) {
151 				results.add(objects[j]);
152 			}
153 		}
154 	}
155 }