1 11 package org.eclipse.ui.internal.intro.impl.model.url; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.UnsupportedEncodingException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Properties ; 18 19 import org.eclipse.ui.internal.intro.impl.util.Log; 20 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 21 22 26 public class IntroURLParser { 27 28 private boolean hasProtocol = false; 30 private boolean isIntroUrl = false; 31 32 private URL url_inst; 33 34 37 public IntroURLParser(String url) { 38 parseUrl(url); 40 } 41 42 private void parseUrl(String url) { 43 if (url == null) 44 return; 45 url_inst = null; 46 try { 47 url_inst = new URL (url); 48 } catch (MalformedURLException e) { 49 return; 51 } 52 53 if (url_inst.getProtocol() != null) { 54 hasProtocol = true; 56 isIntroUrl = isIntroUrl(url_inst); 57 return; 58 } 59 60 return; 62 } 63 64 65 68 public boolean hasProtocol() { 69 return hasProtocol; 70 } 71 72 75 public boolean hasIntroUrl() { 76 return isIntroUrl; 77 } 78 79 80 83 public String getProtocol() { 84 return url_inst.getProtocol(); 85 } 86 87 88 91 public String getHost() { 92 return url_inst.getHost(); 93 } 94 95 96 104 private boolean isIntroUrl(URL url) { 105 if (!url.getProtocol().equalsIgnoreCase(IntroURL.INTRO_PROTOCOL)) 106 return false; 108 109 if (url.getHost().equalsIgnoreCase(IntroURL.INTRO_HOST_ID)) 110 return true; 111 112 return false; 113 } 114 115 116 117 121 public IntroURL getIntroURL() { 122 IntroURL introURL = null; 123 if (isIntroUrl) { 124 String action = getPathAsAction(url_inst); 126 Properties parameters = getQueryParameters(url_inst); 127 128 introURL = new IntroURL(action, parameters); 130 } 131 return introURL; 132 } 133 134 135 136 143 private String getPathAsAction(URL url) { 144 String action = url.getPath(); 146 if (action != null) 148 action = action.substring(1); 149 return action; 150 } 151 152 158 public Properties getQueryParameters(URL url) { 159 Properties properties = new Properties (); 161 String query = url.getQuery(); 162 if (query == null) 163 return properties; 166 167 String [] params = StringUtil.split(query, "&"); for (int i = 0; i < params.length; i++) { 170 String [] keyValuePair = StringUtil.split(params[i], "="); if (keyValuePair.length != 2) { 175 Log.warning("Ignoring the following Intro URL parameter: " + params[i]); 177 continue; 178 } 179 180 String key = urlDecode(keyValuePair[0]); 181 if (key == null) { 182 Log.warning("Failed to URL decode key: " + keyValuePair[0]); continue; 184 } 185 186 String value = urlDecode(keyValuePair[1]); 187 if (value == null) { 188 Log.warning("Failed to URL decode value: " + keyValuePair[1]); continue; 190 } 191 192 properties.setProperty(key, value); 193 } 194 return properties; 195 } 196 197 198 201 private static String urlDecode(String encodedURL) { 202 int len = encodedURL.length(); 203 ByteArrayOutputStream os = new ByteArrayOutputStream (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 '+': os.write(' '); 216 i++; 217 break; 218 default: 219 os.write(encodedURL.charAt(i++)); 220 break; 221 } 222 } 223 return new String (os.toByteArray(), "UTF8"); } catch (UnsupportedEncodingException ex) { 225 return null; 226 } 227 } 228 229 } 230 | Popular Tags |