1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Improved the source search algorithm.

This commit is contained in:
Mikhail Khodjaiants 2003-10-14 18:41:41 +00:00
parent 4af9a7f786
commit 833492ccf0
2 changed files with 93 additions and 33 deletions

View file

@ -1,3 +1,7 @@
2003-10-14 Mikhail Khodjaiants
Improved the source search algorithm.
* CProjectSourceLocation.java
2003-10-13 Mikhail Khodjaiants 2003-10-13 Mikhail Khodjaiants
Fix for bug 43372: changed the source lookup procedures to avoid the usage of 'getCanonicalPath'. Fix for bug 43372: changed the source lookup procedures to avoid the usage of 'getCanonicalPath'.
* CDirectorySourceLocation.java * CDirectorySourceLocation.java

View file

@ -12,7 +12,6 @@ import java.text.MessageFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -31,6 +30,7 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -76,7 +76,7 @@ public class CProjectSourceLocation implements IProjectSourceLocation
{ {
setProject( project ); setProject( project );
fGenerated = true; fGenerated = true;
initializeFolders(); // initializeFolders();
} }
/** /**
@ -86,7 +86,7 @@ public class CProjectSourceLocation implements IProjectSourceLocation
{ {
setProject( project ); setProject( project );
fGenerated = generated; fGenerated = generated;
initializeFolders(); // initializeFolders();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -158,10 +158,19 @@ public class CProjectSourceLocation implements IProjectSourceLocation
private Object findFileByAbsolutePath( String name ) private Object findFileByAbsolutePath( String name )
{ {
File file = new File( name ); File file = new File( name );
Object result = null; LinkedList list = new LinkedList();
if ( file.isAbsolute() && file.exists() ) if ( file.isAbsolute() && file.exists() )
result = findFile( file ); {
IResource[] folders = getFolders();
for ( int i = 0; i < folders.length; ++i )
{
Object result = findFile( folders[i], file );
if ( !searchForDuplicateFileNames() )
return result; return result;
list.add( result );
}
}
return ( list.size() > 0 ) ? list : null;
} }
private Object findFileByRelativePath( String fileName ) private Object findFileByRelativePath( String fileName )
@ -173,48 +182,97 @@ public class CProjectSourceLocation implements IProjectSourceLocation
if ( list.size() > 0 && !searchForDuplicateFileNames() ) if ( list.size() > 0 && !searchForDuplicateFileNames() )
break; break;
IPath path = folders[i].getLocation().append( fileName ); IPath path = folders[i].getLocation().append( fileName );
Object result = findFile( new File( path.toOSString() ) ); Object result = findFile( folders[i], new File( path.toOSString() ) );
if ( result instanceof List ) if ( result != null )
list.addAll( (List)result ); {
else if ( result != null ) if ( !searchForDuplicateFileNames() )
return result;
list.add( result ); list.add( result );
} }
if ( list.size() == 1 || (list.size() > 0 && !searchForDuplicateFileNames()) ) }
return list.getFirst(); return ( list.size() > 0 ) ? list : null;
if ( list.size() > 0 )
return list;
return null;
} }
private Object findFile( final File file ) // private Object findFile( final File file )
{ // {
if ( file != null ) // if ( file != null )
{ // {
final String name = file.getName(); // final String name = file.getName();
IResource[] folders = getFolders(); // IResource[] folders = getFolders();
final LinkedList list = new LinkedList(); // final LinkedList list = new LinkedList();
for ( int i = 0; i < folders.length; ++i ) // for ( int i = 0; i < folders.length; ++i )
// {
// if ( list.size() > 0 && !searchForDuplicateFileNames() )
// break;
// try
// {
// folders[i].accept(
// new IResourceProxyVisitor()
// {
// public boolean visit( IResourceProxy proxy ) throws CoreException
// {
// // use equalsIgnoreCase to make it work for Wondows
// if ( proxy.getType() == IResource.FILE && proxy.getName().equalsIgnoreCase( name ) )
// {
// IResource resource = proxy.requestResource();
// File file1 = new File( resource.getLocation().toOSString() );
// if ( file1.exists() && file1.equals( file ) )
// list.addLast( resource );
// return false;
// }
// else if ( proxy.getType() == IResource.FOLDER )
// return false;
// return true;
// }
// },
// IResource.NONE );
// }
// catch( CoreException e )
// {
// }
// }
// if ( list.size() == 1 || (list.size() > 0 && !searchForDuplicateFileNames()) )
// return list.getFirst();
// if ( list.size() > 0 )
// return list;
// }
// return null;
// }
private Object findFile( IResource folder, final File file )
{ {
// The workspace resources are case-sensitive, so we can not just // The workspace resources are case-sensitive, so we can not just
// append the file name to the folder name and check if the result exists. // append the file name to the folder name and check if the result exists.
if ( list.size() > 0 && !searchForDuplicateFileNames() )
break; final Object[] result = new Object[] { null };
if ( file != null && folder != null )
{
final String name = file.getName();
IPath dirPath = new Path( file.getAbsolutePath() );
dirPath = dirPath.removeLastSegments( 1 );
final File dir = new File( dirPath.toOSString() );
if ( dir.equals( folder.getLocation().toFile() ) )
{
try try
{ {
folders[i].accept( folder.accept(
new IResourceProxyVisitor() new IResourceProxyVisitor()
{ {
public boolean visit( IResourceProxy proxy ) throws CoreException public boolean visit( IResourceProxy proxy ) throws CoreException
{ {
if ( result[0] != null )
return false;
// use equalsIgnoreCase to make it work for Wondows // use equalsIgnoreCase to make it work for Wondows
if ( proxy.getType() == IResource.FILE && proxy.getName().equalsIgnoreCase( name ) ) if ( proxy.getType() == IResource.FILE && proxy.getName().equalsIgnoreCase( name ) )
{ {
IResource resource = proxy.requestResource(); IResource resource = proxy.requestResource();
File file1 = new File( resource.getLocation().toOSString() ); File file1 = new File( resource.getLocation().toOSString() );
if ( file1.exists() && file1.equals( file ) ) if ( file1.exists() && file1.equals( file ) )
list.addLast( resource ); result[0] = resource;
return false; return false;
} }
else if ( proxy.getType() == IResource.FOLDER )
return false;
return true; return true;
} }
}, },
@ -224,12 +282,8 @@ public class CProjectSourceLocation implements IProjectSourceLocation
{ {
} }
} }
if ( list.size() == 1 || (list.size() > 0 && !searchForDuplicateFileNames()) )
return list.getFirst();
if ( list.size() > 0 )
return list;
} }
return null; return result[0];
} }
private Object cacheLookup( String name ) private Object cacheLookup( String name )
@ -308,7 +362,7 @@ public class CProjectSourceLocation implements IProjectSourceLocation
if ( isGeneric == null || isGeneric.trim().length() == 0 ) if ( isGeneric == null || isGeneric.trim().length() == 0 )
isGeneric = Boolean.FALSE.toString(); isGeneric = Boolean.FALSE.toString();
setGenerated( isGeneric.equals( Boolean.TRUE.toString() ) ); setGenerated( isGeneric.equals( Boolean.TRUE.toString() ) );
initializeFolders(); // initializeFolders();
return; return;
} }
catch( ParserConfigurationException e ) catch( ParserConfigurationException e )
@ -403,6 +457,8 @@ public class CProjectSourceLocation implements IProjectSourceLocation
protected IResource[] getFolders() protected IResource[] getFolders()
{ {
if ( fFolders == null )
initializeFolders();
return fFolders; return fFolders;
} }