Java URI Create createURI(String extension)

Here you can find the source of createURI(String extension)

Description

Creates a simple URI for the provided extension.

License

Open Source License

Parameter

Parameter Description
extension The extension for the test URI file. This file probably will not actually exist. If null , then the file will have no extension.

Return

A correctly formed URI with the provided extension.

Declaration

public static URI createURI(String extension) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 UT-Battelle, LLC.//from  ww w .  j  a  va 2 s.c  o  m
 * 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:
 *   Jordan H. Deyton (UT-Battelle, LLC.) - Initial API and implementation 
 *   and/or initial documentation
 *   
 *******************************************************************************/

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**
     * Creates a simple URI for the provided extension. The generated URI will
     * be local.
     * 
     * @param extension
     *            The extension for the test URI file. This file probably will
     *            not actually exist. If {@code null}, then the file will have
     *            no extension.
     * @return A correctly formed URI with the provided extension.
     */
    public static URI createURI(String extension) {
        return createURI(extension, null);
    }

    /**
     * Creates a simple URI for the provided extension.
     * 
     * @param extension
     *            The extension for the test URI file. This file probably will
     *            not actually exist. If {@code null}, then the file will have
     *            no extension.
     * @param host
     *            The host for the file. If null, a local file URI will be
     *            created, otherwise the host name will be used.
     * @return A correctly formed URI with the provided extension.
     */
    public static URI createURI(String extension, String host) {
        String filename = (extension != null ? "kung_fury." + extension : "future_cop");

        URI uri = null;
        if (host == null) {
            uri = new File(filename).toURI();
        } else {
            try {
                uri = new URL("file", host, 10, "/" + filename).toURI();
            } catch (MalformedURLException e) {
                // This will never happen, because "file" is a valid protocol.
            } catch (URISyntaxException e) {
                // This will never happen unless the URL implementation is
                // changed for the worse.
            }
        }
        return uri;
    }
}

Related

  1. createURI(final String scheme, final String host, final int port)
  2. createURI(final String scheme, final String host, int port, final String path, final String query, final String fragment)
  3. createURI(final String scheme, final String[] pathAsArray)
  4. createURI(List proxyPorts)
  5. createUri(String base)
  6. createURI(String filename)
  7. createURI(String hostname, int port)
  8. createUri(String name, Kind kind)
  9. createURI(String path)