Java Path Relative nio relativizePath(IPath path, IPath basePath)

Here you can find the source of relativizePath(IPath path, IPath basePath)

Description

Returns the relative path of the original path with respect to the basePath if they share the same prefix path, otherwise path is returned unchanged.

License

Apache License

Declaration

public static IPath relativizePath(IPath path, IPath basePath) 

Method Source Code


//package com.java2s;
/*/*from w  w w.  j av  a 2s .  c  o  m*/
 * Copyright 2016 Google Inc.
 *
 * 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.
 */

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class Main {
    /**
     * Returns the relative path of the original <code>path</code> with respect to the <code>basePath</code> if they share
     * the same prefix path, otherwise <code>path</code> is returned unchanged. If <code>path</code> is <code>null</code>,
     * <code>basePath</code> is returned. If <code>basePath</code> is <code>null</code>, <code>path</code> is returned.
     */
    public static IPath relativizePath(IPath path, IPath basePath) {
        if (path == null) {
            return basePath;
        }
        if (basePath == null) {
            return path;
        }
        java.nio.file.Path base = basePath.toFile().toPath().toAbsolutePath();
        java.nio.file.Path child = path.toFile().toPath().toAbsolutePath();
        if (child.startsWith(base)) {
            return new Path(base.relativize(child).toString());
        } else {
            return path;
        }
    }
}

Related

  1. relativePath(String input, String mainurl)
  2. relativePathTo(Path path, Path basePath)
  3. relativize(Path absoluteDirectory, Path subDirectory)
  4. relativize(Path target, Collection paths)
  5. relativizeAndNormalizePath(final String baseDirectory, final String path)
  6. relativizePath(IPath path, IPath basePath, boolean strict)
  7. relativizePath(Path root, Path child)
  8. relativizePath(String basePath, File toRelativize)
  9. resolvePathIfRelativeTo(File referenceFile, String partialPath)