View Javadoc
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   */
36  
37  public class PathDescriptor
38  {
39      private final URL baseUrl;
40  
41      private final URL pathUrl;
42  
43      private final String relativePath;
44  
45      /**
46       * Construct a PathDescriptor from a path.
47       *
48       * @param path the path.
49       * @throws java.net.MalformedURLException if a URL cannot be formed from the path.
50       */
51      public PathDescriptor( final String path )
52          throws MalformedURLException
53      {
54          this( (URL) null, path );
55      }
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 )
65          throws MalformedURLException
66      {
67          this( PathDescriptor.buildBaseUrl( base ), path );
68      }
69  
70      /**
71       * Construct a PathDescriptor from a path and a base.
72       *
73       * @param baseUrl a base reference.
74       * @param path the path.
75       * @throws java.net.MalformedURLException if a URL cannot be formed from the path.
76       */
77      public PathDescriptor( final URL baseUrl, final String path )
78          throws MalformedURLException
79      {
80          this.baseUrl = baseUrl;
81  
82          URL pathURL = null;
83          String relPath = null;
84  
85          try
86          {
87              pathURL = new URL( path );
88          }
89          catch ( MalformedURLException e )
90          {
91              try
92              {
93                  pathURL = buildUrl( baseUrl, path );
94              }
95              catch ( MalformedURLException e2 )
96              {
97                  // If we got an absolute path passed in and end here, then the path
98                  // is converted to relative because we have no reference URL anyway
99                  // to which it has been anchored.
100                 if ( path != null && path.startsWith( "/" ) )
101                 {
102                     relPath = path.substring( 1 );
103                 }
104                 else
105                 {
106                     relPath = path;
107                 }
108             }
109         }
110 
111         this.pathUrl = pathURL;
112         this.relativePath = relPath;
113     }
114 
115     private static URL buildBaseUrl( final String base )
116         throws MalformedURLException
117     {
118         if ( base == null )
119         {
120             return null;
121         }
122 
123         try
124         {
125             return new URL( base );
126         }
127         catch ( MalformedURLException e )
128         {
129             return new File( base ).toURI().toURL();
130         }
131     }
132 
133     private static URL buildUrl( final URL baseUrl, final String path )
134         throws MalformedURLException
135     {
136         if ( baseUrl == null )
137         {
138             throw new MalformedURLException( "Base is null!" );
139         }
140 
141         if ( path == null )
142         {
143             return baseUrl;
144         }
145 
146         if ( baseUrl.getProtocol().equals( "file" ) )
147         {
148             return new File( baseUrl.getFile(), path ).toURI().toURL();
149         }
150 
151         if ( path.startsWith( "/" ) && baseUrl.getPath().endsWith( "/" ) )
152         {
153             return new URL( baseUrl, path.substring( 1 ) );
154         }
155 
156         return new URL( baseUrl, path );
157     }
158 
159     /**
160      * Check if this PathDescriptor describes a file.
161      *
162      * @return true for file, false otherwise.
163      */
164     public boolean isFile()
165     {
166         return isRelative() || pathUrl.getProtocol().equals( "file" );
167     }
168 
169     /**
170      * Check if this PathDescriptor describes a relative path.
171      *
172      * @return true if {@link #getPathUrl()} returns null.
173      */
174     public boolean isRelative()
175     {
176         return pathUrl == null;
177     }
178 
179     /**
180      * Get the base URL.
181      *
182      * @return the base URL.
183      */
184     public URL getBaseUrl()
185     {
186         return baseUrl;
187     }
188 
189     /**
190      * Get the path as a URL.
191      *
192      * @return the path as a URL.
193      */
194     public URL getPathUrl()
195     {
196         return pathUrl;
197     }
198 
199     /**
200      * Get the path.
201      *
202      * @return the path.
203      */
204     public String getPath()
205     {
206         if ( getPathUrl() != null )
207         {
208             if ( isFile() )
209             {
210                 return StringUtils.stripEnd( getPathUrl().getPath(), "/" );
211             }
212             else
213             {
214                 return getPathUrl().getPath();
215             }
216         }
217         else
218         {
219             return relativePath;
220         }
221     }
222 
223     /**
224      * Get the location for files.
225      *
226      * @return the location.
227      */
228     public String getLocation()
229     {
230         if ( isFile() )
231         {
232             if ( getPathUrl() != null )
233             {
234                 return StringUtils.stripEnd( getPathUrl().getFile(), "/" );
235             }
236             else
237             {
238                 return relativePath;
239             }
240         }
241         else
242         {
243             return getPathUrl().toExternalForm();
244         }
245     }
246 
247     /** {@inheritDoc} */
248     public String toString()
249     {
250         StringBuilder res =
251             new StringBuilder( ( StringUtils.isNotEmpty( relativePath ) ) ? relativePath : String.valueOf( pathUrl ) );
252         res.append( " (Base: " ).append( baseUrl ).append( ") Location: " ).append( getLocation() );
253         return res.toString();
254     }
255 }