KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.ui.internal.intro.impl.model;
12
13 import java.net.*;
14 import java.util.*;
15
16 /**
17  * A parser that knows how to parser OOBE action URLs. If URL is a valid intro
18  * url, it will create an instance of the IntroURL class.
19  */

20 public class IntroURLParser {
21
22     private String JavaDoc url_string = null;
23     private boolean hasProtocol = false;
24     private boolean isIntroUrl = false;
25     private String JavaDoc action = null;
26     private Properties parameters = null;
27
28     private IntroURL introURL = null;
29
30     /**
31      * Constructor that gets the URL to parse.
32      */

33     public IntroURLParser(String JavaDoc url) {
34         url_string = url;
35         // create a URL instance, and parser it for parameters.
36
parseUrl(url);
37         if (isIntroUrl) {
38             // class instance vars are already populated by now.
39
introURL = new IntroURL(action, parameters);
40         }
41     }
42
43     private void parseUrl(String JavaDoc url) {
44         if (url == null)
45             return;
46         URL url_inst = null;
47         try {
48             url_inst = new URL(url);
49         } catch (MalformedURLException e) {
50             // not a valid URL. set state.
51
return;
52         }
53
54         if (url_inst.getProtocol() != null) {
55             // URL has some valid protocol. Check to see if it is an intro
56
// url.
57
hasProtocol = true;
58             isIntroUrl = isIntoUrl(url_inst);
59             if (isIntroUrl) {
60                 // valid intro URL. Extract the action and parameters.
61
action = getPathAsAction(url_inst);
62                 parameters = getQueryParameters(url_inst);
63             }
64             return;
65         }
66
67         // not an Intro URL. do nothing.
68
return;
69     }
70
71     /**
72      * Checks to see if tha passed URL is an Intro URL. An intro URL is an http
73      * URL that has the intro plugin id as a host. eg:
74      * "http://org.eclipse.ui.intro/test".
75      *
76      * @param url
77      * @return true if url is an intro URL.
78      */

79     private boolean isIntoUrl(URL url) {
80         if (!url.getProtocol().equalsIgnoreCase(IntroURL.INTRO_PROTOCOL))
81             // quick exit. If it is not http, url is not an Intro url.
82
return false;
83
84         if (url.getHost().equalsIgnoreCase(IntroURL.INTRO_HOST_ID))
85             return true;
86         return false;
87     }
88
89     /**
90      * Retruns the path attribute of the passed URL, stripped out of the leading
91      * "/". Returns null if the url does not have a path.
92      *
93      * @param url
94      * @return
95      */

96     private String JavaDoc getPathAsAction(URL url) {
97         // get possible action.
98
String JavaDoc action = url.getPath();
99         // remove leading "/" from path.
100
if (action != null)
101             action = action.substring(1);
102         return action;
103     }
104
105     /**
106      * Retruns the Query part of the URL as an instance of a Properties class.
107      *
108      * @param url
109      * @return
110      */

111     public Properties getQueryParameters(URL url) {
112         // parser all query parameters.
113
Properties properties = new Properties();
114         String JavaDoc query = url.getQuery();
115         if (query == null)
116             // we do not have any parameters in this URL, return an empty
117
// Properties instance.
118
return properties;
119
120         // now extract the key/value pairs from the query.
121
String JavaDoc[] params = query.split("&"); //$NON-NLS-1$
122
for (int i = 0; i < params.length; i++) {
123             // for every parameter, ie: key=value pair, create a property
124
// entry. we know we have the key as the first string in the array,
125
// and the value as the second array.
126
String JavaDoc[] keyValuePair = params[i].split("="); //$NON-NLS-1$
127
properties.setProperty(keyValuePair[0], keyValuePair[1]);
128         }
129         return properties;
130     }
131
132     /**
133      * @return Returns the hasProtocol.
134      */

135     public boolean hasProtocol() {
136         return hasProtocol;
137     }
138
139     /**
140      * @return Returns the isIntroUrl.
141      */

142     public boolean hasIntroUrl() {
143         return isIntroUrl;
144     }
145
146     /**
147      * @return Returns the introURL. Will be null if the parsed URL is not an
148      * Intro URL.
149      */

150     public IntroURL getIntroURL() {
151         return introURL;
152     }
153
154 }
155
Popular Tags