com.helpinput.core.PathUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.helpinput.core.PathUtil.java

Source

/*
 * Copyright 2013-2015 the original author or authors.
 *
 * 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.
 */

/**
 *@Author: niaoge(Zhengsheng Xia)
 *@Email 78493244@qq.com
 *@Date: 2015-6-10
 */
package com.helpinput.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import static com.helpinput.core.LoggerBase.logger;

/**
 *  ??: PathUtils ?? : ?  : Vv   2012-11-29  
 *  ?: ()   
 */
public class PathUtil {
    public static final String PATH_OLD_SPLIT = "\\\\";
    public static final String PATH_OTHER_SPLIT = "//";
    public static final String PATH_SPLIT = "/";
    public static final String UPPER_PATH = "..";
    public static final String PATH_HTML = "http://";

    /**
     * 
     * ??   Vv  ?: ()   
     * 
     * @param parentPath
     *             D:/AA
     * @param subPath
     *            ? BB
     * @return ??  D:/AA/BB
     */
    public static String appendPath(String parentPath, String subPath) {

        if (StringUtils.isNotBlank(parentPath) && !parentPath.endsWith(PATH_SPLIT)) {
            parentPath += PATH_SPLIT;
        }
        if (StringUtils.isNotBlank(subPath)) {
            parentPath = parentPath + subPath;
        }

        return processPath(parentPath);
    }

    /**
     * 
     * ?? ?????  1.??
     * 2.?currentPath??targetPath??i?
     * 2a.????continue????
     * 2b.?? 3.?2??????String
     *  Vv  ?: ()   
     * 
     * @param currentPath
     * @param targetPath
     * @return ?
     * @throws ProcessPathException
     */
    public static String getRelativePath(String currentPath, String targetPath) {
        if (StringUtils.isBlank(currentPath) || StringUtils.isBlank(targetPath)) {
            logger.error("The currentPath or targetPath parameter is required and can't be null or blank.");
            return currentPath;
        }
        currentPath = processPath(currentPath);
        targetPath = processPath(targetPath);

        String[] cpNames = currentPath.split(PATH_SPLIT);
        String[] tpNames = targetPath.split(PATH_SPLIT);

        List<String> rpNames = new ArrayList<String>();

        /**
         * 2.?currentPath??targetPath??i?
         * 2a.????continue????
         * 2b.??
         */
        int i = 0;
        for (i = 0; i < cpNames.length; i++) {
            if (i > tpNames.length - 1)
                break;

            if (cpNames[i].equals(tpNames[i])) {
                continue;
            } else if (i == 0) {// currentPathtargetPath???
                break;
            } else {
                rpNames.add(UPPER_PATH);
                break;
            }
        }

        /**
         * 3.?2? "i"?????String
         */
        for (int j = i; j < tpNames.length; j++) {
            rpNames.add(tpNames[j]);
        }

        String relativePath = "";
        for (String rp : rpNames) {
            relativePath = relativePath + rp + PATH_SPLIT;
        }

        return processPath(relativePath);
    }

    /**
     * ?? ?  ///localhost//uuzz/admin/main.action; 
     * /localhost/uuzz/admin/main.action  Vv  ?: ()   
     * 
     * @param path
     * @return
     */
    public static String processPath(String path) {
        path = path.replaceAll(PATH_OLD_SPLIT, PATH_SPLIT);
        if (path.endsWith(PATH_SPLIT)) {
            path = path.substring(0, path.length() - 1);
        }

        boolean isHttp = false;
        if (path.startsWith(PATH_HTML)) {
            isHttp = true;
            path = path.substring(PATH_HTML.length());
        }

        // ?"//"?"/"
        while (path.contains(PATH_OTHER_SPLIT)) {
            path = path.replaceAll(PATH_OTHER_SPLIT, PATH_SPLIT);
        }

        if (isHttp) {
            path = PATH_HTML + path;
        }

        return path;
    }
}