View Javadoc
1   package org.eclipse.aether.transport.file;
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  /**
23   * URL handling for file URLs. Based on org.apache.maven.wagon.PathUtils.
24   */
25  final class PathUtils
26  {
27  
28      private PathUtils()
29      {
30      }
31  
32      /**
33       * Return the protocol name. <br/>
34       * E.g: for input <code>http://www.codehause.org</code> this method will return <code>http</code>
35       * 
36       * @param url the url
37       * @return the host name
38       */
39      public static String protocol( final String url )
40      {
41          final int pos = url.indexOf( ":" );
42  
43          if ( pos == -1 )
44          {
45              return "";
46          }
47          return url.substring( 0, pos ).trim();
48      }
49  
50      /**
51       * Derive the path portion of the given URL.
52       * 
53       * @param url the file-repository URL
54       * @return the basedir of the repository
55       */
56      public static String basedir( String url )
57      {
58          String protocol = PathUtils.protocol( url );
59  
60          String retValue = null;
61  
62          if ( protocol.length() > 0 )
63          {
64              retValue = url.substring( protocol.length() + 1 );
65          }
66          else
67          {
68              retValue = url;
69          }
70          retValue = decode( retValue );
71          // special case: if omitted // on protocol, keep path as is
72          if ( retValue.startsWith( "//" ) )
73          {
74              retValue = retValue.substring( 2 );
75  
76              if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
77              {
78                  // special case: if there is a windows drive letter, then keep the original return value
79                  retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
80              }
81              else
82              {
83                  // Now we expect the host
84                  int index = retValue.indexOf( "/" );
85                  if ( index >= 0 )
86                  {
87                      retValue = retValue.substring( index + 1 );
88                  }
89  
90                  // special case: if there is a windows drive letter, then keep the original return value
91                  if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
92                  {
93                      retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
94                  }
95                  else if ( index >= 0 )
96                  {
97                      // leading / was previously stripped
98                      retValue = "/" + retValue;
99                  }
100             }
101         }
102 
103         // special case: if there is a windows drive letter using |, switch to :
104         if ( retValue.length() >= 2 && retValue.charAt( 1 ) == '|' )
105         {
106             retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
107         }
108 
109         return retValue.trim();
110     }
111 
112     /**
113      * Decodes the specified (portion of a) URL. <strong>Note:</strong> This decoder assumes that ISO-8859-1 is used to
114      * convert URL-encoded octets to characters.
115      * 
116      * @param url The URL to decode, may be <code>null</code>.
117      * @return The decoded URL or <code>null</code> if the input was <code>null</code>.
118      */
119     static String decode( String url )
120     {
121         String decoded = url;
122         if ( url != null )
123         {
124             int pos = -1;
125             while ( ( pos = decoded.indexOf( '%', pos + 1 ) ) >= 0 )
126             {
127                 if ( pos + 2 < decoded.length() )
128                 {
129                     String hexStr = decoded.substring( pos + 1, pos + 3 );
130                     char ch = (char) Integer.parseInt( hexStr, 16 );
131                     decoded = decoded.substring( 0, pos ) + ch + decoded.substring( pos + 3 );
132                 }
133             }
134         }
135         return decoded;
136     }
137 
138 }