KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > helpers > AjaxHtmlHelper


1 /**
2  * Copyright 2005 Darren L. Spurgeon
3  * Copyright 2007 Jens Kapitza
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.ajaxtags.helpers;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
35  * A helper class to assist in AJAX-enabling certain HTML elements/attributes
36  * used by other tags.
37  *
38  * @author Darren L. Spurgeon
39  * @author Jens Kapitza
40  * @version $Revision: 1.5 $ $Date: 2007/07/22 16:29:15 $ $Author: jenskapitza $
41  */

42 public class AjaxHtmlHelper {
43
44     /**
45      * Get the content of an HTML tag based on tag name.
46      *
47      * @param html
48      * the html content
49      * @param tag
50      * the tag name
51      * @return the content fom tag or null if html is empty od tag not found
52      * @see #getTagContent(String, String, String)
53      */

54     public static String JavaDoc getTagContent(final String JavaDoc html, final String JavaDoc tag) {
55         return getTagContent(html, tag, null);
56     }
57
58     /**
59      * Get the content of an HTML tag based on tag and class names.
60      *
61      * @param html
62      * the html content
63      * @param tag
64      * the tag name
65      * @param className
66      * the class name
67      * @return the content fom tag or null if html is empty od tag not found
68      */

69     public static String JavaDoc getTagContent(final String JavaDoc html, final String JavaDoc tag,
70             final String JavaDoc className) {
71         if (StringUtils.isBlank(html))
72             return null;
73
74         Source source = new Source(html);
75         // Rewrite pagination links
76
List JavaDoc<?> spans = source.findAllElements(tag);
77         for (Iterator JavaDoc<?> spanIter = spans.iterator(); spanIter.hasNext();) {
78             Element spanElement = (Element) spanIter.next();
79             // If no class is especified return firt tag
80
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     /**
94      * Get the content of an HTML tag based on its id.
95      *
96      * @param html
97      * the html content
98      * @param id
99      * the element id
100      * @return the content fom tag or null if html is empty od tag not found
101      */

102     public static String JavaDoc getTagContentById(final String JavaDoc html, final String JavaDoc id) {
103         if (StringUtils.isBlank(html))
104             return null;
105
106         Source source = new Source(html);
107         List JavaDoc<?> spans = source.findAllElements();
108         for (Iterator JavaDoc<?> 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     /**
123      * AJAX-enable HTML anchor tags.
124      *
125      * @param html
126      * the content to rewrite
127      * @param target
128      * the ajax target
129      * @return the rewriten html content
130      * @see #ajaxAnchors(String, String, String)
131      */

132     public static String JavaDoc ajaxAnchors(final String JavaDoc html, final String JavaDoc target) {
133         return ajaxAnchors(html, target, null);
134     }
135
136     /**
137      * AJAX-enable HTML anchor tags.
138      *
139      * @param html
140      * the content to rewrite
141      * @param target
142      * the ajax target
143      * @param ajaxFlag
144      * @return the rewriten html content
145      */

146     @SuppressWarnings JavaDoc("unchecked")
147     public static String JavaDoc ajaxAnchors(final String JavaDoc html, final String JavaDoc target,
148             String JavaDoc 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 JavaDoc<?> anchors = source.findAllStartTags(HTMLElementName.A);
156         for (Iterator JavaDoc<?> 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 JavaDoc href = hrefAttribute.getValue();
163             if (href == null)
164                 continue;
165             OptionsBuilder options = new OptionsBuilder();
166             options.add("evalScripts", "true", false);
167             // Map<String,String>
168
Map JavaDoc 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     /**
178      * Helper to define new AJAX updater for onclick attribute.
179      *
180      * @param target
181      * the target to request
182      * @param href
183      * the url
184      * @param ajaxFlag
185      * the ajaxflag
186      * @return the javascript code to do ajax update
187      */

188     public static final String JavaDoc getOnclickAjax(String JavaDoc target, String JavaDoc href,
189             String JavaDoc ajaxFlag) {
190         return getOnclickAjax(target, href, ajaxFlag, null);
191     }
192
193     /**
194      * Helper to define new AJAX updater for onclick attribute.
195      *
196      * @param target
197      * the target to request
198      * @param href
199      * the url
200      * @param ajaxFlag
201      * the ajaxflag
202      * @param options
203      * the for prototype framework
204      * @return the javascript code to do ajax update
205      */

206     public static final String JavaDoc getOnclickAjax(String JavaDoc target, String JavaDoc href,
207             String JavaDoc ajaxFlag, OptionsBuilder options) {
208         StringBuffer JavaDoc onclick = new StringBuffer JavaDoc("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