| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UrlUtils |
|
| 4.0;4 |
| 1 | package org.codehaus.mojo.exec; | |
| 2 | ||
| 3 | /* | |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one | |
| 5 | * or more contributor license agreements. See the NOTICE file | |
| 6 | * distributed with this work for additional information | |
| 7 | * regarding copyright ownership. The ASF licenses this file | |
| 8 | * to you under the Apache License, Version 2.0 (the | |
| 9 | * "License"); you may not use this file except in compliance | |
| 10 | * with the License. You may obtain a copy of the License at | |
| 11 | * | |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 13 | * | |
| 14 | * Unless required by applicable law or agreed to in writing, | |
| 15 | * software distributed under the License is distributed on an | |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| 17 | * KIND, either express or implied. See the License for the | |
| 18 | * specific language governing permissions and limitations | |
| 19 | * under the License. | |
| 20 | */ | |
| 21 | ||
| 22 | import java.io.File; | |
| 23 | import java.io.UnsupportedEncodingException; | |
| 24 | import java.net.MalformedURLException; | |
| 25 | import java.net.URL; | |
| 26 | import java.util.BitSet; | |
| 27 | ||
| 28 | /** | |
| 29 | * Utility for dealing with URLs in pre-JDK 1.4. | |
| 30 | */ | |
| 31 | public class UrlUtils | |
| 32 | { | |
| 33 | 0 | private static final BitSet UNRESERVED = new BitSet( Byte.MAX_VALUE - Byte.MIN_VALUE + 1 ); |
| 34 | ||
| 35 | private static final int RADIX = 16; | |
| 36 | ||
| 37 | private static final int MASK = 0xf; | |
| 38 | ||
| 39 | private UrlUtils() | |
| 40 | 0 | { |
| 41 | 0 | } |
| 42 | ||
| 43 | private static final String ENCODING = "UTF-8"; | |
| 44 | ||
| 45 | static | |
| 46 | { | |
| 47 | try | |
| 48 | { | |
| 49 | 0 | byte[] bytes = |
| 50 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( ENCODING ); | |
| 51 | 0 | for ( int i = 0; i < bytes.length; i++ ) |
| 52 | { | |
| 53 | 0 | UNRESERVED.set( bytes[i] ); |
| 54 | } | |
| 55 | } | |
| 56 | 0 | catch ( UnsupportedEncodingException e ) |
| 57 | { | |
| 58 | // can't happen as UTF-8 must be present | |
| 59 | 0 | } |
| 60 | 0 | } |
| 61 | ||
| 62 | public static URL getURL( File file ) | |
| 63 | throws MalformedURLException | |
| 64 | { | |
| 65 | // with JDK 1.4+, code would be: return new URL( file.toURI().toASCIIString() ); | |
| 66 | 0 | URL url = file.toURL(); |
| 67 | // encode any characters that do not comply with RFC 2396 | |
| 68 | // this is primarily to handle Windows where the user's home directory contains spaces | |
| 69 | try | |
| 70 | { | |
| 71 | 0 | byte[] bytes = url.toString().getBytes( ENCODING ); |
| 72 | 0 | StringBuffer buf = new StringBuffer( bytes.length ); |
| 73 | 0 | for ( int i = 0; i < bytes.length; i++ ) |
| 74 | { | |
| 75 | 0 | byte b = bytes[i]; |
| 76 | 0 | if ( b > 0 && UNRESERVED.get( b ) ) |
| 77 | { | |
| 78 | 0 | buf.append( (char) b ); |
| 79 | } | |
| 80 | else | |
| 81 | { | |
| 82 | 0 | buf.append( '%' ); |
| 83 | 0 | buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) ); |
| 84 | 0 | buf.append( Character.forDigit( b & MASK, RADIX ) ); |
| 85 | } | |
| 86 | } | |
| 87 | 0 | return new URL( buf.toString() ); |
| 88 | } | |
| 89 | 0 | catch ( UnsupportedEncodingException e ) |
| 90 | { | |
| 91 | // should not happen as UTF-8 must be present | |
| 92 | 0 | throw new RuntimeException( e ); |
| 93 | } | |
| 94 | } | |
| 95 | } |