1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.webkit.network; 27 28 import java.net.MalformedURLException; 29 import java.net.URL; 30 import java.net.URLStreamHandler; 31 import java.util.Collections; 32 import java.util.HashMap; 33 import java.util.Map; 34 35 /** 36 * A collection of static methods for URL creation. 37 */ 38 public final class URLs { 39 40 /** 41 * The mapping between WebPane-specific protocol names and their 42 * respective handlers. 43 */ 44 private static final Map<String,URLStreamHandler> handlerMap; 45 static { 46 Map<String,URLStreamHandler> map = 47 new HashMap<String,URLStreamHandler>(2); 48 map.put("about", new com.sun.webkit.network.about.Handler()); 49 map.put("data", new com.sun.webkit.network.data.Handler()); 50 handlerMap = Collections.unmodifiableMap(map); 51 } 52 53 54 /** 55 * The private default constructor. Ensures non-instantiability. 56 */ 57 private URLs() { 58 throw new AssertionError(); 59 } 60 61 62 /** 63 * Creates a {@code URL} object from the {@code String} representation. 64 * This method is equivalent to the {@link URL#URL(String)} constructor 65 * with the additional support for WebPane-specific protocol handlers. 66 * @param spec the {@code String} to parse as a {@code URL}. 67 * @throws MalformedURLException if the string specifies an unknown 68 * protocol. 69 */ 70 public static URL newURL(String spec) throws MalformedURLException { 71 return newURL(null, spec); 72 } 73 74 /** 75 * Creates a URL by parsing the given spec within a specified context. 76 * This method is equivalent to the {@link URL#URL(URL,String)} 77 * constructor with the additional support for WebPane-specific protocol 78 * handlers. 79 * @param context the context in which to parse the specification. 80 * @param spec the {@code String} to parse as a {@code URL}. 81 * @throws MalformedURLException if no protocol is specified, or an 82 * unknown protocol is found. 83 */ 84 public static URL newURL(URL context, String spec) 85 throws MalformedURLException 86 { 87 try { 88 // Try the standard protocol handler selection procedure 89 return new URL(context, spec); 90 } catch (MalformedURLException ex) { 91 // Try WebPane-specific protocol handler, if any 92 URLStreamHandler handler = null; 93 int colonPosition = spec.indexOf(':'); 94 if (colonPosition != -1) { 95 handler = handlerMap.get( 96 spec.substring(0, colonPosition).toLowerCase()); 97 } 98 if (handler == null) { 99 throw ex; 100 } 101 return new URL(context, spec, handler); 102 } 103 } 104 }