Java Path Combine combineDisplayPaths(String parent_display, String display_path)

Here you can find the source of combineDisplayPaths(String parent_display, String display_path)

Description

Combine display paths

License

Open Source License

Parameter

Parameter Description
parent_display Path to a 'parent' file, may be <code>null</code>
display_path Display file. If relative, it is resolved relative to the parent display

Return

Combined path

Declaration

public static String combineDisplayPaths(String parent_display, String display_path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015-2016 Oak Ridge National Laboratory.
 * 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
 *******************************************************************************/

public class Main {
    /** Combine display paths
     *  @param parent_display Path to a 'parent' file, may be <code>null</code>
     *  @param display_path Display file. If relative, it is resolved relative to the parent display
     *  @return Combined path/*  w w w.  java  2s  .  c  om*/
     */
    public static String combineDisplayPaths(String parent_display, String display_path) {
        // Anything in the parent?
        if (parent_display == null || parent_display.isEmpty())
            return display_path;

        display_path = normalize(display_path);

        // Is display already absolute?
        if (isAbsolute(display_path))
            return display_path;

        parent_display = normalize(parent_display);

        // Remove last segment from parent_display to get path
        String result = getDirectory(parent_display) + "/" + display_path;
        result = normalize(result);

        return result;
    }

    /** Normalize path
     *
     *  <p>Patch windows-type path with '\' into
     *  forward slashes,
     *  and collapse ".." up references.
     *
     *  @param path Path that may use Windows '\' or ".."
     *  @return Path with only '/' and up-references resolved
     */
    public static String normalize(String path) {
        // Pattern: '\(?!\)', i.e. backslash _not_ followed by another one.
        // Each \ is doubled as \\ to get one '\' into the string,
        // then doubled once more to tell regex that we want a '\'
        path = path.replaceAll("\\\\(?!\\\\)", "/");
        // Collapse "something/../" into "something/"
        int up = path.indexOf("/../");
        while (up >= 0) {
            final int prev = path.lastIndexOf('/', up - 1);
            if (prev >= 0)
                path = path.substring(0, prev) + path.substring(up + 3);
            else
                break;
            up = path.indexOf("/../");
        }
        return path;
    }

    private static boolean isAbsolute(final String path) {
        return path.startsWith("/") || isURL(path);
    }

    /** Obtain directory of file. For URL, this is the path up to the last element
     *
     *  <p>For a <code>null</code> path, the location will also be <code>null</code>.
     *
     *  @param path Complete path, i.e. "/some/location/resource"
     *  @return Location, i.e. "/some/location" without trailing "/", or "."
     */
    public static String getDirectory(String path) {
        if (path == null)
            return null;
        // Remove last segment from parent_display to get path
        path = normalize(path);
        int sep = path.lastIndexOf('/');
        if (sep >= 0)
            return path.substring(0, sep);
        return ".";
    }

    private static boolean isURL(final String path) {
        return path.startsWith("http://") || path.startsWith("https://");
    }
}

Related

  1. combine(final String path, final String suffix)
  2. combine(final String path1, final String path2)
  3. combine(String directory, String filename)
  4. combinePath(java.io.File parentDir, String... childDirs)
  5. combinePath(String basePath, String withPath)
  6. combinePath(String parent, String child)
  7. combinePath(String parent, String name)