Java URI Create createUri(String sUri, String defaultSchema, int deafultPort)

Here you can find the source of createUri(String sUri, String defaultSchema, int deafultPort)

Description

It creates an URI from a String.

License

Open Source License

Parameter

Parameter Description
sUri The uri like a string
defaultSchema The deafulet schema
deafultPort The default port

Exception

Parameter Description
URISyntaxException an exception

Return

An uri

Declaration

public static URI createUri(String sUri, String defaultSchema, int deafultPort) throws URISyntaxException 

Method Source Code


//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
 *
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
 *
 * This program 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 *
 * For more information, contact://  w  w w.  j  a v  a2s.co  m
 *
 *  Generalitat Valenciana
 *   Conselleria d'Infraestructures i Transport
 *   Av. Blasco Ib??ez, 50
 *   46010 VALENCIA
 *   SPAIN
 *
 *      +34 963862235
 *   gvsig@gva.es
 *      www.gvsig.gva.es
 *
 *    or
 *
 *   IVER T.I. S.A
 *   Salamanca 50
 *   46005 Valencia
 *   Spain
 *
 *   +34 963163400
 *   dac@iver.es
 */

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

public class Main {
    /**
     * It creates an URI from a String. If the port and the schema
     * have not been especified on the string this method
     * adds them
     * @param sUri
     * The uri like a string
     * @param defaultSchema
     * The deafulet schema
     * @param deafultPort
     * The default port
     * @return
     * An uri
     * @throws URISyntaxException 
     */
    public static URI createUri(String sUri, String defaultSchema, int deafultPort) throws URISyntaxException {
        String schema = defaultSchema;
        String host = null;
        int port = deafultPort;
        String path = "";
        //Cut the schema
        int index = sUri.indexOf("://");
        if (index > -1) {
            schema = sUri.substring(0, index);
            sUri = sUri.substring(index + 3, sUri.length());
        }
        //Cut the path
        index = sUri.indexOf("/");
        if (index > -1) {
            path = sUri.substring(index, sUri.length());
            sUri = sUri.substring(0, index);
        }
        //Cut the host:port
        index = sUri.indexOf(":");
        if (index > -1) {
            host = sUri.substring(0, index);
            port = Integer.valueOf(sUri.substring(index + 1, sUri.length())).intValue();
        } else {
            host = sUri;
        }
        return new URI(schema + "://" + host + ":" + port + path);
    }
}

Related

  1. createUri(String name, Kind kind)
  2. createURI(String path)
  3. createURI(String path)
  4. createURI(String scheme, String host, String path)
  5. createURI(String spec)
  6. createUri(String u)
  7. createUri(String uri)
  8. createURI(String uri)
  9. createURI(String uri)