Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright (C) 2011 [Gobierno de Espana]
 * This file is part of "Cliente @Firma".
 * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
 *   - the GNU General Public License as published by the Free Software Foundation;
 *     either version 2 of the License, or (at your option) any later version.
 *   - or The European Software License; either version 1.1 or (at your option) any later version.
 * Date: 11/01/11
 * You may contact the copyright holder at: soporte.afirma5@mpt.es
 */

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    private static final String[] SUPPORTED_URI_SCHEMES = new String[] { "http", "https", "file", "urn" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    };

    /** Crea una URI a partir de un nombre de fichero local o una URL.
     * @param file Nombre del fichero local o URL
     * @return URI (<code>file://</code>) del fichero local o URL
     * @throws URISyntaxException Si no se puede crear una URI soportada a partir de la cadena de entrada */
    public static URI createURI(final String file) throws URISyntaxException {

        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("No se puede crear una URI a partir de un nulo"); //$NON-NLS-1$
        }

        String filename = file.trim();

        if (filename.isEmpty()) {
            throw new IllegalArgumentException("La URI no puede ser una cadena vacia"); //$NON-NLS-1$
        }

        // Cambiamos los caracteres Windows
        filename = filename.replace('\\', '/');

        // Realizamos los cambios necesarios para proteger los caracteres no
        // seguros
        // de la URL
        filename = filename.replace(" ", "%20") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("<", "%3C") //$NON-NLS-1$ //$NON-NLS-2$
                .replace(">", "%3E") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("\"", "%22") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("{", "%7B") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("}", "%7D") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("|", "%7C") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("^", "%5E") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("[", "%5B") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("]", "%5D") //$NON-NLS-1$ //$NON-NLS-2$
                .replace("`", "%60"); //$NON-NLS-1$ //$NON-NLS-2$

        final URI uri = new URI(filename);

        // Comprobamos si es un esquema soportado
        final String scheme = uri.getScheme();
        for (final String element : SUPPORTED_URI_SCHEMES) {
            if (element.equals(scheme)) {
                return uri;
            }
        }

        // Si el esquema es nulo, aun puede ser un nombre de fichero valido
        // El caracter '#' debe protegerse en rutas locales
        if (scheme == null) {
            filename = filename.replace("#", "%23"); //$NON-NLS-1$ //$NON-NLS-2$
            return createURI("file://" + filename); //$NON-NLS-1$
        }

        // Miramos si el esquema es una letra, en cuyo caso seguro que es una
        // unidad de Windows ("C:", "D:", etc.), y le anado el file://
        // El caracter '#' debe protegerse en rutas locales
        if (scheme.length() == 1 && Character.isLetter((char) scheme.getBytes()[0])) {
            filename = filename.replace("#", "%23"); //$NON-NLS-1$ //$NON-NLS-2$
            return createURI("file://" + filename); //$NON-NLS-1$
        }

        throw new URISyntaxException(filename, "Tipo de URI no soportado"); //$NON-NLS-1$

    }
}