1 16 package org.ajaxtags.tags; 17 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.servlet.jsp.JspException ; 23 24 import org.apache.commons.lang.StringUtils; 25 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; 26 27 import au.id.jericho.lib.html.Attribute; 28 import au.id.jericho.lib.html.Attributes; 29 import au.id.jericho.lib.html.Element; 30 import au.id.jericho.lib.html.HTMLElementName; 31 import au.id.jericho.lib.html.OutputDocument; 32 import au.id.jericho.lib.html.Segment; 33 import au.id.jericho.lib.html.Source; 34 import au.id.jericho.lib.html.StartTag; 35 36 45 public class AjaxDisplayTag extends AjaxAreaTag { 46 47 private String pagelinksClass = "pagelinks"; 48 private String tableClass = "displaytag"; 49 private String columnClass = "sortable"; 50 private String baseUrl = ""; 51 private String postFunction = null; 52 private String preFunction = null; 53 private String parameters; 54 55 public String getPreFunction() { 56 return preFunction; 57 } 58 59 public void setPreFunction(String preFunction) { 60 this.preFunction = preFunction; 61 } 62 63 66 public String getPagelinksClass() { 67 return this.pagelinksClass; 68 } 69 70 74 public void setPagelinksClass(String pagelinksClass) { 75 this.pagelinksClass = pagelinksClass; 76 } 77 78 81 public String getPostFunction() { 82 return postFunction; 83 } 84 85 89 public void setPostFunction(String postFunction) { 90 this.postFunction = postFunction; 91 } 92 93 96 public String getTableClass() { 97 return this.tableClass; 98 } 99 100 104 public void setTableClass(String tableClass) { 105 this.tableClass = tableClass; 106 } 107 108 111 public String getColumnClass() { 112 return this.columnClass; 113 } 114 115 119 public void setColumnClass(String columnClass) { 120 this.columnClass = columnClass; 121 } 122 123 126 public String getBaseUrl() { 127 return this.baseUrl; 128 } 129 130 134 public void setBaseUrl(String baseUrl) { 135 this.baseUrl = baseUrl; 136 } 137 138 public String getParameters() { 139 return parameters; 140 } 141 142 public void setParameters(String parameters) { 143 this.parameters = parameters; 144 } 145 146 149 @Override 150 public void release() { 151 this.pagelinksClass = null; 152 this.tableClass = null; 153 this.columnClass = null; 154 this.baseUrl = null; 155 this.postFunction = null; 156 this.parameters = null; 157 super.release(); 158 } 159 160 163 @Override 164 protected void initParameters() throws JspException { 165 super.initParameters(); 166 if (this.pagelinksClass != null) { 168 this.pagelinksClass = (String ) ExpressionEvaluatorManager.evaluate( 169 "pagelinksClass", this.pagelinksClass, String .class, this, 170 super.pageContext); 171 } 172 if (this.tableClass != null) { 173 this.tableClass = (String ) ExpressionEvaluatorManager.evaluate( 174 "tableClass", this.tableClass, String .class, this, 175 super.pageContext); 176 } 177 if (this.columnClass != null) { 178 this.columnClass = (String ) ExpressionEvaluatorManager.evaluate( 179 "columnClass", this.columnClass, String .class, this, 180 super.pageContext); 181 } 182 if (this.baseUrl != null) { 183 this.baseUrl = (String ) ExpressionEvaluatorManager.evaluate( 184 "baseUrl", this.baseUrl, String .class, this, 185 super.pageContext); 186 } 187 if (this.postFunction != null) { 188 this.postFunction = (String ) ExpressionEvaluatorManager.evaluate( 189 "postFunction", this.postFunction, String .class, this, 190 super.pageContext); 191 } 192 if (this.preFunction != null) { 193 this.preFunction = (String ) ExpressionEvaluatorManager.evaluate( 194 "preFunction", this.preFunction, String .class, this, 195 super.pageContext); 196 } 197 if (this.parameters != null) { 198 this.parameters = (String ) ExpressionEvaluatorManager.evaluate( 199 "parameters", this.parameters, String .class, this, 200 super.pageContext); 201 } 202 } 203 204 207 @Override 208 protected String processContent(String content) { 209 Source source = new Source(content); 210 OutputDocument outputDocument = new OutputDocument(source); 211 List <?> spans = source.findAllElements(HTMLElementName.SPAN); 213 for (Iterator <?> spanIter = spans.iterator(); spanIter.hasNext();) { 214 Element spanElement = (Element) spanIter.next(); 215 Attributes spanAttributes = spanElement.getAttributes(); 216 Attribute classAttribute = spanAttributes.get("class"); 217 if (classAttribute != null 218 && classAttribute.getValue() != null 219 && classAttribute.getValue().equalsIgnoreCase( 220 this.pagelinksClass)) { 221 rewriteAnchors(spanElement, outputDocument); 222 } 223 } 224 List <?> tables = source.findAllElements(HTMLElementName.TABLE); 226 for (Iterator <?> tableIter = tables.iterator(); tableIter.hasNext();) { 227 Element tableElement = (Element) tableIter.next(); 228 Attributes tableAttributes = tableElement.getAttributes(); 229 Attribute classAttribute = tableAttributes.get("class"); 230 if (classAttribute != null 231 && classAttribute.getValue() != null 232 && classAttribute.getValue().equalsIgnoreCase( 233 this.tableClass)) { 234 List <?> theads = tableElement 235 .findAllElements(HTMLElementName.THEAD); 236 for (Iterator <?> theadIter = theads.iterator(); theadIter 237 .hasNext();) { 238 Element theadElement = (Element) theadIter.next(); 239 if (StringUtils.isNotBlank(this.columnClass)) { 240 List <?> ths = theadElement 241 .findAllElements(HTMLElementName.TH); 242 for (Iterator <?> thIter = ths.iterator(); thIter 243 .hasNext();) { 244 Element columnElement = (Element) thIter.next(); 245 Attribute columnClassAttribute = columnElement 246 .getAttributes().get("class"); 247 if (columnClassAttribute != null 248 && columnClassAttribute.getValue() != null 249 && StringUtils.contains( 250 columnClassAttribute.getValue() 251 .toLowerCase(), 252 this.columnClass.toLowerCase())) { 253 rewriteAnchors(columnElement, outputDocument); 254 } 255 } 256 } else { 257 rewriteAnchors(theadElement, outputDocument); 258 } 259 } 260 } 261 } 262 263 return outputDocument.toString(); 264 } 265 266 272 @SuppressWarnings ("unchecked") 273 protected void rewriteAnchors(Segment segment, OutputDocument outputDocument) { 274 List <?> anchors = segment.findAllStartTags(HTMLElementName.A); 275 for (Iterator <?> i = anchors.iterator(); i.hasNext();) { 276 StartTag anchorTag = (StartTag) i.next(); 277 Attributes attributes = anchorTag.getAttributes(); 278 Attribute hrefAttribute = attributes.get("href"); 279 if (hrefAttribute == null) 280 continue; 281 String href = hrefAttribute.getValue(); 282 if (href == null) 283 continue; 284 if (StringUtils.isNotBlank(this.getBaseUrl())) { 287 if (href.indexOf("?") > 0) { 289 href = href.substring(href.indexOf("?")); 290 } 291 href = this.getBaseUrl() + href; 292 } 293 294 Map <String , String > attrreplace = outputDocument.replace( 295 attributes, true); 296 OptionsBuilder onclickOptions = new OptionsBuilder(); 299 if (this.postFunction != null) { 300 onclickOptions.add("postFunction", this.postFunction, false); 301 } 302 if (this.parameters != null) { 303 onclickOptions.add("parameters", this.parameters, true); 304 } 305 306 if (this.preFunction != null) { 307 onclickOptions.add("preFunction", this.preFunction, false); 308 } 309 310 attrreplace.put("href", "javascript://nop/"); 311 312 if (StringUtils.isNotBlank(this.getAjaxFlag())) { 313 if (href.indexOf(this.getAjaxFlag() + "=") < 0) { 314 if (href.indexOf("?") >= 0) { 315 href += "&"; 316 } else { 317 href += "?"; 318 } 319 href += this.getAjaxFlag() + "=true"; 320 } 321 } 322 323 if (href != null) { 324 onclickOptions.add("href", href, true); 325 } 326 327 if (this.getId() != null) { 328 onclickOptions.add("id", this.getId(), true); 329 } 330 attrreplace.put("onclick", 334 getOnclickAjaxInvokePreFunction(onclickOptions)); 335 } 337 } 338 339 private static final String getOnclickAjaxInvokePreFunction( 340 OptionsBuilder options) { 341 StringBuffer onclick = new StringBuffer ( 342 "new AjaxJspTag.PreFunctionUpdateInvoke("); 343 344 onclick.append("{"); 345 onclick.append(options.toString()); 346 onclick.append("}"); 347 onclick.append("); return false;"); 348 349 return onclick.toString(); 350 } 351 352 } 353
| Popular Tags
|