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 this( (URL) null, path );
55 }
56
57
58
59
60
61
62
63
64 public PathDescriptor( final String base, final String path ) throws MalformedURLException
65 {
66 this( PathDescriptor.buildBaseUrl( base ), path );
67 }
68
69
70
71
72
73
74
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
96
97
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
157
158
159
160 public boolean isFile()
161 {
162 return isRelative() || pathUrl.getProtocol().equals( "file" );
163 }
164
165
166
167
168
169
170 public boolean isRelative()
171 {
172 return pathUrl == null;
173 }
174
175
176
177
178
179
180 public URL getBaseUrl()
181 {
182 return baseUrl;
183 }
184
185
186
187
188
189
190 public URL getPathUrl()
191 {
192 return pathUrl;
193 }
194
195
196
197
198
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
221
222
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
244 public String toString()
245 {
246 StringBuffer res =
247 new StringBuffer( ( StringUtils.isNotEmpty( relativePath ) ) ? relativePath : String.valueOf( pathUrl ) );
248 res.append( " (Base: " ).append( baseUrl ).append( ") Location: " ).append( getLocation() );
249 return res.toString();
250 }
251 }