1 | |
package org.apache.maven.doxia.site.decoration.inheritance; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
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 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public PathDescriptor( final String path ) throws MalformedURLException |
53 | |
{ |
54 | 20 | this( (URL) null, path ); |
55 | 20 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public PathDescriptor( final String base, final String path ) throws MalformedURLException |
65 | |
{ |
66 | 64 | this( PathDescriptor.buildBaseUrl( base ), path ); |
67 | 64 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public PathDescriptor( final URL baseUrl, final String path ) throws MalformedURLException |
77 | 84 | { |
78 | 84 | this.baseUrl = baseUrl; |
79 | |
|
80 | 84 | URL pathURL = null; |
81 | 84 | String relPath = null; |
82 | |
|
83 | |
try |
84 | |
{ |
85 | 84 | pathURL = new URL( path ); |
86 | |
} |
87 | 80 | catch ( MalformedURLException e ) |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | 80 | pathURL = buildUrl( baseUrl, path ); |
92 | |
} |
93 | 32 | catch ( MalformedURLException e2 ) |
94 | |
{ |
95 | |
|
96 | |
|
97 | |
|
98 | 32 | if ( path != null && path.startsWith( "/" ) ) |
99 | |
{ |
100 | 8 | relPath = path.substring( 1 ); |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 24 | relPath = path; |
105 | |
} |
106 | 48 | } |
107 | 4 | } |
108 | |
|
109 | 84 | this.pathUrl = pathURL; |
110 | 84 | this.relativePath = relPath; |
111 | 84 | } |
112 | |
|
113 | |
private static URL buildBaseUrl( final String base ) throws MalformedURLException |
114 | |
{ |
115 | 64 | if ( base == null ) |
116 | |
{ |
117 | 14 | return null; |
118 | |
} |
119 | |
|
120 | |
try |
121 | |
{ |
122 | 50 | return new URL( base ); |
123 | |
} |
124 | 24 | catch ( MalformedURLException e ) |
125 | |
{ |
126 | 24 | return new File( base ).toURI().toURL(); |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
private static URL buildUrl( final URL baseUrl, final String path ) throws MalformedURLException |
131 | |
{ |
132 | 80 | if ( baseUrl == null ) |
133 | |
{ |
134 | 32 | throw new MalformedURLException( "Base is null!" ); |
135 | |
} |
136 | |
|
137 | 48 | if ( path == null ) |
138 | |
{ |
139 | 8 | return baseUrl; |
140 | |
} |
141 | |
|
142 | 40 | if ( baseUrl.getProtocol().equals( "file" ) ) |
143 | |
{ |
144 | 28 | return new File( baseUrl.getFile(), path ).toURI().toURL(); |
145 | |
} |
146 | |
|
147 | 12 | if ( path.startsWith( "/" ) && baseUrl.getPath().endsWith( "/" ) ) |
148 | |
{ |
149 | 4 | return new URL( baseUrl, path.substring( 1 ) ); |
150 | |
} |
151 | |
|
152 | 8 | return new URL( baseUrl, path ); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public boolean isFile() |
161 | |
{ |
162 | 286 | return isRelative() || pathUrl.getProtocol().equals( "file" ); |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public boolean isRelative() |
171 | |
{ |
172 | 370 | return pathUrl == null; |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public URL getBaseUrl() |
181 | |
{ |
182 | 52 | return baseUrl; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public URL getPathUrl() |
191 | |
{ |
192 | 406 | return pathUrl; |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public String getPath() |
201 | |
{ |
202 | 122 | if ( getPathUrl() != null ) |
203 | |
{ |
204 | 80 | if ( isFile() ) |
205 | |
{ |
206 | 56 | return StringUtils.stripEnd( getPathUrl().getPath(), "/" ); |
207 | |
} |
208 | |
else |
209 | |
{ |
210 | 24 | return getPathUrl().getPath(); |
211 | |
} |
212 | |
} |
213 | |
else |
214 | |
{ |
215 | 42 | return relativePath; |
216 | |
} |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public String getLocation() |
225 | |
{ |
226 | 104 | if ( isFile() ) |
227 | |
{ |
228 | 84 | if ( getPathUrl() != null ) |
229 | |
{ |
230 | 44 | return StringUtils.stripEnd( getPathUrl().getFile(), "/" ); |
231 | |
} |
232 | |
else |
233 | |
{ |
234 | 40 | return relativePath; |
235 | |
} |
236 | |
} |
237 | |
else |
238 | |
{ |
239 | 20 | return getPathUrl().toExternalForm(); |
240 | |
} |
241 | |
} |
242 | |
|
243 | |
|
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 | |
} |