1 package org.jahia.urls; 2 3 import java.util.Map ; 4 import java.util.StringTokenizer ; 5 import org.jahia.utils.*; 6 7 22 23 public class QueryMapURL extends URI { 24 25 protected InsertionSortedMap queryParameters = new InsertionSortedMap(); 26 27 32 public QueryMapURL() { 33 } 34 35 46 public QueryMapURL(String url) 47 throws java.net.MalformedURLException { 48 setURI(url); 49 parseQueryParameters(super.getQueryString()); 50 } 51 52 56 public QueryMapURL(java.net.URL javaURL) { 57 setURI(javaURL); 58 parseQueryParameters(super.getQueryString()); 59 } 60 61 62 74 public void setQueryParameter(String name, String value) { 75 queryParameters.put(name, value); 76 } 77 78 90 public String getQueryParameter(String name) { 91 return (String ) queryParameters.get(name); 92 } 93 94 99 public void removeQueryParameter(String name) { 100 queryParameters.remove(name); 101 } 102 103 107 public boolean containsQueryParameter(String name) { 108 return queryParameters.containsKey(name); 109 } 110 111 private void parseQueryParameters(String queryString) { 112 if (queryString == null) { 113 return; 114 } 115 StringTokenizer tokenizer = new StringTokenizer (queryString, "&"); 116 while (tokenizer.hasMoreTokens()) { 117 String curToken = tokenizer.nextToken(); 118 int equalPos = curToken.indexOf("="); 119 if (equalPos == -1) { 120 queryParameters.put(curToken, null); 121 } else { 122 String curName = curToken.substring(0, equalPos); 123 String curValue = curToken.substring(equalPos+1); 124 queryParameters.put(curName, curValue); 125 } 126 } 127 } 128 129 public String getQueryString () { 130 if (queryParameters.size() == 0) { 131 return null; 132 } 133 StringBuffer result = new StringBuffer (); 134 java.util.Iterator parameterIter = queryParameters.entrySet().iterator(); 135 while (parameterIter.hasNext()) { 136 Map.Entry curEntry = (Map.Entry ) parameterIter.next(); 137 result.append(curEntry.getKey()); 138 result.append("="); 139 result.append(curEntry.getValue()); 140 if (parameterIter.hasNext()) { 141 result.append("&"); 142 } 143 } 144 return result.toString(); 145 } 146 147 public void setQueryString(String queryString) { 148 super.setQueryString(queryString); 149 parseQueryParameters(queryString); 150 } 151 152 } | Popular Tags |