PR: MNG-2006

Replaced appendPath with a more robust implementation,
after discussing it with jdcasey.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@382881 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kenney Westerhof 2006-03-03 16:22:44 +00:00
parent 4c85dfc331
commit f3a9e38340
1 changed files with 59 additions and 81 deletions

View File

@ -32,6 +32,7 @@ import org.codehaus.plexus.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -485,98 +486,75 @@ public class DefaultModelInheritanceAssembler
// TODO: This should eventually be migrated to DefaultPathTranslator. // TODO: This should eventually be migrated to DefaultPathTranslator.
protected String appendPath( String parentPath, String childPath, String pathAdjustment, boolean appendPaths ) protected String appendPath( String parentPath, String childPath, String pathAdjustment, boolean appendPaths )
{ {
List pathFragments = new ArrayList(); String uncleanPath = parentPath;
String rootPath = parentPath;
String protocol = null;
int protocolIdx = rootPath.indexOf( "://" );
if ( protocolIdx > -1 )
{
protocol = rootPath.substring( 0, protocolIdx + 3 );
rootPath = rootPath.substring( protocolIdx + 3 );
}
pathFragments.add( rootPath );
if ( appendPaths ) if ( appendPaths )
{ {
if ( pathAdjustment != null ) if ( pathAdjustment != null )
{ uncleanPath += "/" + pathAdjustment;
pathFragments.add( pathAdjustment );
}
if ( childPath != null ) if ( childPath != null )
uncleanPath += "/" + childPath;
}
String cleanedPath = "";
int protocolIdx = uncleanPath.indexOf( "://" );
if ( protocolIdx > -1 )
{
cleanedPath = uncleanPath.substring( 0, protocolIdx + 3 );
uncleanPath = uncleanPath.substring( protocolIdx + 3 );
}
if ( uncleanPath.startsWith( "/" ) )
cleanedPath += "/";
return cleanedPath + resolvePath( uncleanPath );
}
// TODO: Move this to plexus-utils' PathTool.
private static String resolvePath( String uncleanPath )
{
LinkedList pathElements = new LinkedList();
StringTokenizer tokenizer = new StringTokenizer( uncleanPath, "/" );
while ( tokenizer.hasMoreTokens() )
{
String token = (String) tokenizer.nextToken();
if ( token.equals( "" ) )
{ {
pathFragments.add( childPath ); // Empty path entry ("...//.."), remove.
}
else if ( token.equals( ".." ) )
{
if ( pathElements.isEmpty() )
{
// FIXME: somehow report to the user
// that there are too many '..' elements.
// For now, ignore the extra '..'.
}
else
{
pathElements.removeLast();
}
}
else
{
pathElements.addLast( token );
} }
} }
StringBuffer cleanedPath = new StringBuffer(); StringBuffer cleanedPath = new StringBuffer();
if ( protocol != null ) while ( !pathElements.isEmpty() )
{ {
cleanedPath.append( protocol ); cleanedPath.append( pathElements.removeFirst() );
} if ( !pathElements.isEmpty() )
cleanedPath.append( '/' );
if ( rootPath.startsWith( "/" ) )
{
cleanedPath.append( '/' );
}
String lastToken = null;
String currentToken = null;
for ( Iterator it = pathFragments.iterator(); it.hasNext(); )
{
String pathFragment = (String) it.next();
StringTokenizer tokens = new StringTokenizer( pathFragment, "/" );
while ( tokens.hasMoreTokens() )
{
lastToken = currentToken;
currentToken = tokens.nextToken();
if ( "..".equals( currentToken ) && lastToken != null )
{
int cleanedPathLen = cleanedPath.length();
int lastTokenLen = lastToken.length();
if ( cleanedPathLen > lastTokenLen )
{
// trim the previous path part off...
cleanedPath.setLength( cleanedPath.length() - ( lastToken.length() + 1 ) );
}
}
else if ( !".".equals( currentToken ) )
{
// don't worry about /./ self-references.
cleanedPath.append( currentToken ).append( '/' );
}
}
}
String lastPathPart = childPath;
if ( lastPathPart == null )
{
lastPathPart = pathAdjustment;
}
if ( lastPathPart == null )
{
lastPathPart = parentPath;
}
if ( appendPaths && lastPathPart != null && !lastPathPart.endsWith( "/" ) )
{
int cleanedPathLen = cleanedPath.length();
if ( cleanedPathLen > 0 )
{
cleanedPath.setLength( cleanedPathLen - 1 );
}
} }
return cleanedPath.toString(); return cleanedPath.toString();