1 17 package org.ajaxtags.helpers; 18 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.ajaxtags.tags.OptionsBuilder; 24 import org.apache.commons.lang.StringUtils; 25 26 import au.id.jericho.lib.html.Attribute; 27 import au.id.jericho.lib.html.Attributes; 28 import au.id.jericho.lib.html.Element; 29 import au.id.jericho.lib.html.HTMLElementName; 30 import au.id.jericho.lib.html.OutputDocument; 31 import au.id.jericho.lib.html.Source; 32 import au.id.jericho.lib.html.StartTag; 33 34 42 public class AjaxHtmlHelper { 43 44 54 public static String getTagContent(final String html, final String tag) { 55 return getTagContent(html, tag, null); 56 } 57 58 69 public static String getTagContent(final String html, final String tag, 70 final String className) { 71 if (StringUtils.isBlank(html)) 72 return null; 73 74 Source source = new Source(html); 75 List <?> spans = source.findAllElements(tag); 77 for (Iterator <?> spanIter = spans.iterator(); spanIter.hasNext();) { 78 Element spanElement = (Element) spanIter.next(); 79 if (StringUtils.isBlank(className)) { 81 return spanElement.getContent().toString(); 82 } 83 Attributes spanAttributes = spanElement.getAttributes(); 84 Attribute classAttribute = spanAttributes.get("class"); 85 if (classAttribute != null && classAttribute.getValue() != null 86 && classAttribute.getValue().equalsIgnoreCase(className)) { 87 return spanElement.getContent().toString(); 88 } 89 } 90 return null; 91 } 92 93 102 public static String getTagContentById(final String html, final String id) { 103 if (StringUtils.isBlank(html)) 104 return null; 105 106 Source source = new Source(html); 107 List <?> spans = source.findAllElements(); 108 for (Iterator <?> spanIter = spans.iterator(); spanIter.hasNext();) { 109 Element spanElement = (Element) spanIter.next(); 110 Attributes spanAttributes = spanElement.getAttributes(); 111 Attribute classAttribute = spanAttributes.get("id"); 112 if (classAttribute != null && classAttribute.getValue() != null 113 && classAttribute.getValue().equalsIgnoreCase(id)) { 114 return spanElement.getContent().toString(); 115 } 116 } 117 118 return null; 119 120 } 121 122 132 public static String ajaxAnchors(final String html, final String target) { 133 return ajaxAnchors(html, target, null); 134 } 135 136 146 @SuppressWarnings ("unchecked") 147 public static String ajaxAnchors(final String html, final String target, 148 String ajaxFlag) { 149 if (StringUtils.isBlank(html)) 150 return null; 151 152 Source source = new Source(html); 153 OutputDocument outputDocument = new OutputDocument(source); 154 155 List <?> anchors = source.findAllStartTags(HTMLElementName.A); 156 for (Iterator <?> i = anchors.iterator(); i.hasNext();) { 157 StartTag anchorTag = (StartTag) i.next(); 158 Attributes attributes = anchorTag.getAttributes(); 159 Attribute hrefAttribute = attributes.get("href"); 160 if (hrefAttribute == null) 161 continue; 162 String href = hrefAttribute.getValue(); 163 if (href == null) 164 continue; 165 OptionsBuilder options = new OptionsBuilder(); 166 options.add("evalScripts", "true", false); 167 Map attrreplace = outputDocument.replace(attributes, true); 169 170 attrreplace.put("href", "javascript://nop/"); 171 attrreplace.put("onclick", getOnclickAjax(target, href, ajaxFlag, 172 options)); 173 } 174 return outputDocument.toString(); 175 } 176 177 188 public static final String getOnclickAjax(String target, String href, 189 String ajaxFlag) { 190 return getOnclickAjax(target, href, ajaxFlag, null); 191 } 192 193 206 public static final String getOnclickAjax(String target, String href, 207 String ajaxFlag, OptionsBuilder options) { 208 StringBuffer onclick = new StringBuffer ("new Ajax.Updater('"); 209 onclick.append(target); 210 onclick.append("', '"); 211 onclick.append(href); 212 if (StringUtils.isNotBlank(ajaxFlag)) { 213 if (href.indexOf(ajaxFlag + "=") < 0) { 214 if (href.indexOf("?") >= 0) { 215 onclick.append("&"); 216 } else { 217 onclick.append("?"); 218 } 219 onclick.append(ajaxFlag); 220 onclick.append("=true"); 221 } 222 } 223 onclick.append("'"); 224 if (options != null) { 225 onclick.append(",{"); 226 onclick.append(options.toString()); 227 onclick.append("}"); 228 } 229 onclick.append("); return false;"); 230 231 return onclick.toString(); 232 } 233 234 } 235 | Popular Tags |