Java Path Relative nio checkNormalizedRelative(String path)

Here you can find the source of checkNormalizedRelative(String path)

Description

Checks that the given path is relative and does not contain any .

License

Apache License

Declaration

public static String checkNormalizedRelative(String path) 

Method Source Code

//package com.java2s;
/*/*from w ww . j a v  a2  s . co m*/
 * Copyright (C) 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 com.google.common.base.Preconditions;

import java.nio.file.Path;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern RELATIVISM = Pattern.compile("(.*/)?[.][.]?(/.*)?");

    /**
     * Checks that the given path is relative and does not contain any {@code .} or {@code ..}
     * components.
     *
     * @returns the {@code path} passed
     */
    public static String checkNormalizedRelative(String path) {
        Preconditions.checkArgument(!RELATIVISM.matcher(path).matches(),
                "path has unexpected . or .. components: %s", path);
        Preconditions.checkArgument(!path.startsWith("/"), "path must be relative, but it starts with /: %s", path);
        return path;
    }

    /**
     * Checks that the given path is relative and does not contain any {@code .} or {@code ..}
     * components.
     *
     * @returns the {@code path} passed
     */
    public static Path checkNormalizedRelative(Path path) {
        checkNormalizedRelative(path.toString());
        return path;
    }
}

Related

  1. combinePaths(String baseDir, String relativePath)
  2. get(File basePath, String... relatives)
  3. getChildEntryRelativePath(Path base, Path child, boolean convertToLinuxPath)
  4. getFileFromBaseFileAndPathThatMayBeRelative(File baseDir, String pathThatMayBeRelative)