KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > UrlPathTokenizer


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.util;
17
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24 /**
25  * Parses URIs string into tokens and returns reolved URLs as array.
26  * <p>; and : are used as separators. This class is simlar to Ant's PathTokenizer but has
27  * 2 important differences:
28  * <ul>
29  * <li>List of URLs instead of Paths
30  * <li>This implementation does not take into accout the operating system we are currently running on
31  * </ul>
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public class UrlPathTokenizer {
37     //Path separator regexp,
38
// \\s* - means extra whitespaces
39
// \\:(?=[^/]{2} - means do not treat protocol:// as two paths - protocol and //
40
private static final Pattern JavaDoc SEPARATOR = Pattern.compile("\\s*(\\;|(\\:(?=[^/]{2})))\\s*");
41
42
43     private final URL JavaDoc baseURL;
44
45     public UrlPathTokenizer(URL JavaDoc baseURL) {
46         this.baseURL = baseURL;
47     }
48
49     /**
50      * Splits a string with set of URIs into array of URLs resolved relatively to baseURL.
51      *
52      * @param urls not null string with ; or : separated URIs.
53      * @return array of resolved URLs.
54      * @throws MalformedURLException if urls contain a malformed URI or URL
55      */

56     public URL JavaDoc[] split(String JavaDoc urls) throws MalformedURLException JavaDoc {
57         if (urls == null) {
58             throw new IllegalArgumentException JavaDoc("urls cannot be null");
59         }
60         String JavaDoc[] strings = SEPARATOR.split(urls);
61         List JavaDoc<URL JavaDoc> res = new ArrayList JavaDoc<URL JavaDoc>(strings.length);
62         for (String JavaDoc s : strings) {
63             String JavaDoc u = s.trim();
64             if (u.length() > 0) {
65                 res.add(new URL JavaDoc(baseURL, u));
66             }
67         }
68         return res.toArray(new URL JavaDoc[res.size()]);
69     }
70
71 }
72
Popular Tags