Frames | No Frames |
1: /* gnu.java.net.protocol.jar.Handler - jar protocol handler for java.net 2: Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.net.protocol.jar; 40: 41: import gnu.java.net.URLParseError; 42: 43: import java.io.IOException; 44: import java.net.MalformedURLException; 45: import java.net.URL; 46: import java.net.URLConnection; 47: import java.net.URLStreamHandler; 48: 49: /** 50: * @author Kresten Krab Thorup (krab@gnu.org) 51: */ 52: public class Handler extends URLStreamHandler 53: { 54: /** 55: * A do nothing constructor 56: */ 57: public Handler() 58: { 59: } 60: 61: /** 62: * This method returs a new JarURLConnection for the specified URL 63: * 64: * @param url The URL to return a connection for 65: * 66: * @return The URLConnection 67: * 68: * @exception IOException If an error occurs 69: */ 70: protected URLConnection openConnection(URL url) throws IOException 71: { 72: return new Connection(url); 73: } 74: 75: /** 76: * This method overrides URLStreamHandler's for parsing url of protocol "jar" 77: * 78: * @param url The URL object in which to store the results 79: * @param url_string The String-ized URL to parse 80: * @param start The position in the string to start scanning from 81: * @param end The position in the string to stop scanning 82: */ 83: protected void parseURL (URL url, String url_string, int start, int end) 84: { 85: // This method does not throw an exception or return a value. Thus our 86: // strategy when we encounter an error in parsing is to return without 87: // doing anything. 88: String file = url.getFile(); 89: 90: if (!file.equals("")) 91: { //has context url 92: url_string = url_string.substring (start, end); 93: if (url_string.startsWith("/")) 94: { //url string is an absolute path 95: int idx = file.lastIndexOf ("!/"); 96: 97: if (idx < 0) 98: throw new URLParseError("no !/ in spec"); 99: 100: file = file.substring (0, idx + 1) + url_string; 101: } 102: else if (url_string.length() > 0) 103: { 104: int idx = file.lastIndexOf ("/"); 105: if (idx == -1) //context path is weird 106: file = "/" + url_string; 107: else if (idx == (file.length() - 1)) 108: //just concatenate two parts 109: file = file + url_string; 110: else 111: // according to Java API Documentation, here is a little different 112: // with URLStreamHandler.parseURL 113: // but JDK seems doesn't handle it well 114: file = file.substring(0, idx + 1) + url_string; 115: } 116: 117: setURL (url, "jar", url.getHost(), url.getPort(), file, null); 118: return; 119: } 120: 121: // Bunches of things should be true. Make sure. 122: if (end < start) 123: return; 124: if (end - start < 2) 125: return; 126: if (start > url_string.length()) 127: return; 128: 129: // Skip remains of protocol 130: url_string = url_string.substring (start, end); 131: 132: int jar_stop; 133: if ((jar_stop = url_string.indexOf("!/")) < 0) 134: throw new URLParseError("no !/ in spec"); 135: 136: try 137: { 138: new URL(url_string.substring (0, jar_stop)); 139: } 140: catch (MalformedURLException e) 141: { 142: throw new URLParseError("invalid inner URL: " + e.getMessage()); 143: } 144: 145: if (!url.getProtocol().equals ("jar") ) 146: throw new URLParseError("unexpected protocol " + url.getProtocol()); 147: 148: setURL (url, "jar", url.getHost(), url.getPort(), url_string, null); 149: } 150: 151: /** 152: * This method converts a Jar URL object into a String. 153: * 154: * @param url The URL object to convert 155: */ 156: protected String toExternalForm (URL url) 157: { 158: String file = url.getFile(); 159: String ref = url.getRef(); 160: 161: // return "jar:" + file; 162: // Performance!!: 163: // Do the concatenation manually to avoid resize StringBuffer's 164: // internal buffer. The length of ref is not taken into consideration 165: // as it's a rare path. 166: StringBuffer sb = new StringBuffer (file.length() + 5); 167: sb.append ("jar:"); 168: sb.append (file); 169: if (ref != null) 170: sb.append('#').append(ref); 171: return sb.toString(); 172: } 173: }