JarHandler.java revision adc854b798c1cfe3bfd4c27d68d5cee38ca617da
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.internal.net.www.protocol.jar; 19 20import java.io.IOException; 21import java.net.MalformedURLException; 22import java.net.URL; 23import java.net.URLConnection; 24import java.net.URLStreamHandler; 25 26import org.apache.harmony.luni.util.Msg; 27import org.apache.harmony.luni.util.URLUtil; 28 29public class Handler extends URLStreamHandler { 30 /** 31 * Returns a connection to the jar file pointed by this <code>URL</code> 32 * in the file system 33 * 34 * @return java.net.URLConnection A connection to the resource pointed by 35 * this url. 36 * @param u 37 * java.net.URL The URL to which the connection is pointing to 38 * 39 * @thows IOException thrown if an IO error occurs when this method tries to 40 * establish connection. 41 */ 42 @Override 43 protected URLConnection openConnection(URL u) throws IOException { 44 return new JarURLConnection(u); 45 } 46 47 /** 48 * 49 * @param url 50 * URL the context URL 51 * @param spec 52 * java.lang.String the spec string 53 * @param start 54 * int the location to start parsing from 55 * @param limit 56 * int the location where parsing ends 57 */ 58 @Override 59 protected void parseURL(URL url, String spec, int start, int limit) { 60 String file = url.getFile(); 61 if (file == null) { 62 file = ""; //$NON-NLS-1$ 63 } 64 if (limit > start) { 65 spec = spec.substring(start, limit); 66 } else { 67 spec = ""; //$NON-NLS-1$ 68 } 69 if (spec.indexOf("!/") == -1 && (file.indexOf("!/") == -1)) { //$NON-NLS-1$ //$NON-NLS-2$ 70 throw new NullPointerException(Msg.getString("K01b6")); //$NON-NLS-1$ 71 } 72 if (file.equals("")) { 73 file = spec; 74 } else if (spec.charAt(0) == '/') { 75 file = file.substring(0, file.indexOf('!') + 1) + spec; 76 } else { 77 int idx = file.indexOf('!'); 78 String tmpFile = file.substring(idx + 1, file.lastIndexOf('/') + 1) + spec; 79 tmpFile = URLUtil.canonicalizePath(tmpFile); 80 file = file.substring(0, idx + 1) + tmpFile; 81 } 82 try { 83 // check that the embedded url is valid 84 new URL(file); 85 } catch (MalformedURLException e) { 86 throw new NullPointerException(e.toString()); 87 } 88 setURL(url, "jar", "", -1, null, null, file, null, null); //$NON-NLS-1$//$NON-NLS-2$ 89 } 90 91 /** 92 * Build and return the externalized string representation of url. 93 * 94 * @return String the externalized string representation of url 95 * @param url 96 * a URL 97 */ 98 @Override 99 protected String toExternalForm(URL url) { 100 StringBuffer sb = new StringBuffer(); 101 sb.append("jar:"); //$NON-NLS-1$ 102 sb.append(url.getFile()); 103 String ref = url.getRef(); 104 if (ref != null) { 105 sb.append(ref); 106 } 107 return sb.toString(); 108 } 109} 110