Java URL from makeURL(String urlstr)

Here you can find the source of makeURL(String urlstr)

Description

make URL

License

Open Source License

Declaration

public static URL makeURL(String urlstr) 

Method Source Code

//package com.java2s;
/*##############################################################################

 Copyright (C) 2011 HPCC Systems./*from   w w  w.ja v a2 s.  c om*/

 All rights reserved. This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
 ############################################################################## */

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

import java.util.regex.Pattern;

public class Main {
    public static final String defaultprotocol = "http";
    public static final String protocolsep = "://";
    public static final Pattern URLPROTPATTERN = Pattern.compile(
            "((https?|ftp|file)://|www\\.).+", Pattern.CASE_INSENSITIVE
                    | Pattern.UNICODE_CASE);

    public static URL makeURL(String urlstr) {
        URL theURL = null;
        try {
            theURL = new URL(ensureURLProtocol(urlstr));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return theURL;
    }

    public static String ensureURLProtocol(String urlstr) {
        if (!URLPROTPATTERN.matcher(urlstr).matches()) {
            return defaultprotocol + protocolsep + urlstr;
        } else
            return urlstr;
    }
}

Related

  1. makeURL(String location)
  2. makeURL(String title, String urlString)
  3. makeUrl(String url)
  4. makeUrl(String url)
  5. makeURL(String url)
  6. makeURL(String[] URLStrings)
  7. newURL(CharSequence urlString)
  8. newUrl(String href)
  9. newURL(String location)