Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    public static String getAbsoluteUri(String parent, String child) {
        if (parent == null || child == null) {
            return null;
        }
        if (isAbsolute(child)) {
            return child;
        }
        String[] parentPaths = parent.split("/");
        String[] childPaths = child.split("/");
        int start = parent.endsWith("/") ? 0 : 1;
        int upLevel = start;
        for (String childPath : childPaths) {
            String path = childPath.trim();
            if ("..".equals(path)) {
                upLevel++;
            } else {
                break;
            }
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append('/');
        for (int i = 0; i < parentPaths.length - upLevel; i++) {
            String path = parentPaths[i];
            if (!"".equals(path)) {
                buffer.append(path);
                buffer.append('/');
            }
        }
        for (int i = upLevel - start; i < childPaths.length; i++) {
            buffer.append(childPaths[i]);
            if (i != childPaths.length - 1) {
                buffer.append('/');
            }
        }
        return buffer.toString();
    }

    public static boolean isAbsolute(String uri) {
        if (uri == null) {
            return false;
        }
        return uri.startsWith("/");
    }
}