001    package org.codehaus.mojo.exec;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.io.UnsupportedEncodingException;
024    import java.net.MalformedURLException;
025    import java.net.URL;
026    import java.util.BitSet;
027    
028    /**
029     * Utility for dealing with URLs in pre-JDK 1.4.
030     */
031    public class UrlUtils
032    {
033        private static final BitSet UNRESERVED = new BitSet( Byte.MAX_VALUE - Byte.MIN_VALUE + 1 );
034    
035        private static final int RADIX = 16;
036    
037        private static final int MASK = 0xf;
038    
039        private UrlUtils()
040        {
041        }
042    
043        private static final String ENCODING = "UTF-8";
044    
045        static
046        {
047            try
048            {
049                byte[] bytes =
050                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( ENCODING );
051                for ( int i = 0; i < bytes.length; i++ )
052                {
053                    UNRESERVED.set( bytes[i] );
054                }
055            }
056            catch ( UnsupportedEncodingException e )
057            {
058                // can't happen as UTF-8 must be present
059            }
060        }
061    
062        public static URL getURL( File file )
063            throws MalformedURLException
064        {
065            // with JDK 1.4+, code would be: return new URL( file.toURI().toASCIIString() );
066            URL url = file.toURL();
067            // encode any characters that do not comply with RFC 2396
068            // this is primarily to handle Windows where the user's home directory contains spaces
069            try
070            {
071                byte[] bytes = url.toString().getBytes( ENCODING );
072                StringBuffer buf = new StringBuffer( bytes.length );
073                for ( int i = 0; i < bytes.length; i++ )
074                {
075                    byte b = bytes[i];
076                    if ( b > 0 && UNRESERVED.get( b ) )
077                    {
078                        buf.append( (char) b );
079                    }
080                    else
081                    {
082                        buf.append( '%' );
083                        buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) );
084                        buf.append( Character.forDigit( b & MASK, RADIX ) );
085                    }
086                }
087                return new URL( buf.toString() );
088            }
089            catch ( UnsupportedEncodingException e )
090            {
091                // should not happen as UTF-8 must be present
092                throw new RuntimeException( e );
093            }
094        }
095    }