Returns the relative path that expresses the location of path seen as relative to origin - Java File Path IO

Java examples for File Path IO:Path

Description

Returns the relative path that expresses the location of path seen as relative to origin

Demo Code

/*//from   w  w w. ja v  a 2s.c  o m
 * Copyright (c) 2008 Dennis Thrys?e
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
//package com.java2s;

import java.util.StringTokenizer;

public class Main {
    /**
     * Returns the relative path that expresses the location of <code>path</code> seen as relative to <code>origin</code>
     * @param origin Where to make relative to
     * @param path The full path
     * @return The relative path from <code>origin</code> to <code>path</code>
     */
    public static String makeRelative(String origin, String path) {
        boolean trailingSlash = path.endsWith("/");
        StringBuffer sb = new StringBuffer(40);
        StringTokenizer originTokenizer = new StringTokenizer(origin, "/",
                false);
        StringTokenizer pathTokenizer = new StringTokenizer(path, "/",
                false);

        // Walk through the parts that are in common
        String firstTokenNotInCommon = null;
        while (originTokenizer.hasMoreTokens()
                && pathTokenizer.hasMoreTokens()
                && firstTokenNotInCommon == null) {
            firstTokenNotInCommon = pathTokenizer.nextToken();
            if (originTokenizer.nextToken().equals(firstTokenNotInCommon))
                firstTokenNotInCommon = null;
        }

        // Find number of elements that aren't in common, and print a '..' for each
        int stepsBack = originTokenizer.countTokens();
        if (firstTokenNotInCommon != null)
            stepsBack++;
        for (int i = 0; i < stepsBack; i++) {
            sb.append("..");
            if (trailingSlash || pathTokenizer.hasMoreTokens()
                    || firstTokenNotInCommon != null || i < stepsBack - 1)
                sb.append('/');
        }

        // Print the rest of path
        if (firstTokenNotInCommon != null) {
            sb.append(firstTokenNotInCommon);
            if (trailingSlash || pathTokenizer.hasMoreTokens())
                sb.append('/');
        }
        while (pathTokenizer.hasMoreTokens()) {
            String part = pathTokenizer.nextToken();
            sb.append(part);
            if (trailingSlash || pathTokenizer.hasMoreTokens())
                sb.append('/');
        }
        if (sb.length() == 0)
            return "";
        else
            return sb.toString();
    }
}

Related Tutorials