Java Path Normalize normalizePath(final String... path)

Here you can find the source of normalizePath(final String... path)

Description

normalize Path

License

Open Source License

Declaration

public static String normalizePath(final String... path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc../*w  w  w  . j a  v a 2s .  c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

public class Main {
    public static String normalizePath(final String... path) {
        if (path == null || path.length < 1) {
            return "/";
        }

        final StringBuilder sb = new StringBuilder();
        int idx = 0;
        parts: for (String part : path) {
            if (part == null || part.length() < 1 || "/".equals(part)) {
                continue parts;
            }

            if (idx == 0 && part.startsWith("file:")) {
                if (part.length() > 5) {
                    sb.append(part.substring(5));
                }

                continue parts;
            }

            if (idx > 0) {
                while (part.charAt(0) == '/') {
                    if (part.length() < 2) {
                        continue parts;
                    }

                    part = part.substring(1);
                }
            }

            while (part.charAt(part.length() - 1) == '/') {
                if (part.length() < 2) {
                    continue parts;
                }

                part = part.substring(0, part.length() - 1);
            }

            if (sb.length() > 0) {
                sb.append('/');
            }

            sb.append(part);
            idx++;
        }

        if (path[path.length - 1].endsWith("/")) {
            sb.append("/");
        }

        return sb.toString();
    }
}

Related

  1. normalizePath(final String in)
  2. normalizePath(final String path)
  3. normalizePath(final String path)
  4. normalizePath(final String path)
  5. normalizePath(final String path)
  6. normalizePath(String _path, boolean _appendFinalSeparator)
  7. normalizePath(String absPath)
  8. normalizePath(String filepath)
  9. normalizePath(String fileSystemPath)