Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2000-2013 Enonic AS
 * http://www.enonic.com/license
 */

import java.net.URI;
import java.net.URLDecoder;

public class Main {
    public static String resolveRelativePath(final String href, final String base) {
        if (href.startsWith("/")) {
            return href;
        }

        return removeExtraSlashes(resolveBasePath(base) + "/" + href);
    }

    private static String removeExtraSlashes(final String str) {
        if (str.contains("//")) {
            return removeExtraSlashes(str.replace("//", "/"));
        } else {
            return str;
        }
    }

    private static String resolveBasePath(final String path) {
        final String name = resolvePath(path);
        return removeExtraSlashes(name.substring(0, name.lastIndexOf('/')));
    }

    public static String resolvePath(final String path) {
        try {
            final URI uri = new URI(path);
            return removeExtraSlashes(URLDecoder.decode(uri.getPath(), "UTF-8"));
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}