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