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 public class PathDescriptor
38 {
39 private final URL baseUrl;
40
41 private final URL pathUrl;
42
43 private final String relativePath;
44
45
46
47
48
49
50
51 public PathDescriptor( final String path )
52 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 )
65 throws MalformedURLException
66 {
67 this( PathDescriptor.buildBaseUrl( base ), path );
68 }
69
70
71
72
73
74
75
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
98
99
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
161
162
163
164 public boolean isFile()
165 {
166 return isRelative() || pathUrl.getProtocol().equals( "file" );
167 }
168
169
170
171
172
173
174 public boolean isRelative()
175 {
176 return pathUrl == null;
177 }
178
179
180
181
182
183
184 public URL getBaseUrl()
185 {
186 return baseUrl;
187 }
188
189
190
191
192
193
194 public URL getPathUrl()
195 {
196 return pathUrl;
197 }
198
199
200
201
202
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
225
226
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
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 }