Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * 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:
 *   IBM - Initial API and implementation
 *******************************************************************************/

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class Main {
    /**
     * Create URL using base URI.
     * 
     * @param url a URL string.
     * @param baseURI a base url string to assist in creating a URL.
     * @return newly created URL string.
     * @throws MalformedURLException if a malformed URL has occurred.
     */
    public static String createURLString(String url, String baseURI) throws MalformedURLException {
        return createURL(url, baseURI).toExternalForm();
    }

    /**
     * Create URL using base URI.
     * 
     * @param url a URL string.
     * @param baseURI a base url string to assist in creating a URL.
     * @return newly created URL.
     * @throws MalformedURLException if a malformed URL has occurred.
     */
    public static URL createURL(String url, String baseURI) throws MalformedURLException {
        URL returnURL = null;
        URI uri = null;
        try {
            returnURL = new URL(url);
            uri = new URI(url);
            uri = uri.normalize();
            returnURL = new URL(uri.toString());
        }

        catch (Exception mue) {
            int i = baseURI.lastIndexOf('/');
            int j = baseURI.lastIndexOf('\\');
            if (j > i)
                i = j;
            try {
                uri = new URI(baseURI.substring(0, i + 1) + url);
                uri = uri.normalize();
                returnURL = uri.toURL();
            } catch (Exception e) {
                return new URL(baseURI.substring(0, i + 1) + url);
            }
        }
        return returnURL;
    }
}