1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.tools;
13 import java.io.File;
14 import java.net.MalformedURLException;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.net.URL;
18 /***
19 * Representation of directory, denoted by given URL.
20 * @author Martin Vysny
21 */
22 public final class URLDir {
23 /***
24 * Returns an URL object, that is represented by this object. This URL is
25 * always absolute, and ends with '/'.
26 */
27 private URL url;
28 /***
29 * Returns an URI object, that is represented by this object. This URI is
30 * always absolute, never opaque, and ends with '/'.
31 */
32 private URI uri;
33 /***
34 * Creates instance of object with given url. If URL is relative, then it is
35 * rooted in current directory.
36 * @param url url. Must denote a directory.
37 * @throws MalformedURLException if url is invalid.
38 * @throws URISyntaxException if url is invalid.
39 */
40 public URLDir(String url) throws MalformedURLException, URISyntaxException {
41 this(new URI(url.replaceAll(" ", "%20")));
42 }
43 /***
44 * Creates object pointing onto given directory. The file must point into an
45 * existing directory in the local filesystem.
46 * @param file the file
47 * @return the object pointing into the directory denoted by the file
48 * parameter.
49 */
50 public static URLDir create(File file) {
51 if (!file.isDirectory())
52 throw new IllegalArgumentException(file.toString()
53 + " is not a valid directory");
54 try {
55 return new URLDir(file.toURI().toURL());
56 } catch (MalformedURLException ex) {
57 throw new Error(ex);
58 }
59 }
60 /***
61 * Creates instance of object with given url. If URL is relative, then it is
62 * rooted in current directory.
63 * @param url url. Must denote a directory.
64 */
65 public URLDir(URL url) {
66 super();
67 try {
68 init(toURI(format(url)));
69 } catch (MalformedURLException ex) {
70 throw new Error("Unexpected URL exception.", ex);
71 }
72 }
73 /***
74 * Creates instance of object with given uri. If uri is relative, then it is
75 * rooted in current directory.
76 * @param uri uri. Must denote a directory.
77 * @throws MalformedURLException if given uri is not a valid URI
78 */
79 public URLDir(URI uri) throws MalformedURLException {
80 super();
81 init(uri);
82 }
83 private void init(URI uri) throws MalformedURLException {
84
85
86
87 if (!uri.isAbsolute()) {
88
89 uri = toURI(getCurrentDir()).resolve(uri);
90 }
91 uri = format(uri);
92 this.uri = uri;
93 this.url = new URL(uri.toString());
94 }
95 /***
96 * Creates instance of object with current directory.
97 */
98 public URLDir() {
99 super();
100 url = getCurrentDir();
101 uri = toURI(url);
102 }
103 /***
104 * Correctly formats given URL. If it doesn't end with slash, then trailing
105 * slash is added. Every space is converted to a %20 string.
106 * @param url url to format, should denote a directory.
107 * @return formatted uri.
108 */
109 private static URL format(URL url) {
110 String sUrl = url.toString();
111 boolean changed = false;
112 if (!sUrl.endsWith("/")) {
113 changed = true;
114 sUrl = sUrl + "/";
115 }
116 String sUrlNew = sUrl.replaceAll(" ", "%20");
117 if (!sUrlNew.equals(sUrl)) {
118 changed = true;
119 sUrl = sUrlNew;
120 }
121 return changed ? createURL(sUrl) : url;
122 }
123 /***
124 * Correctly formats given URI. If it doesn't end with slash, then trailing
125 * slash is added.
126 * @param uri uri to format, should denote a directory.
127 * @return formatted uri.
128 */
129 private static URI format(URI uri) {
130 String sUrl = uri.toString();
131 boolean changed = false;
132 if (!sUrl.endsWith("/")) {
133 changed = true;
134 sUrl = sUrl + "/";
135 }
136 String sUrlNew = sUrl.replaceAll(" ", "%20");
137 if (!sUrlNew.equals(sUrl)) {
138 changed = true;
139 sUrl = sUrlNew;
140 }
141 return changed ? URI.create(sUrl) : uri;
142 }
143 /***
144 * Returns current dir, as an URL object.
145 * @return current dir. The URL ends with slash.
146 */
147 public static URL getCurrentDir() {
148 try {
149 return format(new File("").toURL());
150 } catch (MalformedURLException e) {
151 throw new Error("Unexpected URL exception.", e);
152 }
153 }
154 /***
155 * Resolves given uri against this object. See <code>URI.resolve</code>
156 * for details.
157 * @param uri uri to resolve.
158 * @return resolved url
159 * @throws MalformedURLException if given uri is not a valid URI
160 * @throws URISyntaxException if given uri is not a valid URI
161 */
162 public URL resolve(String uri) throws MalformedURLException,
163 URISyntaxException {
164 uri = uri.replaceAll("%", "%25");
165 uri = uri.replaceAll(" ", "%20");
166 return resolve(new URI(uri));
167 }
168 /***
169 * Resolves given uri against this object. See <code>URI.resolve</code>
170 * for details.
171 * @param uri uri to resolve.
172 * @return resolved url
173 * @throws MalformedURLException if given uri is not a valid URI
174 */
175 public URL resolve(URI uri) throws MalformedURLException {
176 return new URL(this.uri.resolve(uri).toString());
177 }
178 /***
179 * Resolves given uri against this object. See <code>URI.resolve</code>
180 * for details.
181 * @param uri uri to resolve. Must be a directory.
182 * @return resolved url
183 * @throws MalformedURLException if given uri is not a valid URI
184 * @throws URISyntaxException if given uri is not a valid URI
185 */
186 public URLDir resolveDir(String uri) throws MalformedURLException,
187 URISyntaxException {
188 return new URLDir(resolve(uri));
189 }
190
191
192
193
194 @Override
195 public String toString() {
196 return url.toString();
197 }
198 /***
199 * From given URL creates an URI instance.
200 * @param url this string will be converted to URI object.
201 * @return URI object, representing given url.
202 */
203 public static URI toURI(URL url) {
204 return URI.create(url.toString());
205 }
206 /***
207 * From given string creates an URL instance. It doesn't expect error, so
208 * <code>MalformedURLException</code> is thrown as an
209 * <code>IllegalArgumentException</code>.
210 * @param url this string will be converted to URL object.
211 * @return URL object, representing given url.
212 */
213 public static URL createURL(String url) {
214 try {
215 return new URL(url);
216 } catch (MalformedURLException ex) {
217 throw new IllegalArgumentException(ex.getMessage());
218 }
219 }
220 /***
221 * Returns this instance as URI.
222 * @return URI representation.
223 */
224 public URI getURI() {
225 return uri;
226 }
227 /***
228 * Returns this instance as URL.
229 * @return URL representation.
230 */
231 public URL getURL() {
232 return url;
233 }
234 /***
235 * Returns the last path part from given url, without the query and fragment
236 * parts.
237 * @param url
238 * @return the file name, or <code>null</code> if none can be obtained.
239 */
240 public static String getFileName(String url) {
241 int lastSlash = url.lastIndexOf('/');
242 if (lastSlash >= 0)
243 url = url.substring(lastSlash + 1);
244 int first = url.indexOf('?');
245 if (first < 0)
246 first = url.indexOf('#');
247 if (first >= 0)
248 url = url.substring(0, first);
249 return StringTools.nullStr(url);
250 }
251 }