KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > url > IntroURLParser


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.model.url;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import org.eclipse.ui.internal.intro.impl.util.Log;
20 import org.eclipse.ui.internal.intro.impl.util.StringUtil;
21
22 /**
23  * A parser that knows how to parser OOBE action URLs. If URL is a valid intro
24  * url, it will create an instance of the IntroURL class.
25  */

26 public class IntroURLParser {
27
28     // private String url_string = null;
29
private boolean hasProtocol = false;
30     private boolean isIntroUrl = false;
31
32     private URL JavaDoc url_inst;
33
34     /**
35      * Constructor that gets the URL to parse.
36      */

37     public IntroURLParser(String JavaDoc url) {
38         // create a URL instance, and parser it for parameters.
39
parseUrl(url);
40     }
41
42     private void parseUrl(String JavaDoc url) {
43         if (url == null)
44             return;
45         url_inst = null;
46         try {
47             url_inst = new URL JavaDoc(url);
48         } catch (MalformedURLException JavaDoc e) {
49             // not a valid URL. set state.
50
return;
51         }
52
53         if (url_inst.getProtocol() != null) {
54             // URL has some valid protocol. Check to see if it is an intro url.
55
hasProtocol = true;
56             isIntroUrl = isIntroUrl(url_inst);
57             return;
58         }
59
60         // not an Intro URL. do nothing.
61
return;
62     }
63
64
65     /**
66      * @return Returns the hasProtocol.
67      */

68     public boolean hasProtocol() {
69         return hasProtocol;
70     }
71
72     /**
73      * @return Returns the isIntroUrl.
74      */

75     public boolean hasIntroUrl() {
76         return isIntroUrl;
77     }
78
79
80     /**
81      * @return Returns the currebt url Protocol.
82      */

83     public String JavaDoc getProtocol() {
84         return url_inst.getProtocol();
85     }
86
87
88     /**
89      * @return Returns the currebt url Protocol.
90      */

91     public String JavaDoc getHost() {
92         return url_inst.getHost();
93     }
94
95
96     /**
97      * Checks to see if tha passed URL is an Intro URL. An intro URL is an http
98      * URL that has the intro plugin id as a host. eg:
99      * "http://org.eclipse.ui.intro/test".
100      *
101      * @param url
102      * @return true if url is an intro URL.
103      */

104     private boolean isIntroUrl(URL JavaDoc url) {
105         if (!url.getProtocol().equalsIgnoreCase(IntroURL.INTRO_PROTOCOL))
106             // quick exit. If it is not http, url is not an Intro url.
107
return false;
108
109         if (url.getHost().equalsIgnoreCase(IntroURL.INTRO_HOST_ID))
110             return true;
111
112         return false;
113     }
114
115
116
117     /**
118      * @return Returns the introURL. Will be null if the parsed URL is not an
119      * Intro URL.
120      */

121     public IntroURL getIntroURL() {
122         IntroURL introURL = null;
123         if (isIntroUrl) {
124             // valid intro URL. Extract the action and parameters.
125
String JavaDoc action = getPathAsAction(url_inst);
126             Properties JavaDoc parameters = getQueryParameters(url_inst);
127
128             // class instance vars are already populated by now.
129
introURL = new IntroURL(action, parameters);
130         }
131         return introURL;
132     }
133
134
135
136     /**
137      * Retruns the path attribute of the passed URL, stripped out of the leading
138      * "/". Returns null if the url does not have a path.
139      *
140      * @param url
141      * @return
142      */

143     private String JavaDoc getPathAsAction(URL JavaDoc url) {
144         // get possible action.
145
String JavaDoc action = url.getPath();
146         // remove leading "/" from path.
147
if (action != null)
148             action = action.substring(1);
149         return action;
150     }
151
152     /**
153      * Retruns the Query part of the URL as an instance of a Properties class.
154      *
155      * @param url
156      * @return
157      */

158     public Properties JavaDoc getQueryParameters(URL JavaDoc url) {
159         // parser all query parameters.
160
Properties JavaDoc properties = new Properties JavaDoc();
161         String JavaDoc query = url.getQuery();
162         if (query == null)
163             // we do not have any parameters in this URL, return an empty
164
// Properties instance.
165
return properties;
166
167         // now extract the key/value pairs from the query.
168
String JavaDoc[] params = StringUtil.split(query, "&"); //$NON-NLS-1$
169
for (int i = 0; i < params.length; i++) {
170             // for every parameter, ie: key=value pair, create a property
171
// entry. we know we have the key as the first string in the array,
172
// and the value as the second array.
173
String JavaDoc[] keyValuePair = StringUtil.split(params[i], "="); //$NON-NLS-1$
174
if (keyValuePair.length != 2) {
175                 Log.warning("Ignoring the following Intro URL parameter: " //$NON-NLS-1$
176
+ params[i]);
177                 continue;
178             }
179             
180             String JavaDoc key = urlDecode(keyValuePair[0]);
181             if (key == null) {
182                 Log.warning("Failed to URL decode key: " + keyValuePair[0]); //$NON-NLS-1$
183
continue;
184             }
185
186             String JavaDoc value = urlDecode(keyValuePair[1]);
187             if (value == null) {
188                 Log.warning("Failed to URL decode value: " + keyValuePair[1]); //$NON-NLS-1$
189
continue;
190             }
191             
192             properties.setProperty(key, value);
193         }
194         return properties;
195     }
196
197
198     /*
199      * Note: This was copied and adapted from org.eclipse.help.internal.util.URLCoder
200      */

201     private static String JavaDoc urlDecode(String JavaDoc encodedURL) {
202         int len = encodedURL.length();
203         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc(len);
204
205         try {
206             for (int i = 0; i < len;) {
207                 switch (encodedURL.charAt(i)) {
208                 case '%':
209                     if (len >= i + 3) {
210                         os.write(Integer.parseInt(encodedURL.substring(i + 1, i + 3), 16));
211                     }
212                     i += 3;
213                     break;
214                 case '+': // exception from standard
215
os.write(' ');
216                     i++;
217                     break;
218                 default:
219                     os.write(encodedURL.charAt(i++));
220                     break;
221                 }
222             }
223             return new String JavaDoc(os.toByteArray(), "UTF8"); //$NON-NLS-1$
224
} catch (UnsupportedEncodingException JavaDoc ex) {
225             return null;
226         }
227     }
228
229 }
230
Popular Tags