KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > URLUtilities


1 package org.jahia.clipbuilder.html.util;
2
3 import java.net.*;
4
5 import java.io.*;
6 import java.util.*;
7 import org.apache.commons.lang.StringUtils;
8
9 /**
10  * Description of the Class
11  *
12  *@author Tlili Khaled
13  */

14 public abstract class URLUtilities {
15     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(URLUtilities.class);
16
17
18     /**
19      * Gets the URL attribute of the URLUtilities object
20      *
21      *@param url Description of Parameter
22      *@return The URL value
23      */

24     public static URL getURL(String JavaDoc url) {
25         URL uurl = null;
26         try {
27             uurl = new URL(url);
28
29         }
30         catch (MalformedURLException ex) {
31             logger.error(" [ Url " + url + " malformed ] ");
32         }
33         return uurl;
34     }
35
36
37     /**
38      * Constructor for the URLUtilities object
39      */

40     /*
41      * public static String getRessourceAbsoluteUrl(String baseUrl, String url) throws WebClippingException {
42      * if (url == null || url.length() == 0) {
43      * logger.warn("[ Url is null or empty string !!]");
44      * throw new WebClippingException("Url to rewrite is null", new Exception("Kind of null exception"));
45      * }
46      * // if relatif --> absolute
47      * if (isRelatif(url)) {
48      * //logger.debug("[ Old Url is: " + url + " ]");
49      * try {
50      * URL baseURL = new URL(baseUrl);
51      * //logger.debug("[ Base Url is: " + baseURL.toString() + " ]");
52      * // add '/'
53      * if (url.charAt(0) != '/') {
54      * url = '/' + url;
55      * }
56      * // set the base
57      * URL u = new URL(baseURL, url);
58      * url = u.toString();
59      * }
60      * catch (MalformedURLException ex) {
61      * logger.error("[Relatif url" + url + " ]");
62      * ex.printStackTrace();
63      * }
64      * }
65      * else {
66      * //logger.debug("[ URL " + url + " is an absolute url ]");
67      * }
68      * //logger.debug("[ New url " + url + " ]");
69      * return url;
70      * }
71      */

72     /**
73      * Gets the HrefAbsoluteValue attribute of the URLUtilities class
74      *
75      *@param baseURL Description of Parameter
76      *@param url Description of Parameter
77      *@return The HrefAbsoluteValue value
78      */

79     public static String JavaDoc getHrefAbsoluteValue(URL baseURL, String JavaDoc url) {
80
81         //logger.debug("[ Get absolute url of: " + url + " ]");
82
try {
83             if (isRelatif(url)) {
84                 return getAbsolutURL(baseURL, url).toExternalForm();
85             }
86             else {
87                 return url;
88             }
89         }
90         catch (MalformedURLException ex) {
91             logger.error("[ Malformed url ]");
92             return url;
93         }
94     }
95
96
97     /**
98      * Gets the HrefAbsoluteValue attribute of the URLUtilities class
99      *
100      *@param baseUrl Description of Parameter
101      *@param url Description of Parameter
102      *@return The HrefAbsoluteValue value
103      *@exception MalformedURLException Description of Exception
104      */

105     public static String JavaDoc getUrlAbsoluteValue(String JavaDoc baseUrl, String JavaDoc url) throws MalformedURLException {
106         logger.debug("[ Get absolute url of: " + url + " ]");
107         logger.debug("[ Base url: " + baseUrl + " ]");
108         String JavaDoc res = getAbsolutURL(getURL(baseUrl), url).toExternalForm();
109
110         logger.debug("[ Result: " + res + " ]");
111         return res;
112     }
113
114
115     /**
116      * test if its a relatif url or not
117      *
118      *@param url Description of Parameter
119      *@return The Relatif value
120      */

121     public static boolean isRelatif(String JavaDoc url) {
122         try {
123             new URL(url);
124             return false;
125         }
126         catch (MalformedURLException ex) {
127             return true;
128         }
129
130     }
131
132
133     /**
134      * relative Url --> absolute URL
135      *
136      *@param baseURL Description of Parameter
137      *@param relativeUrl Description of Parameter
138      *@return Description of the Returned Value
139      *@exception MalformedURLException Description of Exception
140      */

141     public static URL getAbsolutURL(final URL baseURL, String JavaDoc relativeUrl) throws MalformedURLException {
142         if (relativeUrl == null) {
143             logger.error("relative Url is null");
144             logger.error("Returned value: " + baseURL.toExternalForm());
145             return baseURL;
146         }
147         if (relativeUrl.indexOf("//") == 0) {
148             // urls like: //slashdot.org
149
return getURL("http:" + relativeUrl);
150         }
151         return com.gargoylesoftware.htmlunit.WebClient.expandUrl(baseURL, relativeUrl);
152     }
153
154
155     /**
156      * Url --> {path of the url , query of the url}
157      *
158      *@param url Description of Parameter
159      *@return Description of the Returned Value
160      */

161     public static String JavaDoc[] splitQuery(String JavaDoc url) {
162         String JavaDoc[] result = {"", ""};
163
164         // looking gor the query
165
int queryIndex = url.indexOf('?');
166         if (queryIndex > 1) {
167             //rsplit in to part
168
result[0] = url.substring(0, queryIndex);
169             result[1] = url.substring(queryIndex + 1);
170             /*
171              * StringTokenizer st = new StringTokenizer(url, "?");
172              * int i = 0;
173              * while (st.hasMoreTokens()) {
174              * result[i] = st.nextToken();
175              * i++;
176              * }
177              */

178         }
179         else {
180             // there is no query
181
result[0] = url;
182         }
183         //logger.debug("[ URL is: " + url + " ]");
184
//logger.debug("[ Path is: " + result[0] + " ]");
185
//logger.debug("[ Query is: " + result[1] + " ]");
186

187         return result;
188     }
189
190
191     /**
192      * Description of the Method
193      *
194      *@param url Description of Parameter
195      *@return Description of the Returned Value
196      */

197     public static String JavaDoc encode(String JavaDoc url) {
198         url = StringUtils.replaceChars(url, "/", "_");
199         url = StringUtils.replaceChars(url, "&", "_");
200         url = StringUtils.replaceChars(url, "+", "_");
201         url = StringUtils.replaceChars(url, "#", "_");
202         return url;
203     }
204
205
206
207     /**
208      * Extract the parameters from an url
209      *
210      *@param urlValue Description of Parameter
211      *@return Description of the Returned Value
212      *@exception IOException Description of Exception
213      */

214     public static ArrayList parseUrl(String JavaDoc urlValue) throws IOException {
215         //Set the parameter from the Url
216
ArrayList param = new ArrayList();
217         URL url = null;
218         try {
219             url = new URL(urlValue);
220         }
221         catch (IOException ex) {
222             ex.printStackTrace();
223         }
224
225         //Determine the uri part
226
String JavaDoc uri = url.getFile();
227
228         //the query part
229
String JavaDoc query = StringUtilities.replaceSpecialGraphics(url.getQuery());
230
231         if (query != null) {
232             //param1=value1&param2=value2...
233
String JavaDoc[] queryParam = query.split("&");
234             for (int i = 0; i < queryParam.length; i++) {
235                 // param1=value1
236
String JavaDoc nameAndValue = queryParam[i];
237                 int indexEqual = nameAndValue.indexOf('=');
238                 try {
239                     String JavaDoc name = URLDecoder.decode(nameAndValue.substring(0, indexEqual), "UTF-8");
240                     String JavaDoc value = URLDecoder.decode(nameAndValue.substring(indexEqual +
241                             1), "UTF-8");
242                     String JavaDoc[] p = {name, value};
243                     param.add(p);
244                 }
245                 catch (UnsupportedEncodingException exc) {
246                     exc.printStackTrace();
247                 }
248             }
249         }
250         return param;
251     }
252
253
254     /**
255      * Gets the Path attribute of the URLUtilities class
256      *
257      *@param url Description of Parameter
258      *@return The Path value
259      */

260     private static String JavaDoc getPath(String JavaDoc url) {
261         String JavaDoc path = url;
262         int lastIndexSeparator = url.lastIndexOf('/');
263         if (lastIndexSeparator > 0) {
264             path = url.substring(0, lastIndexSeparator);
265         }
266         logger.debug("[ Path is: " + path + " ]");
267         return path;
268     }
269
270 }
271
Popular Tags