Java Path Normalize normalize(final boolean absolutize, final String... path)

Here you can find the source of normalize(final boolean absolutize, final String... path)

Description

normalize

License

Apache License

Declaration

public static String normalize(final boolean absolutize, final String... path) 

Method Source Code

//package com.java2s;
/*//  w  w  w.j av a 2  s.c o m
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * 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.
 */

public class Main {
    public static String normalize(final boolean absolutize, final String... path) {
        if (path == null || path.length < 1) {
            return null;
        }

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

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

                continue;
            }

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

                    part = part.substring(1);
                }
            }

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

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

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

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

        return deRelativize(absolutize, sb.toString());
    }

    /**
     * Normalize a relative URI path that may have relative values ("/./",
     * "/../", and so on ) it it. <strong>WARNING</strong> - This method is
     * useful only for normalizing application-generated paths. It does not try
     * to perform security checks for malicious input.
     * @param absolutize 
     * 
     * @param path
     *      Relative path to be normalized
     */
    public static String deRelativize(final boolean absolutize, final String path) {

        if (path == null) {
            return null;
        }

        // Create a place for the normalized path
        String result = path;

        if (result.equals("/.")) {
            return "/";
        }

        // Add a leading "/" if necessary
        if (absolutize && !result.startsWith("/")) {
            result = "/" + result;
        }

        // Resolve occurrences of "//" in the normalized path
        while (true) {
            final int index = result.indexOf("//");
            if (index < 0) {
                break;
            }
            result = result.substring(0, index) + result.substring(index + 1);
        }

        // Resolve occurrences of "/./" in the normalized path
        while (true) {
            final int index = result.indexOf("/./");
            if (index < 0) {
                break;
            }
            result = result.substring(0, index) + result.substring(index + 2);
        }

        // Resolve occurrences of "/../" in the normalized path
        while (true) {
            final int index = result.indexOf("/../");
            if (index < 0) {
                break;
            }
            if (index == 0) {
                return (null); // Trying to go outside our context
            }
            final int index2 = result.lastIndexOf('/', index - 1);
            result = result.substring(0, index2) + result.substring(index + 3);
        }

        // Return the normalized path that we have completed
        return result;

    }
}

Related

  1. normalise(String path)
  2. normalisePath(final String path)
  3. normalisePath(String path)
  4. normalisePath(String path)
  5. normalize(final String path)
  6. normalize(final String path)
  7. normalize(final String path)
  8. normalize(final String pathname)