Index: src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java (revision 671445) +++ src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java (working copy) @@ -47,7 +47,6 @@ import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; - import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -58,6 +57,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringTokenizer; /** * @author Edwin Punzalan @@ -211,19 +211,95 @@ */ protected String toRelative( File basedir, String absolutePath ) { + getLog().debug( "translating to relative path: basedir=" + basedir + ", absolutePath=" + absolutePath ); String relative; + String convertedAbsolutePath = absolutePath.replaceAll( "\\\\", "/" ); + String convertedBasedirAbsolutePath = basedir.getAbsolutePath().replaceAll( "\\\\", "/" ); - if ( absolutePath.startsWith( basedir.getAbsolutePath() ) - && absolutePath.length() > basedir.getAbsolutePath().length() ) + convertedAbsolutePath = StringUtils.replace( convertedAbsolutePath, "\\", "/" ); + convertedBasedirAbsolutePath = StringUtils.replace( convertedBasedirAbsolutePath, "\\", "/" ); + + StringTokenizer baseTokens = new StringTokenizer( convertedBasedirAbsolutePath, "/", false ); + + int baseCount = baseTokens.countTokens(); + List baseTokenList = new ArrayList( baseCount ); + while ( baseTokens.hasMoreTokens() ) { - relative = absolutePath.substring( basedir.getAbsolutePath().length() + 1 ); + baseTokenList.add( baseTokens.nextToken() ); } + + StringTokenizer pathTokens = new StringTokenizer( convertedAbsolutePath, "/", false ); + + int pathCount = pathTokens.countTokens(); + List pathTokenList = new ArrayList( pathCount ); + while ( pathTokens.hasMoreTokens() ) + { + pathTokenList.add( pathTokens.nextToken() ); + } + + int maxCount = Math.max( baseTokenList.size(), pathTokenList.size() ); + int differIndex = -1; + for ( int i = 0; i < maxCount; i++ ) + { + if ( i >= pathTokenList.size() || i >= baseTokenList.size() ) + { + differIndex = i; + break; + } + String basePart = (String) baseTokenList.get( i ); + String pathPart = (String) pathTokenList.get( i ); + if ( !basePart.equals( pathPart ) ) + { + differIndex = i; + break; + } + } + if ( differIndex < 1 ) + { + // paths are either equal or completely different + relative = convertedAbsolutePath; + } else { - relative = absolutePath; + StringBuffer result = new StringBuffer(); + int parentCount = baseTokenList.size() - differIndex; + /* + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "parentCount="+parentCount); + } + */ + boolean isFirst = true; + for ( int i = 0; i < parentCount; i++ ) + { + // add parents + if ( isFirst ) + { + isFirst = false; + } + else + { + result.append( "/" ); + } + result.append( ".." ); + } + for ( int i = differIndex; i < pathTokenList.size(); i++ ) + { + // add the remaining path elements + if ( isFirst ) + { + isFirst = false; + } + else + { + result.append( "/" ); + } + result.append( pathTokenList.get( i ) ); + } + relative = result.toString(); } - relative = StringUtils.replace( relative, "\\", "/" ); + getLog().debug( "translated to " + relative ); return relative; }