37
|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2006, 2008 Nokia and others.
|
|
3 |
* All rights reserved. This program and the accompanying materials
|
|
4 |
* are made available under the terms of the Eclipse Public License v1.0
|
|
5 |
* which accompanies this distribution, and is available at
|
|
6 |
* http://www.eclipse.org/legal/epl-v10.html
|
|
7 |
*
|
|
8 |
* Contributors:
|
|
9 |
* Nokia - Initial implementation (159833)
|
|
10 |
* Broadcom - http://bugs.eclipse.org/247853
|
|
11 |
*******************************************************************************/
|
|
12 |
|
|
13 |
package org.eclipse.cdt.debug.core.sourcelookup;
|
|
14 |
|
|
15 |
import java.io.File;
|
|
16 |
import java.io.IOException;
|
|
17 |
|
|
18 |
import org.eclipse.cdt.core.model.CoreModel;
|
|
19 |
import org.eclipse.cdt.core.model.ICProject;
|
|
20 |
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
|
|
21 |
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
|
|
22 |
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
|
|
23 |
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
|
|
24 |
import org.eclipse.core.resources.IFile;
|
|
25 |
import org.eclipse.core.resources.IProject;
|
|
26 |
import org.eclipse.core.resources.ResourcesPlugin;
|
|
27 |
import org.eclipse.core.runtime.CoreException;
|
|
28 |
import org.eclipse.core.runtime.IPath;
|
|
29 |
import org.eclipse.core.runtime.Path;
|
|
30 |
import org.eclipse.debug.core.ILaunchConfiguration;
|
|
31 |
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
|
|
32 |
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
|
|
33 |
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
|
|
34 |
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
|
|
35 |
|
|
36 |
public class AbsolutePathSourceContainer extends AbstractSourceContainer {
|
|
37 |
/**
|
|
38 |
* Unique identifier for the absolute source container type
|
|
39 |
* (value <code>org.eclipse.cdt.debug.core.containerType.absolutePath</code>).
|
|
40 |
*/
|
|
41 |
public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.absolutePath"; //$NON-NLS-1$
|
|
42 |
|
|
43 |
private Object[] findSourceElementByFile(File file) {
|
|
44 |
IFile[] wfiles = ResourceLookup.findFilesForLocation(new Path(file.getAbsolutePath()));
|
|
45 |
if (wfiles.length > 0) {
|
|
46 |
ResourceLookup.sortFilesByRelevance(wfiles, getProject());
|
|
47 |
return wfiles;
|
|
48 |
}
|
|
49 |
|
|
50 |
try {
|
|
51 |
// Check the canonical path as well to support case insensitive file
|
|
52 |
// systems like Windows.
|
|
53 |
wfiles = ResourceLookup.findFilesForLocation(new Path(file.getCanonicalPath()));
|
|
54 |
if (wfiles.length > 0) {
|
|
55 |
ResourceLookup.sortFilesByRelevance(wfiles, getProject());
|
|
56 |
return wfiles;
|
|
57 |
}
|
|
58 |
|
|
59 |
// The file is not already in the workspace so try to create an external translation unit for it.
|
|
60 |
ISourceLookupDirector director = getDirector();
|
|
61 |
if (director != null)
|
|
62 |
{
|
|
63 |
ILaunchConfiguration launchConfiguration = director.getLaunchConfiguration();
|
|
64 |
if (launchConfiguration != null)
|
|
65 |
{
|
|
66 |
String projectName = launchConfiguration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
|
|
67 |
if (projectName.length() > 0) {
|
|
68 |
ICProject project = CoreModel.getDefault().getCModel().getCProject(projectName);
|
|
69 |
if (project != null)
|
|
70 |
{
|
|
71 |
IPath path = Path.fromOSString(file.getCanonicalPath());
|
|
72 |
String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment());
|
|
73 |
return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, path, id) };
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
} catch (IOException e) { // ignore if getCanonicalPath throws
|
|
79 |
} catch (CoreException e) {
|
|
80 |
}
|
|
81 |
|
|
82 |
// If we can't create an ETU then fall back on LocalFileStorage.
|
|
83 |
return new LocalFileStorage[] { new LocalFileStorage(file) };
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Find the project associated with the current launch configuration
|
|
88 |
* @return IProject or null
|
|
89 |
*/
|
|
90 |
private IProject getProject() {
|
57
|
91 |
ISourceLookupDirector director = getDirector();
|
|
92 |
if (director != null)
|
|
93 |
{
|
|
94 |
ILaunchConfiguration config = director.getLaunchConfiguration();
|
|
95 |
if (config != null) {
|
|
96 |
try {
|
|
97 |
String name = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
|
|
98 |
if (name.length() > 0)
|
|
99 |
return ResourcesPlugin.getWorkspace().getRoot().getProject(name);
|
|
100 |
} catch (CoreException e) {
|
|
101 |
// Don't care carry on search using other heuristics
|
|
102 |
}
|
37
|
103 |
}
|
|
104 |
}
|
|
105 |
return null;
|
|
106 |
}
|
|
107 |
|
|
108 |
public boolean isValidAbsoluteFilePath( String name )
|
|
109 |
{
|
|
110 |
return isValidAbsoluteFilePath( new File(name) );
|
|
111 |
}
|
|
112 |
|
|
113 |
public boolean isValidAbsoluteFilePath( File file )
|
|
114 |
{
|
|
115 |
return file.isAbsolute() && file.exists() && file.isFile();
|
|
116 |
}
|
|
117 |
|
|
118 |
public Object[] findSourceElements( String name ) throws CoreException {
|
|
119 |
if ( name != null ) {
|
|
120 |
File file = new File( name );
|
|
121 |
if ( isValidAbsoluteFilePath(file) ) {
|
|
122 |
return findSourceElementByFile( file );
|
|
123 |
}
|
|
124 |
}
|
|
125 |
return new Object[0];
|
|
126 |
}
|
|
127 |
|
|
128 |
public String getName() {
|
|
129 |
return SourceLookupMessages.getString( "AbsolutePathSourceContainer.0" ); //$NON-NLS-1$
|
|
130 |
}
|
|
131 |
|
|
132 |
/* (non-Javadoc)
|
|
133 |
* @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
|
|
134 |
*/
|
|
135 |
public ISourceContainerType getType() {
|
|
136 |
return getSourceContainerType( TYPE_ID );
|
|
137 |
}
|
|
138 |
|
|
139 |
@Override
|
|
140 |
public int hashCode() {
|
|
141 |
return TYPE_ID.hashCode();
|
|
142 |
}
|
|
143 |
|
|
144 |
@Override
|
|
145 |
public boolean equals(Object obj) {
|
|
146 |
if (!(obj instanceof AbsolutePathSourceContainer))
|
|
147 |
return false;
|
|
148 |
return true;
|
|
149 |
}
|
|
150 |
}
|