All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sft.report.RelativeHtmlPathResolver Maven / Gradle / Ivy

There is a newer version: 1.9
Show newest version
/*******************************************************************************
 * Copyright (c) 2014 Sylvain Lézier.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Sylvain Lézier - initial implementation
 *******************************************************************************/
package sft.report;

import java.util.LinkedList;
import java.util.List;

import static java.util.Arrays.asList;

public class RelativeHtmlPathResolver {
    public String getRelativePathToFile(String callerPath, String targetPath) {

        LinkedList callerPathElements = getLinkedListOfPathElements(callerPath);
        LinkedList targetPathElements = getLinkedListOfPathElements(targetPath);

        List relativePathToFile = getRelativePathToFile(callerPathElements, targetPathElements);

        return generateRelativePathString(relativePathToFile);
    }

    private LinkedList getLinkedListOfPathElements(String path) {
        return new LinkedList(asList(path.split("/")));
    }

    private List getRelativePathToFile(LinkedList pathOfSourceFile, LinkedList pathOfDestinationFile) {
        if (pathOfSourceFile.size() == 1) {
            return pathOfDestinationFile;
        } else if (pathOfSourceFile.get(0).equals(pathOfDestinationFile.get(0))) {
            pathOfSourceFile.remove(0);
            pathOfDestinationFile.remove(0);
            return getRelativePathToFile(pathOfSourceFile, pathOfDestinationFile);
        } else {
            LinkedList toCommonPath = new LinkedList();
            for (int i = 0; i < pathOfSourceFile.size() - 1; i++) {
                toCommonPath.add("..");
            }
            toCommonPath.addAll(pathOfDestinationFile);
            return toCommonPath;
        }
    }

    private String generateRelativePathString(List relativePathToFile) {
        String result = "";
        for (String element : relativePathToFile) {
            if (!result.isEmpty()) {
                result += "/";
            }
            result += element;
        }
        return result;
    }

    public String getPathOf(Class aClass, String extension) {
        return aClass.getCanonicalName().replaceAll("\\.", "/") + extension;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy