Java Path Relative nio resolvePathIfRelativeTo(File referenceFile, String partialPath)

Here you can find the source of resolvePathIfRelativeTo(File referenceFile, String partialPath)

Description

resolve Path If Relative To

License

Open Source License

Parameter

Parameter Description
referenceFile a parameter
partialPath a parameter

Return

If partialPath is not relative, then returns partialPath. If partialPath is relative, then returns partialPath nested inside folder of referenceFile.

Declaration

public static String resolvePathIfRelativeTo(File referenceFile, String partialPath) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2013, 2014 ETAS GmbH.//from  w  w  w  .jav a  2 s. c o m
* 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:
* Dennis Eder (ETAS GmbH) - initial API and implementation and/or initial documentation
*******************************************************************************/

import java.io.File;

import java.nio.file.Paths;

public class Main {
    /**
     * @param referenceFile
     * @param partialPath
     * @return If partialPath is not relative, then returns partialPath. 
     * If partialPath is relative, then returns partialPath nested inside folder of referenceFile.
     */
    public static String resolvePathIfRelativeTo(File referenceFile, String partialPath) {
        if (partialPath.startsWith("..") || partialPath.startsWith(".")) {
            String definitionOriginDir = referenceFile.getParentFile().getAbsolutePath();
            partialPath = Paths.get(definitionOriginDir, partialPath).toAbsolutePath().toString();
        }
        return partialPath;
    }
}

Related

  1. relativizeAndNormalizePath(final String baseDirectory, final String path)
  2. relativizePath(IPath path, IPath basePath)
  3. relativizePath(IPath path, IPath basePath, boolean strict)
  4. relativizePath(Path root, Path child)
  5. relativizePath(String basePath, File toRelativize)
  6. resolveRelative(Path p, String other)
  7. resolveRelativePath(String first, String... more)
  8. resolveRelativeRemotePath(Path root, Path file)
  9. toFileWithAbsolutePath(String relativePath)