com.ctriposs.r2.util.URIUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.ctriposs.r2.util.URIUtil.java

Source

/**
 * Copyright (C) 2014 the original author or authors.
 * See the notice.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * 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.
 */
package com.ctriposs.r2.util;

import java.util.regex.Pattern;

import com.google.common.base.Preconditions;

public class URIUtil {
    private static final char PATH_SEP = '/';

    private static final Pattern PATH_SEP_PATTERN = Pattern.compile(Pattern.quote(String.valueOf(PATH_SEP)));

    /**
     * Returns an array of each path segment, using the definition as defined in RFC 2396.
     * If present, a leading path separator is removed.
     *
     * @param path the path to tokenize
     * @return an array of the path segments in the given path
     */
    public static String[] tokenizePath(String path) {
        Preconditions.checkNotNull(path, "uri");
        if (!path.isEmpty() && path.charAt(0) == PATH_SEP) {
            path = path.substring(1);
        }
        return PATH_SEP_PATTERN.split(path);
    }
}