1 8 package org.roller.presentation.tags; 9 import java.lang.reflect.InvocationTargetException ; 10 import java.net.MalformedURLException ; 11 import java.net.URLEncoder ; 12 import java.util.Map ; 13 import javax.servlet.jsp.JspException ; 14 import org.apache.commons.beanutils.PropertyUtils; 15 import org.apache.log4j.Category; 16 import org.apache.struts.taglib.TagUtils; 17 import org.apache.struts.util.RequestUtils; 18 import org.apache.struts.util.ResponseUtils; 19 20 45 public class LinkTag extends org.apache.struts.taglib.html.LinkTag 46 { 47 static Category cat = Category.getInstance(LinkTag.class); 49 53 private StringBuffer hrefURL = new StringBuffer (); 54 55 63 public int doStartTag() throws JspException 64 { 65 if (linkName != null) 67 { 68 StringBuffer results = new StringBuffer ("<a name=\""); 69 results.append(linkName); 70 results.append("\">"); 71 return (EVAL_BODY_TAG); 72 } 73 Map params = RequestUtils.computeParameters(pageContext, paramId, 75 paramName, paramProperty, paramScope, name, property, 76 scope, transaction); 77 String url = null; 78 try 79 { 80 url = RequestUtils.computeURL(pageContext, forward, href, page, 81 params, anchor, false); 82 } 83 catch (MalformedURLException e) 84 { 85 RequestUtils.saveException(pageContext, e); 86 throw new JspException (messages.getMessage("rewrite.url", e 87 .toString())); 88 } 89 hrefURL = new StringBuffer ("<a HREF=\""); 91 hrefURL.append(url); 92 if (cat.isDebugEnabled()) 93 cat.debug("hrefURL = '" + hrefURL.toString()); 94 this.text = null; 96 return (EVAL_BODY_TAG); 97 } 98 99 107 public void addRequestParameter(String name, String value) 108 { 109 if (cat.isDebugEnabled()) 110 cat.debug("Adding '" + name + "' with value '" + value + "'"); 111 boolean question = (hrefURL.toString().indexOf('?') >= 0); 112 if (question) 113 { hrefURL.append('&'); 115 } 116 else 117 hrefURL.append('?'); 118 hrefURL.append(name); 119 hrefURL.append('='); 120 hrefURL.append(URLEncoder.encode(value)); 121 if (cat.isDebugEnabled()) 122 cat.debug("hrefURL = '" + hrefURL.toString() + "'"); 123 } 124 125 131 public int doEndTag() throws JspException 132 { 133 hrefURL.append("\""); 134 if (target != null) 135 { 136 hrefURL.append(" target=\""); 137 hrefURL.append(target); 138 hrefURL.append("\""); 139 } 140 hrefURL.append(prepareStyles()); 141 hrefURL.append(prepareEventHandlers()); 142 hrefURL.append(">"); 143 if (text != null) 144 hrefURL.append(text); 145 hrefURL.append("</a>"); 146 if (cat.isDebugEnabled()) 147 cat.debug("Total request is = '" + hrefURL.toString() + "'"); 148 ResponseUtils.write(pageContext, hrefURL.toString()); 150 return (EVAL_PAGE); 151 } 152 153 156 public void release() 157 { 158 super.release(); 159 forward = null; 160 href = null; 161 name = null; 162 property = null; 163 target = null; 164 } 165 166 174 protected String hyperlink() throws JspException 175 { 176 String href = this.href; 177 if ((property != null) && (name == null)) 195 throw new JspException (messages.getMessage("getter.name")); 196 if (name == null) 197 return (href); 198 Object bean = pageContext.findAttribute(name); 200 if (bean == null) 201 throw new JspException (messages.getMessage("getter.bean", name)); 202 Map map = null; 203 if (property == null) 204 { 205 try 206 { 207 map = (Map ) bean; 208 } 209 catch (ClassCastException e) 210 { 211 throw new JspException (messages.getMessage("linkTag.type")); 212 } 213 } 214 else 215 { 216 try 217 { 218 map = (Map ) PropertyUtils.getProperty(bean, property); 219 if (map == null) 220 throw new JspException (messages.getMessage( 221 "getter.property", property)); 222 } 223 catch (IllegalAccessException e) 224 { 225 throw new JspException (messages.getMessage("getter.access", 226 property, name)); 227 } 228 catch (InvocationTargetException e) 229 { 230 Throwable t = e.getTargetException(); 231 throw new JspException (messages.getMessage("getter.result", 232 property, t.toString())); 233 } 234 catch (ClassCastException e) 235 { 236 throw new JspException (messages.getMessage("linkTag.type")); 237 } 238 catch (NoSuchMethodException e) 239 { 240 throw new JspException (messages.getMessage("getter.method", 241 property, name)); 242 } 243 } 244 try 286 { 287 return TagUtils.getInstance().computeURL( 288 pageContext, 289 forward, 290 href, 291 null, 292 null, 293 null, 294 map, 295 null, 296 false 297 ); 298 } 299 catch (MalformedURLException e) 300 { 301 throw new JspException (e); 302 } 303 } 304 } 305 | Popular Tags |