1 37 package net.thauvin.google; 38 39 import java.net.URLEncoder ; 40 41 import java.util.Enumeration ; 42 43 import javax.servlet.ServletRequest ; 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.jsp.JspTagException ; 46 import javax.servlet.jsp.PageContext ; 47 import javax.servlet.jsp.tagext.BodyContent ; 48 49 50 58 public class TagUtility 59 { 60 63 public static final String CACHE_PARAM = "cache"; 64 65 68 public static final String FILTER_PARAM = "filter"; 69 70 73 public static final String GOOGLE_PROXY_HOST = "google_proxy_host"; 74 75 78 public static final String GOOGLE_PROXY_PASSWORD = "google_proxy_password"; 79 80 83 public static final String GOOGLE_PROXY_PORT = "google_proxy_port"; 84 85 88 public static final String GOOGLE_PROXY_USERNAME = "google_proxy_username"; 89 90 93 public static final String GOOGLE_SEARCH_BEAN = "GoogleSearchBean"; 94 95 98 public static final String IE_PARAM = "ie"; 99 100 103 public static final String KEY_CONTEXT_PARAM = "google_key"; 104 105 108 public static final String KEY_PARAM = "key"; 109 110 113 public static final String LR_PARAM = "lr"; 114 115 118 public static final String MAX_RESULTS_PARAM = "maxResults"; 119 120 123 public static final String OE_PARAM = "oe"; 124 125 128 public static final String QUERY_PARAM = "q"; 129 130 133 public static final String RESTRICT_PARAM = "restrict"; 134 135 138 public static final String SAFE_SEARCH_PARAM = "safeSearch"; 139 140 143 public static final String SITE_PARAM = "site"; 144 145 148 public static final String START_PARAM = "start"; 149 150 153 public static final String TYPE_PARAM = "type"; 154 155 158 protected TagUtility() 159 { 160 } 162 163 169 public static final String getTagBody(BodyContent bodyContent) 170 { 171 return getTagBody(bodyContent, false); 172 } 173 174 181 public static final String getTagBody(BodyContent bodyContent, boolean trim) 182 { 183 try 184 { 185 if (trim) 186 { 187 return bodyContent.getString().trim(); 188 } 189 else 190 { 191 return bodyContent.getString(); 192 } 193 } 194 catch (NullPointerException e) 195 { 196 return null; 197 } 198 } 199 200 210 public static final boolean isValidString(String stringValue, boolean trim) 211 { 212 if ((stringValue != null)) 213 { 214 if (trim) 215 { 216 return isValidString(stringValue.trim()); 217 } 218 else 219 { 220 return isValidString(stringValue); 221 } 222 } 223 224 return false; 225 } 226 227 233 public static final boolean isValidString(String stringValue) 234 { 235 if ((stringValue != null) && (stringValue.length() > 0)) 236 { 237 return true; 238 } 239 240 return false; 241 } 242 243 250 public static GoogleSearchBean getGoogleSearchBean(PageContext pageContext) 251 { 252 return ((GoogleSearchBean)pageContext.findAttribute(GOOGLE_SEARCH_BEAN)); 254 } 255 256 263 public static String getParameter(ServletRequest request, String paramName) 264 { 265 String paramValue = null; 267 268 if (isValidString(paramName)) 269 { 270 final Enumeration names = request.getParameterNames(); 272 273 while (names.hasMoreElements()) 275 { 276 final String name = (String )names.nextElement(); 278 279 if (name.equalsIgnoreCase(paramName)) 281 { 282 paramValue = request.getParameter(name); 284 } 285 } 286 } 287 288 return paramValue; 290 } 291 292 304 public static final String buildRefLink(String url, String body, 305 String target, String style, 306 String css) 307 { 308 final StringBuffer refLink = new StringBuffer (); 309 310 refLink.append("<A HREF=\"").append(url).append('"'); 312 313 if (TagUtility.isValidString(target)) 315 { 316 refLink.append(" TARGET=\"").append(target).append('"'); 318 } 319 320 if (TagUtility.isValidString(css)) 321 { 322 refLink.append(" CLASS=\"").append(css).append('"'); 324 } 325 326 if (TagUtility.isValidString(style)) 327 { 328 refLink.append(" STYLE=\"").append(style).append('"'); 330 } 331 332 refLink.append('>').append(body).append("</A>"); 334 335 return (refLink.toString()); 336 } 337 338 345 public static final JspTagException misplacedError(String tag, 346 String container) 347 { 348 return new JspTagException ("The '" + tag 349 + "' tag must be located within the '" 350 + container + "' container tag."); 351 } 352 353 360 public static final JspTagException misplacedError(String tag) 361 { 362 return new JspTagException ("The '" + tag 363 + "' tag must be located within a valid container tag."); 364 } 365 366 373 public static final String nameValuePair(String name, String value) 374 { 375 return (URLEncoder.encode(name) + "=" + URLEncoder.encode(value)); 376 } 377 378 394 public static final JspTagException nestedException(String msg, 395 Exception old) 396 { 397 return new JspTagException (msg + "\n nested exception is: \n\t" 398 + old.toString()); 399 } 400 401 408 public static final JspTagException outputError(String tag, Exception e) 409 { 410 StringBuffer buff = 411 new StringBuffer ("An error occurred while processing the '" + tag 412 + "' tag."); 413 414 if (e != null) 415 { 416 buff.append("\n\nException:\n"); 417 } 418 419 if (e != null) 420 { 421 buff.append("\n nested exception is: \n\t" 422 + e.getClass().getName() + ": " 423 + e.getLocalizedMessage()); 424 } 425 426 return new JspTagException (buff.toString()); 427 } 428 429 437 public static final String requestParamsToUrl(HttpServletRequest request, 438 String remove) 439 { 440 final StringBuffer buff = new StringBuffer (); 441 final Enumeration names = request.getParameterNames(); 442 443 if (names.hasMoreElements()) 445 { 446 while (names.hasMoreElements()) 448 { 449 final String name = (String )names.nextElement(); 451 452 if ((remove == null) || (!remove.equals(name))) 454 { 455 final String values[] = request.getParameterValues(name); 457 458 for (int i = 0; i < values.length; i++) 460 { 461 if (buff.length() > 0) 463 { 464 buff.append("&"); 466 } 467 468 buff.append(nameValuePair(name, values[i])); 470 } 471 } 472 } 473 } 474 475 return (buff.toString()); 476 } 477 } 478 | Popular Tags |