Home | History | Annotate | Download | only in url
      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 
     18 package libcore.net.url;
     19 
     20 import java.io.IOException;
     21 import java.net.MalformedURLException;
     22 import java.net.URL;
     23 import java.net.URLConnection;
     24 import java.net.URLStreamHandler;
     25 
     26 public class JarHandler extends URLStreamHandler {
     27     /**
     28      * Returns a connection to the jar file pointed by this <code>URL</code>
     29      * in the file system
     30      *
     31      * @return java.net.URLConnection A connection to the resource pointed by
     32      *         this url.
     33      * @param u
     34      *            java.net.URL The URL to which the connection is pointing to
     35      *
     36      * @throws IOException
     37      *             thrown if an IO error occurs when this method tries to
     38      *             establish connection.
     39      */
     40     @Override
     41     protected URLConnection openConnection(URL u) throws IOException {
     42         return new JarURLConnectionImpl(u);
     43     }
     44 
     45     /**
     46      *
     47      * @param url
     48      *            URL the context URL
     49      * @param spec
     50      *            java.lang.String the spec string
     51      * @param start
     52      *            int the location to start parsing from
     53      * @param limit
     54      *            int the location where parsing ends
     55      */
     56     @Override
     57     protected void parseURL(URL url, String spec, int start, int limit) {
     58         String file = url.getFile();
     59         if (file == null) {
     60             file = "";
     61         }
     62         if (limit > start) {
     63             spec = spec.substring(start, limit);
     64         } else {
     65             spec = "";
     66         }
     67         if (spec.indexOf("!/") == -1 && (file.indexOf("!/") == -1)) {
     68             throw new NullPointerException("Cannot find \"!/\"");
     69         }
     70         if (file.isEmpty()) {
     71             file = spec;
     72         } else if (spec.charAt(0) == '/') {
     73             file = file.substring(0, file.indexOf('!') + 1) + spec;
     74         } else {
     75             int idx = file.indexOf('!');
     76             String tmpFile = file.substring(idx + 1, file.lastIndexOf('/') + 1) + spec;
     77             tmpFile = UrlUtils.canonicalizePath(tmpFile, true);
     78             file = file.substring(0, idx + 1) + tmpFile;
     79         }
     80         try {
     81             // check that the embedded url is valid
     82             new URL(file);
     83         } catch (MalformedURLException e) {
     84             throw new NullPointerException(e.toString());
     85         }
     86         setURL(url, "jar", "", -1, null, null, file, null, null);
     87     }
     88 
     89     /**
     90      * Build and return the externalized string representation of url.
     91      *
     92      * @return String the externalized string representation of url
     93      * @param url
     94      *            a URL
     95      */
     96     @Override
     97     protected String toExternalForm(URL url) {
     98         StringBuilder sb = new StringBuilder();
     99         sb.append("jar:");
    100         sb.append(url.getFile());
    101         String ref = url.getRef();
    102         if (ref != null) {
    103             sb.append(ref);
    104         }
    105         return sb.toString();
    106     }
    107 }
    108