/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.ide.visualstudio.tasks.internal;

import com.google.common.base.Joiner;
import org.gradle.api.Transformer;
import org.gradle.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class RelativeFileNameTransformer implements Transformer<String, File> {
    private final File rootDir;
    private final File currentDir;

    private RelativeFileNameTransformer(File rootDir, File currentDir) {
        this.rootDir = rootDir;
        this.currentDir = currentDir;
    }

    public static Transformer<String, File> forFile(File rootDir, File relativeFile) {
        return new RelativeFileNameTransformer(rootDir, relativeFile.getParentFile());
    }

    public static Transformer<String, File> forDirectory(File rootDir, File currentDirectory) {
        return new RelativeFileNameTransformer(rootDir, currentDirectory);
    }

    public String transform(File file) {
        String canonicalRoot;
        String canonicalFrom;
        String canonicalTo;
        try {
            canonicalRoot = rootDir.getCanonicalPath();
            canonicalFrom = currentDir.getCanonicalPath();
            canonicalTo = file.getCanonicalPath();
        } catch (IOException e) {
            return file.getAbsolutePath();
        }

        return findRelativePathInRoot(canonicalRoot, canonicalFrom, canonicalTo);
    }

    private String findRelativePathInRoot(String root, String from, String to) {
        if (!from.contains(root) || !to.contains(root)) {
            return to;
        }

        String relativePath = findRelativePath(from, to);
        return relativePath.length() == 0 ? "." : relativePath;
    }

    private String findRelativePath(String from, String to) {
        String fileSeparatorPattern = Pattern.quote(File.separator);
        List<String> fromPath = CollectionUtils.toList(from.split(fileSeparatorPattern));
        List<String> toPath = CollectionUtils.toList(to.split(fileSeparatorPattern));
        List<String> relativePath = new ArrayList<String>();

        while (!fromPath.isEmpty() && !toPath.isEmpty() && fromPath.get(0).equals(toPath.get(0))) {
            fromPath.remove(0);
            toPath.remove(0);
        }
        for (String ignored : fromPath) {
            relativePath.add("..");
        }
        for (String entry : toPath) {
            relativePath.add(entry);
        }
        return Joiner.on(File.separatorChar).join(relativePath);
    }
}
@@ -739,48 +739,8 @@
 mer;
-%0Aimport org.gradle.util.CollectionUtils;
 %0A%0Aim
@@ -831,16 +831,22 @@
 va.util.
+Linked
 List;%0Aim
@@ -864,21 +864,12 @@
 til.
-regex.Pattern
+List
 ;%0A%0Ap
@@ -2386,242 +2386,91 @@
     
-String fileSeparatorPattern = Pattern.quote(File.separator);%0A        List%3CString%3E fromPath = CollectionUtils.toList(from.split(fileSeparatorPattern));%0A        List%3CString%3E toPath = CollectionUtils.toList(to.split(fileSeparatorPattern)
+List%3CString%3E fromPath = splitPath(from);%0A        List%3CString%3E toPath = splitPath(to
 );%0A 
@@ -2945,10 +2945,324 @@
 );%0A    %7D
+%0A%0A    private List%3CString%3E splitPath(String path) %7B%0A        File pathFile = new File(path);%0A        List%3CString%3E split = new LinkedList%3CString%3E();%0A        while (pathFile != null) %7B%0A            split.add(0, pathFile.getName());%0A            pathFile = pathFile.getParentFile();%0A        %7D%0A        return split;%0A    %7D
 %0A%7D

Use File.getParentFile() instead of String.split() to split a file name into components.