Frames | No Frames |
1: /* gnu.java.net.protocol.jar.Handler - jar protocol handler for java.net 2: Copyright (C) 1999, 2002, 2003, 2005, 2006 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: import java.util.ArrayList; 49: import java.util.Iterator; 50: import java.util.StringTokenizer; 51: 52: /** 53: * @author Kresten Krab Thorup (krab@gnu.org) 54: */ 55: public class Handler extends URLStreamHandler 56: { 57: /** 58: * A do nothing constructor 59: */ 60: public Handler() 61: { 62: } 63: 64: /** 65: * This method returs a new JarURLConnection for the specified URL 66: * 67: * @param url The URL to return a connection for 68: * 69: * @return The URLConnection 70: * 71: * @exception IOException If an error occurs 72: */ 73: protected URLConnection openConnection(URL url) throws IOException 74: { 75: return new Connection(url); 76: } 77: 78: /** 79: * This method overrides URLStreamHandler's for parsing url of protocol "jar" 80: * 81: * @param url The URL object in which to store the results 82: * @param url_string The String-ized URL to parse 83: * @param start The position in the string to start scanning from 84: * @param end The position in the string to stop scanning 85: */ 86: protected void parseURL (URL url, String url_string, int start, int end) 87: { 88: // This method does not throw an exception or return a value. Thus our 89: // strategy when we encounter an error in parsing is to return without 90: // doing anything. 91: String file = url.getFile(); 92: 93: if (!file.equals("")) 94: { //has context url 95: url_string = url_string.substring (start, end); 96: if (url_string.startsWith("/")) 97: { //url string is an absolute path 98: int idx = file.lastIndexOf ("!/"); 99: 100: if (idx < 0) 101: throw new URLParseError("no !/ in spec"); 102: 103: file = file.substring (0, idx + 1) + url_string; 104: } 105: else if (url_string.length() > 0) 106: { 107: int idx = file.lastIndexOf ("/"); 108: if (idx == -1) //context path is weird 109: file = "/" + url_string; 110: else if (idx == (file.length() - 1)) 111: //just concatenate two parts 112: file = file + url_string; 113: else 114: // according to Java API Documentation, here is a little different 115: // with URLStreamHandler.parseURL 116: // but JDK seems doesn't handle it well 117: file = file.substring(0, idx + 1) + url_string; 118: } 119: 120: setURL (url, "jar", url.getHost(), url.getPort(), flat(file), null); 121: return; 122: } 123: 124: // Bunches of things should be true. Make sure. 125: if (end < start) 126: return; 127: if (end - start < 2) 128: return; 129: if (start > url_string.length()) 130: return; 131: 132: // Skip remains of protocol 133: url_string = url_string.substring (start, end); 134: 135: int jar_stop; 136: if ((jar_stop = url_string.indexOf("!/")) < 0) 137: throw new URLParseError("no !/ in spec"); 138: 139: try 140: { 141: new URL(url_string.substring (0, jar_stop)); 142: } 143: catch (MalformedURLException e) 144: { 145: throw new URLParseError("invalid inner URL: " + e.getMessage()); 146: } 147: 148: if (!url.getProtocol().equals ("jar") ) 149: throw new URLParseError("unexpected protocol " + url.getProtocol()); 150: 151: setURL (url, "jar", url.getHost(), url.getPort(), url_string, null); 152: } 153: 154: /** 155: * Makes the given jar url string 'flat' by removing any . and .. from 156: * jar file path because ZipFile entries can only handle flat paths. 157: * Inside jar files '/' is always the path separator. 158: */ 159: private static String flat(String url_string) 160: { 161: int jar_stop = url_string.indexOf("!/"); 162: String jar_path = url_string.substring(jar_stop + 1, url_string.length()); 163: 164: if (jar_path.indexOf("/.") < 0) 165: return url_string; 166: 167: ArrayList<String> tokens = new ArrayList<String>(); 168: StringTokenizer st = new StringTokenizer(jar_path, "/"); 169: while (st.hasMoreTokens()) 170: { 171: String token = st.nextToken(); 172: if (token.equals(".")) 173: continue; 174: else if (token.equals("..")) 175: { 176: if (! tokens.isEmpty()) 177: tokens.remove(tokens.size() - 1); 178: } 179: else 180: tokens.add(token); 181: } 182: 183: StringBuffer path = new StringBuffer(url_string.length()); 184: path.append(url_string.substring(0, jar_stop + 1)); 185: 186: Iterator<String> it = tokens.iterator(); 187: while (it.hasNext()) 188: path.append('/').append(it.next()); 189: 190: return path.toString(); 191: } 192: 193: /** 194: * This method converts a Jar URL object into a String. 195: * 196: * @param url The URL object to convert 197: */ 198: protected String toExternalForm (URL url) 199: { 200: String file = url.getFile(); 201: String ref = url.getRef(); 202: 203: // return "jar:" + file; 204: // Performance!!: 205: // Do the concatenation manually to avoid resize StringBuffer's 206: // internal buffer. The length of ref is not taken into consideration 207: // as it's a rare path. 208: StringBuffer sb = new StringBuffer (file.length() + 5); 209: sb.append ("jar:"); 210: sb.append (file); 211: if (ref != null) 212: sb.append('#').append(ref); 213: return sb.toString(); 214: } 215: }