Here you can find the source of createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
String
as an URL.
Parameter | Description |
---|---|
spec | the <code>String</code> to parse |
urlHandlerFactory | an URL stream handler factory to use |
public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
//package com.java2s; /*//w ww .j a va 2s . com * JasperReports - Free Java Reporting Library. * Copyright (C) 2001 - 2014 TIBCO Software Inc. All rights reserved. * http://www.jaspersoft.com * * Unless you have purchased a commercial license agreement from Jaspersoft, * the following license terms apply: * * This program is part of JasperReports. * * JasperReports is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * JasperReports 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with JasperReports. If not, see <http://www.gnu.org/licenses/>. */ import java.net.MalformedURLException; import java.net.URL; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; public class Main { /** * Tries to parse a <code>String</code> as an URL. * * @param spec the <code>String</code> to parse * @param urlHandlerFactory an URL stream handler factory to use * @return an URL if the parsing is successful * @see #getURLHandler(String, URLStreamHandlerFactory) * @see #getURLHandlerFactory(URLStreamHandlerFactory) */ public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory) { URLStreamHandler handler = getURLHandler(spec, urlHandlerFactory); URL url; try { if (handler == null) { url = new URL(spec); } else { url = new URL(null, spec, handler); } } catch (MalformedURLException e) { url = null; } return url; } /** * Returns an URL stream handler for an URL specified as a <code>String</code>. * * @param spec the <code>String</code> to parse as an URL * @param urlHandlerFact an URL stream handler factory * @return an URL stream handler if one was found for the protocol of the URL * @see #getURLHandlerFactory(URLStreamHandlerFactory) */ public static URLStreamHandler getURLHandler(String spec, URLStreamHandlerFactory urlHandlerFact) { URLStreamHandlerFactory urlHandlerFactory = urlHandlerFact;//getURLHandlerFactory(urlHandlerFact); URLStreamHandler handler = null; if (urlHandlerFactory != null) { String protocol = getURLProtocol(spec); if (protocol != null) { handler = urlHandlerFactory .createURLStreamHandler(protocol); } } return handler; } private static String getURLProtocol(String urlSpec) { String protocol = null; String spec = urlSpec.trim(); int colon = spec.indexOf(':'); if (colon > 0) { String proto = spec.substring(0, colon); if (protocolValid(proto)) { protocol = proto; } } return protocol; } private static boolean protocolValid(String protocol) { int length = protocol.length(); if (length < 1) { return false; } if (!Character.isLetter(protocol.charAt(0))) { return false; } for (int i = 1; i < length; ++i) { char c = protocol.charAt(i); if (!(Character.isLetterOrDigit(c) || c == '+' || c == '-' || c == '.')) { return false; } } return true; } }