Coverage Report - org.apache.maven.doxia.site.decoration.inheritance.PathDescriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
PathDescriptor
94%
48/51
90%
29/32
3,25
 
 1  
 package org.apache.maven.doxia.site.decoration.inheritance;
 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.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 
 26  
 import org.codehaus.plexus.util.StringUtils;
 27  
 
 28  
 /**
 29  
  * This class holds an instance of a maven path. This consists of a relative path (e.g. images/maven-logo.png) and a
 30  
  * base reference which can also be a relative path (e.g. '.' or '../doxia') or an URL that is used for an absolute
 31  
  * anchor.
 32  
  *
 33  
  * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
 34  
  * @deprecated use {@link URIPathDescriptor} instead.
 35  
  * @version $Id: PathDescriptor.java 1058083 2011-01-12 11:34:02Z ltheussl $
 36  
  */
 37  
 
 38  
 public class PathDescriptor
 39  
 {
 40  
     private final URL baseUrl;
 41  
 
 42  
     private final URL pathUrl;
 43  
 
 44  
     private final String relativePath;
 45  
 
 46  
     /**
 47  
      * Construct a PathDescriptor from a path.
 48  
      *
 49  
      * @param path the path.
 50  
      * @throws java.net.MalformedURLException if a URL cannot be formed from the path.
 51  
      */
 52  
     public PathDescriptor( final String path ) throws MalformedURLException
 53  
     {
 54  60
         this( (URL) null, path );
 55  60
     }
 56  
 
 57  
     /**
 58  
      * Construct a PathDescriptor from a path and a base.
 59  
      *
 60  
      * @param base a base reference.
 61  
      * @param path the path.
 62  
      * @throws java.net.MalformedURLException if a URL cannot be formed from the path.
 63  
      */
 64  
     public PathDescriptor( final String base, final String path ) throws MalformedURLException
 65  
     {
 66  192
         this( PathDescriptor.buildBaseUrl( base ), path );
 67  192
     }
 68  
 
 69  
     /**
 70  
      * Construct a PathDescriptor from a path and a base.
 71  
      *
 72  
      * @param baseUrl a base reference.
 73  
      * @param path the path.
 74  
      * @throws java.net.MalformedURLException if a URL cannot be formed from the path.
 75  
      */
 76  
     public PathDescriptor( final URL baseUrl, final String path ) throws MalformedURLException
 77  252
     {
 78  252
         this.baseUrl = baseUrl;
 79  
 
 80  252
         URL pathURL = null;
 81  252
         String relPath = null;
 82  
 
 83  
         try
 84  
         {
 85  252
             pathURL = new URL( path );
 86  
         }
 87  240
         catch ( MalformedURLException e )
 88  
         {
 89  
             try
 90  
             {
 91  240
                 pathURL = buildUrl( baseUrl, path );
 92  
             }
 93  96
             catch ( MalformedURLException e2 )
 94  
             {
 95  
                 // If we got an absolute path passed in and end here, then the path
 96  
                 // is converted to relative because we have no reference URL anyway
 97  
                 // to which it has been anchored.
 98  96
                 if ( path != null && path.startsWith( "/" ) )
 99  
                 {
 100  24
                     relPath = path.substring( 1 );
 101  
                 }
 102  
                 else
 103  
                 {
 104  72
                     relPath = path;
 105  
                 }
 106  144
             }
 107  12
         }
 108  
 
 109  252
         this.pathUrl = pathURL;
 110  252
         this.relativePath = relPath;
 111  252
     }
 112  
 
 113  
     private static URL buildBaseUrl( final String base ) throws MalformedURLException
 114  
     {
 115  192
         if ( base == null )
 116  
         {
 117  42
             return null;
 118  
         }
 119  
 
 120  
         try
 121  
         {
 122  150
             return new URL( base );
 123  
         }
 124  72
         catch ( MalformedURLException e )
 125  
         {
 126  72
             return new File( base ).toURI().toURL();
 127  
         }
 128  
     }
 129  
 
 130  
     private static URL buildUrl( final URL baseUrl, final String path ) throws MalformedURLException
 131  
     {
 132  240
         if ( baseUrl == null )
 133  
         {
 134  96
             throw new MalformedURLException( "Base is null!" );
 135  
         }
 136  
 
 137  144
         if ( path == null )
 138  
         {
 139  24
             return baseUrl;
 140  
         }
 141  
 
 142  120
         if ( baseUrl.getProtocol().equals( "file" ) )
 143  
         {
 144  84
             return new File( baseUrl.getFile(), path ).toURI().toURL();
 145  
         }
 146  
 
 147  36
         if ( path.startsWith( "/" ) && baseUrl.getPath().endsWith( "/" ) )
 148  
         {
 149  12
             return new URL( baseUrl, path.substring( 1 ) );
 150  
         }
 151  
 
 152  24
         return new URL( baseUrl, path );
 153  
     }
 154  
 
 155  
     /**
 156  
      * Check if this PathDescriptor decribes a file.
 157  
      *
 158  
      * @return true for file, false otherwise.
 159  
      */
 160  
     public boolean isFile()
 161  
     {
 162  858
         return isRelative() || pathUrl.getProtocol().equals( "file" );
 163  
     }
 164  
 
 165  
     /**
 166  
      * Check if this PathDescriptor decribes a relative path.
 167  
      *
 168  
      * @return true if {@link #getPathUrl()} returns null.
 169  
      */
 170  
     public boolean isRelative()
 171  
     {
 172  1110
         return pathUrl == null;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Get the base URL.
 177  
      *
 178  
      * @return the base URL.
 179  
      */
 180  
     public URL getBaseUrl()
 181  
     {
 182  156
         return baseUrl;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Get the path as a URL.
 187  
      *
 188  
      * @return the path as a URL.
 189  
      */
 190  
     public URL getPathUrl()
 191  
     {
 192  1218
         return pathUrl;
 193  
     }
 194  
 
 195  
     /**
 196  
      * Get the path.
 197  
      *
 198  
      * @return the path.
 199  
      */
 200  
     public String getPath()
 201  
     {
 202  366
         if ( getPathUrl() != null )
 203  
         {
 204  240
             if ( isFile() )
 205  
             {
 206  168
                 return StringUtils.stripEnd( getPathUrl().getPath(), "/" );
 207  
             }
 208  
             else
 209  
             {
 210  72
                 return getPathUrl().getPath();
 211  
             }
 212  
         }
 213  
         else
 214  
         {
 215  126
             return relativePath;
 216  
         }
 217  
     }
 218  
 
 219  
     /**
 220  
      * Get the location for files.
 221  
      *
 222  
      * @return the location.
 223  
      */
 224  
     public String getLocation()
 225  
     {
 226  312
         if ( isFile() )
 227  
         {
 228  252
             if ( getPathUrl() != null )
 229  
             {
 230  132
                 return StringUtils.stripEnd( getPathUrl().getFile(), "/" );
 231  
             }
 232  
             else
 233  
             {
 234  120
                 return relativePath;
 235  
             }
 236  
         }
 237  
         else
 238  
         {
 239  60
             return getPathUrl().toExternalForm();
 240  
         }
 241  
     }
 242  
 
 243  
     /** {@inheritDoc} */
 244  
     public String toString()
 245  
     {
 246  0
         StringBuffer res =
 247  
             new StringBuffer( ( StringUtils.isNotEmpty( relativePath ) ) ? relativePath : String.valueOf( pathUrl ) );
 248  0
         res.append( " (Base: " ).append( baseUrl ).append( ") Location: " ).append( getLocation() );
 249  0
         return res.toString();
 250  
     }
 251  
 }