1 9 package org.ajaxanywhere; 10 11 12 import java.text.MessageFormat ; 13 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.jsp.JspException ; 17 import javax.servlet.jsp.JspWriter ; 18 import javax.servlet.jsp.tagext.BodyContent ; 19 import javax.servlet.jsp.tagext.BodyTagSupport ; 20 21 49 public class LinkTag extends BodyTagSupport { 51 52 55 private static final long serialVersionUID = 1927146406417618943L; 56 57 public static final String LINK_HTML_START = "<a HREF=\"{0}\" onclick=\"{1}\" {2} >"; 58 public static final String LINK_HTML_END = "</a>"; 59 public static final String DEFAULT_FUNCTION_NAME = "ajaxAnywhere.getAJAX"; 60 61 private String functionName; 63 private String zones; 64 private String href; 65 private String styleClass; 66 private String style; 67 68 69 public LinkTag( ) 70 { 71 super(); 72 functionName = DEFAULT_FUNCTION_NAME; 73 } 74 75 78 public String getFunctionName() 79 { 80 return functionName; 81 } 82 83 90 public void setFunctionName( String function ) 91 { 92 this.functionName = function; 93 } 94 95 private Object buildOnClick() 96 { 97 StringBuffer strbuff = new StringBuffer ( this.getFunctionName() ); 98 strbuff.append( "('" ); 99 strbuff.append( buildUrl() ); 100 strbuff.append( "', '" ); 101 strbuff.append( this.getZones() ); 102 strbuff.append( "')" ); 103 return strbuff.toString(); 104 } 105 110 private Object buildOtherAttributes() 111 { 112 StringBuffer strbuff = new StringBuffer ( ); 113 114 if ( style != null && style.trim().length() > 0 ) 115 { 116 strbuff.append( "style=\"" ); 117 strbuff.append( style ); 118 strbuff.append( "\"" ); 119 strbuff.append( " " ); 120 } 121 122 if ( styleClass != null && styleClass.trim().length() > 0 ) 123 { 124 strbuff.append( "class=\"" ); 125 strbuff.append( styleClass ); 126 strbuff.append( "\"" ); 127 strbuff.append( " " ); 128 } 129 130 return strbuff.toString(); 131 } 132 133 136 public String getZones() 137 { 138 return zones; 139 } 140 141 148 public void setZones( String zones ) 149 { 150 this.zones = zones; 151 } 152 153 156 public int doStartTag() throws JspException 157 { 158 try 159 { 160 Object [] arguments = { buildUrl(), buildOnClick(), buildOtherAttributes() }; 161 String html = MessageFormat.format( LINK_HTML_START, arguments ); 162 pageContext.getOut().print( html ); 163 } 164 catch ( Exception ex ) 165 { 166 throw new JspException ( ex ); 167 } 168 return EVAL_BODY_BUFFERED; } 170 171 172 173 174 179 private Object buildUrl() 180 { 181 HttpServletRequest request = ( HttpServletRequest ) pageContext.getRequest(); 182 return ( ( HttpServletResponse ) this.pageContext.getResponse() ).encodeURL( request.getContextPath() + "/" + href ); 183 } 184 185 193 public int doEndTag() throws JspException 194 { 195 196 try 197 { 198 BodyContent bc = getBodyContent(); 199 String body = bc.getString(); 201 JspWriter out = bc.getEnclosingWriter(); 203 if ( body != null ) 204 { 205 out.print( body ); 206 pageContext.getOut().print( LINK_HTML_END ); 207 } 208 } 209 catch ( Exception e ) 210 { 211 throw new JspException ( e ); 212 } 213 return EVAL_PAGE; 215 } 216 217 218 219 220 221 224 public String getStyle() 225 { 226 return style; 227 } 228 229 230 236 public void setStyle( String style ) 237 { 238 this.style = style; 239 } 240 241 242 245 public String getStyleClass() 246 { 247 return styleClass; 248 } 249 250 251 257 public void setStyleClass( String styleClass ) 258 { 259 this.styleClass = styleClass; 260 } 261 262 263 266 public String getHref() 267 { 268 return href; 269 } 270 271 272 278 public void setHref( String href ) 279 { 280 this.href = href; 281 } 282 283 } 284 285 | Popular Tags |