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 )
53 throws MalformedURLException
54 {
55 this( (URL) null, path );
56 }
57
58
59
60
61
62
63
64
65 public PathDescriptor( final String base, final String path )
66 throws MalformedURLException
67 {
68 this( PathDescriptor.buildBaseUrl( base ), path );
69 }
70
71
72
73
74
75
76
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
99
100
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
162
163
164
165 public boolean isFile()
166 {
167 return isRelative() || pathUrl.getProtocol().equals( "file" );
168 }
169
170
171
172
173
174
175 public boolean isRelative()
176 {
177 return pathUrl == null;
178 }
179
180
181
182
183
184
185 public URL getBaseUrl()
186 {
187 return baseUrl;
188 }
189
190
191
192
193
194
195 public URL getPathUrl()
196 {
197 return pathUrl;
198 }
199
200
201
202
203
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
226
227
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
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 }