1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

generalize ProjectRelativeLocationConverter

This commit is contained in:
Andrew Ferguson 2007-02-27 08:54:51 +00:00
parent 30d2f050b4
commit 7028cddf94
3 changed files with 31 additions and 33 deletions

View file

@ -26,7 +26,7 @@ import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.index.ProjectRelativeLocationConverter;
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
import org.eclipse.cdt.core.index.URIRelativeLocationConverter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
@ -157,15 +157,15 @@ public class IndexLocationTest extends BaseTestCase {
}
}
public void testProjectRelativeLocationConverter() throws Exception {
public void testResourceContainerRelativeLocationConverter() throws Exception {
String[] paths = new String[] {"this.cpp", "inc/header.h", "a b c/d/e f/g.h", "a \\b /c.d"};
for(int i=0; i<paths.length; i++) {
IFile file= cproject.getProject().getFile(paths[i]);
IIndexFileLocation ifl1= IndexLocationFactory.getWorkspaceIFL(file);
ProjectRelativeLocationConverter prlc1= new ProjectRelativeLocationConverter(cproject);
ResourceContainerRelativeLocationConverter prlc1= new ResourceContainerRelativeLocationConverter(cproject.getProject());
String r1= prlc1.toInternalFormat(ifl1);
assertNotNull(r1);
ProjectRelativeLocationConverter prlc2= new ProjectRelativeLocationConverter(emptyCProject);
ResourceContainerRelativeLocationConverter prlc2= new ResourceContainerRelativeLocationConverter(emptyCProject.getProject());
IIndexFileLocation ifl2= prlc2.fromInternalFormat(r1);
assertNotNull(ifl2);
assertEquals(
@ -175,7 +175,7 @@ public class IndexLocationTest extends BaseTestCase {
}
}
public void testURLC_PRLC_Interaction1() throws Exception {
public void testURLC_RCRLC_Interaction1() throws Exception {
String[] paths = new String[] {
"c:/foo/bar/baz.cpp",
"c:\\foo\\bar\\a b c\\baz.cpp",
@ -196,7 +196,7 @@ public class IndexLocationTest extends BaseTestCase {
URIRelativeLocationConverter urlc = new URIRelativeLocationConverter(base);
String r1 = urlc.toInternalFormat(ifl1);
assertNotNull(r1);
ProjectRelativeLocationConverter prlc= new ProjectRelativeLocationConverter(cproject);
ResourceContainerRelativeLocationConverter prlc= new ResourceContainerRelativeLocationConverter(cproject.getProject());
IIndexFileLocation ifl2= prlc.fromInternalFormat(r1);
String r2= prlc.toInternalFormat(ifl2);
assertNotNull(r2);
@ -207,7 +207,7 @@ public class IndexLocationTest extends BaseTestCase {
}
}
public void testURLC_PRLC_Interaction2() throws Exception {
public void testURLC_RCRLC_Interaction2() throws Exception {
String[] paths = new String[] {
"a b c/d/e f/g.h",
"a \\b /c.d",
@ -222,7 +222,7 @@ public class IndexLocationTest extends BaseTestCase {
for(int i=0; i<paths.length; i++) {
IFile file= cproject.getProject().getFile(paths[i]);
IIndexFileLocation ifl1= IndexLocationFactory.getWorkspaceIFL(file);
ProjectRelativeLocationConverter prlc= new ProjectRelativeLocationConverter(cproject);
ResourceContainerRelativeLocationConverter prlc= new ResourceContainerRelativeLocationConverter(cproject.getProject());
String r1= prlc.toInternalFormat(ifl1);
assertNotNull(r1);
URI base = URIUtil.toURI("c:/foo/bar/");

View file

@ -7,57 +7,55 @@
*
* Contributors:
* Andrew Ferguson (Symbian) - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.core.index;
import org.eclipse.cdt.core.model.ICProject;
*******************************************************************************/
package org.eclipse.cdt.core.index;
import org.eclipse.cdt.internal.core.index.IndexFileLocation;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Path;
/**
* A location converter for converting project resource locations to be project relative. Resources outside of
* the associated project will be ignored.
* A location converter for converting project resource locations to be relative to a specified container.
* Resources outside of the associated project will not be converted (ignored).
* <br>
* This location converter is internal-representation-compatible with URIRelativeLocationConverter
*/
/*
* Internal representation is project relative path
* Internal representation is a relative path
*/
public class ProjectRelativeLocationConverter implements IIndexLocationConverter {
public class ResourceContainerRelativeLocationConverter implements IIndexLocationConverter {
protected IWorkspaceRoot root;
protected String cprojectName;
protected IPath fullPath;
/**
* @param cproject the CDT project to convert relative to
* @param container the resource container to convert relative to
*/
public ProjectRelativeLocationConverter(ICProject cproject) {
this.cprojectName = cproject.getProject().getName();
public ResourceContainerRelativeLocationConverter(IContainer container) {
this.fullPath = container.getFullPath();
this.root = ResourcesPlugin.getWorkspace().getRoot();
}
}
/*
* (non-Javadoc)
* @see org.eclipse.cdt.core.index.IIndexLocationConverter#fromInternalFormat(java.lang.String)
*/
public IIndexFileLocation fromInternalFormat(String raw) {
IResource member= root.getFile(new Path(cprojectName +"/"+ raw)); //$NON-NLS-1$
IResource member= root.getFile(fullPath.append(raw));
return new IndexFileLocation(member.getLocationURI(), member.getFullPath().toString());
}
}
/*
* (non-Javadoc)
* @see org.eclipse.cdt.core.index.IIndexLocationConverter#toInternalFormat(org.eclipse.cdt.core.index.IIndexFileLocation)
*/
public String toInternalFormat(IIndexFileLocation location) {
String fullPath= location.getFullPath();
if(fullPath!=null) {
IPath path = new Path(fullPath).removeFirstSegments(1);
return path.toString();
String sFullPath= location.getFullPath();
if(sFullPath!=null) {
IPath path= new Path(sFullPath);
if(fullPath.isPrefixOf(path)) {
return path.removeFirstSegments(fullPath.segmentCount()).toString();
}
}
return null;
}
}
}

View file

@ -19,7 +19,7 @@ import org.eclipse.core.filesystem.URIUtil;
* A IIndexLocationConverter for converting relative paths within an index, by prefixing them
* with the supplied base URI
* <br>
* This location converter is internal-representation-compatible with ProjectRelativeLocationConverter
* This location converter is internal-representation-compatible with ResourceContainerRelativeLocationConverter
*/
/*
* Internal representation is uri relative path (non encoded form)