Java Relative Path Get relativizePathSegments(String[] srcSegments, String[] destSegments)

Here you can find the source of relativizePathSegments(String[] srcSegments, String[] destSegments)

Description

Computes a string containing the relative path from the directory or file represented by the source segments to the directory or file represented by the destination segments.

License

Open Source License

Parameter

Parameter Description
srcSegments the segments of the source path
destSegments the segments of the destination path

Declaration

private static String relativizePathSegments(String[] srcSegments, String[] destSegments) 

Method Source Code


//package com.java2s;
/*==========================================================================*\
 |  $Id: PathUtils.java,v 1.2 2009/10/26 14:35:54 aallowat Exp $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2009 Virginia Tech/*from   w  w  w .  jav a  2s  .  c o m*/
 |
 |  This file is part of the Web-CAT CxxTest Distribution.
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU Affero General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT 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 Affero General Public License
 |  along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

import java.io.File;

public class Main {
    /**
     * Computes a string containing the relative path from the directory or
     * file represented by the source segments to the directory or file
     * represented by the destination segments.
     * 
     * @param srcSegments the segments of the source path
     * @param destSegments the segments of the destination path
     */
    private static String relativizePathSegments(String[] srcSegments, String[] destSegments) {
        int srcIndex = srcSegments.length - 1;
        int destIndex = destSegments.length - 1;

        StringBuffer relativePath = new StringBuffer();

        // Skip past the parts of the paths that both have in common. String
        // equality suffices here since the paths are canonicalized before
        // being passed into this function.

        while (srcIndex >= 0 && destIndex >= 0 && srcSegments[srcIndex].equals(destSegments[destIndex])) {
            srcIndex--;
            destIndex--;
        }

        // For each remaining segment in the source path, add a reference to
        // the parent directory.

        for (; srcIndex >= 0; srcIndex--) {
            relativePath.append("..");
            relativePath.append(File.separator);
        }

        // Then, now that we're at the location where the remainder of the
        // destination path starts, append those segments to the end of the
        // path.

        for (; destIndex > 0; destIndex--) {
            relativePath.append(destSegments[destIndex]);
            relativePath.append(File.separator);
        }

        // Finally, append the filename, if there is one.

        if (destIndex == 0) {
            relativePath.append(destSegments[destIndex]);
        }

        // Remove a final trailing slash if it is there.

        if (relativePath.charAt(relativePath.length() - 1) == File.separatorChar) {
            relativePath.deleteCharAt(relativePath.length() - 1);
        }

        return relativePath.toString();
    }
}

Related

  1. relativize(String path)
  2. relativize(String path, String base)
  3. relativizePath(File from, File to)
  4. relativizePath(final File path, final File base)
  5. relativizePath(String parent, String child)