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 1438392 2013-01-25 08:54:48Z olamy $
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 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 ) throws MalformedURLException
65 {
66 this( PathDescriptor.buildBaseUrl( base ), path );
67 }
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 {
78 this.baseUrl = baseUrl;
79
80 URL pathURL = null;
81 String relPath = null;
82
83 try
84 {
85 pathURL = new URL( path );
86 }
87 catch ( MalformedURLException e )
88 {
89 try
90 {
91 pathURL = buildUrl( baseUrl, path );
92 }
93 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 if ( path != null && path.startsWith( "/" ) )
99 {
100 relPath = path.substring( 1 );
101 }
102 else
103 {
104 relPath = path;
105 }
106 }
107 }
108
109 this.pathUrl = pathURL;
110 this.relativePath = relPath;
111 }
112
113 private static URL buildBaseUrl( final String base ) throws MalformedURLException
114 {
115 if ( base == null )
116 {
117 return null;
118 }
119
120 try
121 {
122 return new URL( base );
123 }
124 catch ( MalformedURLException e )
125 {
126 return new File( base ).toURI().toURL();
127 }
128 }
129
130 private static URL buildUrl( final URL baseUrl, final String path ) throws MalformedURLException
131 {
132 if ( baseUrl == null )
133 {
134 throw new MalformedURLException( "Base is null!" );
135 }
136
137 if ( path == null )
138 {
139 return baseUrl;
140 }
141
142 if ( baseUrl.getProtocol().equals( "file" ) )
143 {
144 return new File( baseUrl.getFile(), path ).toURI().toURL();
145 }
146
147 if ( path.startsWith( "/" ) && baseUrl.getPath().endsWith( "/" ) )
148 {
149 return new URL( baseUrl, path.substring( 1 ) );
150 }
151
152 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 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 return pathUrl == null;
173 }
174
175 /**
176 * Get the base URL.
177 *
178 * @return the base URL.
179 */
180 public URL getBaseUrl()
181 {
182 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 return pathUrl;
193 }
194
195 /**
196 * Get the path.
197 *
198 * @return the path.
199 */
200 public String getPath()
201 {
202 if ( getPathUrl() != null )
203 {
204 if ( isFile() )
205 {
206 return StringUtils.stripEnd( getPathUrl().getPath(), "/" );
207 }
208 else
209 {
210 return getPathUrl().getPath();
211 }
212 }
213 else
214 {
215 return relativePath;
216 }
217 }
218
219 /**
220 * Get the location for files.
221 *
222 * @return the location.
223 */
224 public String getLocation()
225 {
226 if ( isFile() )
227 {
228 if ( getPathUrl() != null )
229 {
230 return StringUtils.stripEnd( getPathUrl().getFile(), "/" );
231 }
232 else
233 {
234 return relativePath;
235 }
236 }
237 else
238 {
239 return getPathUrl().toExternalForm();
240 }
241 }
242
243 /** {@inheritDoc} */
244 public String toString()
245 {
246 StringBuilder res =
247 new StringBuilder( ( StringUtils.isNotEmpty( relativePath ) ) ? relativePath : String.valueOf( pathUrl ) );
248 res.append( " (Base: " ).append( baseUrl ).append( ") Location: " ).append( getLocation() );
249 return res.toString();
250 }
251 }