All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.vault.util.PathUtil Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.jackrabbit.vault.util;

import java.io.File;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

/**
 * {@code PathUtil}...
 *
 */
public class PathUtil {

    /**
     * make a canonical path. removes all /./ and /../ and multiple slashes.
     * @param parent the parent path (can be {@code null})
     * @param path the path to resolve
     * @return the canonicalized path
     */
    public static String[] makePath(String[] parent, String path) {
        if (path == null || path.equals("") || path.equals(".")) {
            return parent;
        }
        // compose parent and path
        boolean isAbsolute = false;
        String[] composed = Text.explode(path, '/');
        if (path.charAt(0) == '/') {
            isAbsolute = true;
        } else {
            if (parent != null && parent.length > 0) {
                int off = 0;
                if (parent[0].equals("/")) {
                    isAbsolute = true;
                    off = 1;
                }
                String[] c = new String[parent.length - off + composed.length];
                System.arraycopy(parent, off, c, 0, parent.length - off);
                System.arraycopy(composed, 0, c, parent.length - off, composed.length);
                composed = c;
            }
        }
        // canonicalize
        int dst = 0;
        boolean startsWithParent = false;
        for (int i=0; i 0 && ret.charAt(ret.length()-1) != '/') {
            ret.append('/');
        }
        ret.append(relPath);
        return ret.toString();
    }
    
    public static int getDepth(String path) {
        // assume valid absolute path
        int len = path.length();
        if (len <=1) {
            return 0;
        }
        int depth = 1;
        for (int i=1; i 0) {
            if (path.endsWith("/")) {
                path += relPath;
            } else {
                path += "/" + relPath;
            }
        }
        return path;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy