KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > collection > FastCollectionItemTag


1 package fr.improve.struts.taglib.layout.collection;
2
3 import java.text.DecimalFormat JavaDoc;
4 import java.text.DecimalFormatSymbols JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10
11 import org.apache.struts.util.ResponseUtils;
12
13 import fr.improve.struts.taglib.layout.LayoutTagSupport;
14 import fr.improve.struts.taglib.layout.collection.header.CollectionItemEvent;
15 import fr.improve.struts.taglib.layout.collection.header.MultiLevelHeader;
16 import fr.improve.struts.taglib.layout.el.Expression;
17 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
18 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
19 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
20 import fr.improve.struts.taglib.layout.formatter.FormatException;
21 import fr.improve.struts.taglib.layout.sort.JavascriptSortUtil;
22 import fr.improve.struts.taglib.layout.util.LayoutUtils;
23 import fr.improve.struts.taglib.layout.util.NestedHelper;
24 import fr.improve.struts.taglib.layout.util.TagUtils;
25
26 /**
27  * This tag is a simpler version of the CollectionItemTag
28  * with not bodycontent support.
29  *
30  * This makes the tag faster with some versions of Jasper which allocate a 7k buffer for each bodycontent.
31
32  * @author: Jean-Noel Ribette
33  **/

34 public class FastCollectionItemTag extends LayoutTagSupport implements LayoutEventListener {
35     protected String JavaDoc name;
36     protected String JavaDoc property;
37     
38     protected String JavaDoc title;
39     protected String JavaDoc jspTitle;
40     
41     protected String JavaDoc arg0;
42     protected String JavaDoc arg1;
43     protected String JavaDoc tooltip;
44     
45     protected String JavaDoc footer;
46     protected String JavaDoc footerArg0;
47     protected String JavaDoc footerArg1;
48     
49     protected String JavaDoc jspFooter;
50     
51     protected String JavaDoc action;
52     protected String JavaDoc url;
53     protected String JavaDoc forward;
54     protected String JavaDoc page;
55     protected String JavaDoc target;
56     protected String JavaDoc param;
57     protected String JavaDoc paramId;
58     protected String JavaDoc paramName;
59     protected String JavaDoc paramProperty;
60     protected String JavaDoc onclick;
61     protected boolean sortable = false;
62     
63     protected String JavaDoc type;
64     protected String JavaDoc jspType;
65     
66     protected String JavaDoc width;
67     protected String JavaDoc styleClass;
68     protected String JavaDoc style;
69     protected boolean filter = true;
70     protected CollectionTag collectionTag;
71     protected boolean skip = false;
72     
73     protected SimpleItemContext context = createItemContext();
74     
75     /**
76      * Return the item context to use
77      * in association with this class.
78      * Subclass may override this method.
79      */

80     protected SimpleItemContext createItemContext() {
81         context = new SimpleItemContext();
82         return context;
83     }
84     
85     
86     public static final String JavaDoc SPAN_KEY = "fr.improve.struts.taglib.layout.collection.CollectionItemTag.SPAN_KEY";
87     
88     /**
89      * Added by Damien Viel for MathCollection
90      * Math operation to apply.
91      * It must be the name of a method in org.apache.commons.math.stat.StatsUtils with a double[] parameter.
92      * @see org.apache.commons.math.stat.StatsUtils
93      */

94     protected String JavaDoc mathOperation;
95     
96     /**
97      * Added by Damien Viel for MathCollection
98      * Decimal pattern to apply to format the math operation result.
99      */

100     protected String JavaDoc mathPattern;
101     
102     /**
103      * @author Damien Viel
104      * @return Returns the mathOperation.
105      */

106     public String JavaDoc getMathOperation() {
107         return mathOperation;
108     }
109     
110     /**
111      * @author Damien Viel
112      * @param mathOperation The mathOperation to set.
113      */

114     public void setMathOperation(String JavaDoc mathOperation) {
115         this.mathOperation = mathOperation;
116     }
117     
118     public int doEndLayoutTag() throws JspException JavaDoc {
119         if (skip) {
120             skip = false;
121             collectionTag.incrementColumn();
122             return EVAL_PAGE;
123         }
124
125         // The cell value.
126
Object JavaDoc lc_cell = buildContent();
127         
128         // Math operation.
129
if (mathOperation!=null) {
130             double lc_mathValue = LayoutUtils.getDouble(lc_cell);
131             collectionTag.storeMathData(lc_mathValue);
132         }
133
134         // Hidden value
135
String JavaDoc lc_hiddenValue = null;
136         if (collectionTag.sortType==BaseCollectionTag.SORT_JAVASCRIPT && sortable) {
137             lc_hiddenValue = JavascriptSortUtil.makeSortable(lc_cell);
138         }
139         
140         // Computed url.
141
String JavaDoc lc_url = buildUrl();
142         
143         boolean lc_filter = buildFilter();
144         
145         if (lc_cell != null && lc_filter && lc_cell instanceof String JavaDoc) {
146             // Filter HTML sensitive characters.
147
// Only do that if the value is a String (the formatter may want the original Object)
148
lc_cell = ResponseUtils.filter(lc_cell.toString());
149         }
150         if (lc_cell!=null && mathPattern!=null){
151             double lc_value = LayoutUtils.getDouble(lc_cell);
152             DecimalFormatSymbols JavaDoc lc_symbols = new DecimalFormatSymbols JavaDoc(LayoutUtils.getLocale(pageContext));
153             lc_cell = new DecimalFormat JavaDoc(mathPattern, lc_symbols).format(lc_value);
154         }
155         if (lc_cell != null && type != null) {
156             // Format the cell value
157

158             try {
159                 lc_cell = LayoutUtils.getSkin(pageContext.getSession()).getFormatter().format(lc_cell, type, pageContext);
160             } catch (FormatException fe) {
161                 throw new JspException JavaDoc("format " + type + " failed (" + fe.getMessage() + ")");
162                 /*
163                  JspException lc_jspException = new JspException("Format failed");
164                  lc_jspException.initCause(fe);
165                  throw lc_jspException;
166                  */

167             }
168         }
169         
170         if (lc_hiddenValue!=null) {
171             lc_cell = lc_hiddenValue + lc_cell;
172         }
173         
174         if (styleClass!=null) {
175             collectionTag.setTempStyleClass(styleClass);
176         }
177         if (style!=null) {
178             collectionTag.addTempStyle(style);
179         }
180         
181         // Create a buffer. Try to set a good initial size.
182
int lc_size = tryToGuessBufferSize(lc_cell, lc_url);
183         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(lc_size);
184         
185         // Initialize the context.
186
context.arg0 = arg0;
187         context.arg1 = arg1;
188         context.item = lc_cell !=null ? lc_cell.toString() : "";
189         
190         context.footer = footer;
191         context.footerArg0 = footerArg0;
192         context.footerArg1 = footerArg1;
193         
194         context.onclick = onclick;
195         context.property = property;
196         context.sortProperty = sortable ? (property==null ? "" : property) : null;
197         context.target = target;
198         context.title = title;
199         context.url = lc_url;
200         context.width = width;
201         context.mathOperation = mathOperation;
202         context.mathPattern = mathPattern;
203         
204         collectionTag.addItem(buffer, context);
205         TagUtils.write(pageContext, buffer.toString());
206         
207         if (styleClass!=null) {
208             collectionTag.setTempStyleClass(null);
209         }
210         if (style!=null) {
211             collectionTag.removeTempStyle();
212         }
213         //reset();
214

215         pageContext.removeAttribute(SPAN_KEY);
216         
217         return EVAL_PAGE;
218     }
219     /**
220      * @return
221      */

222     private int tryToGuessBufferSize(Object JavaDoc in_value, String JavaDoc in_url) {
223         int lc_size = 16;
224         if (in_url!=null) {
225             // Pour <a HREF="..."></a>
226
lc_size += in_url.length() + 15;
227         }
228         
229         if (onclick!=null) {
230             // onclick="valeur"
231
lc_size += onclick.length() + 10;
232         }
233         if (styleClass!=null) {
234             lc_size += styleClass.length() + 8;
235         }
236         if (style!=null) {
237             lc_size += style.length() + 8;
238         }
239         
240         if (in_value!=null) {
241             lc_size += in_value.toString().length();
242         }
243         
244         return lc_size;
245     }
246     
247     protected Object JavaDoc buildContent() throws JspException JavaDoc {
248         Object JavaDoc lc_cell = null;
249         if (name == null) {
250             // The item to add is a property of a bean in the current collection.
251
lc_cell = collectionTag.getBean();
252         } else {
253             // The item to add is a property of a bean in the context.
254
lc_cell = pageContext.findAttribute(name);
255         }
256         if (lc_cell != null && property != null) {
257             // Get the property of the bean.
258
lc_cell = getPropertyValue(lc_cell, property);
259         }
260         return lc_cell;
261     }
262     
263     protected String JavaDoc buildUrl() throws JspException JavaDoc {
264         String JavaDoc lc_url = null;
265         Object JavaDoc lc_cell = null;
266         if (name == null) {
267             // The item to add is a property of a bean in the current collection.
268
lc_cell = collectionTag.getBean();
269         } else {
270             // The item to add is a property of a bean in the context.
271
lc_cell = pageContext.findAttribute(name);
272         }
273         if (lc_cell!=null && (url != null || page != null || forward!=null || action!=null)) {
274             //url += LayoutUtils.getProperty(lc_cell, param);
275
lc_url = getLink(url, lc_cell, param, paramId, paramName, paramProperty);
276         }
277         return lc_url;
278     }
279     
280     protected boolean buildFilter() {
281         return filter;
282     }
283     
284     public int doStartLayoutTag() throws JspException JavaDoc {
285         context.reset();
286         try {
287             collectionTag =
288                 (CollectionTag) findAncestorWithClass(this, CollectionTag.class);
289             if (collectionTag.isFirst()) {
290                 MultiLevelHeader lc_header = new MultiLevelHeader(title, arg0, arg1, styleClass, true);
291                 lc_header.setTooltip(tooltip);
292                 lc_header.setWidth(width);
293                 lc_header.setSortProperty(sortable ? property : null);
294                 new CollectionItemEvent(this, lc_header).send();
295                 return SKIP_BODY;
296             }
297             if (name!=null) {
298                 if (!collectionTag.getSpans().containsKey(name)) {
299                     skip = true;
300                     return SKIP_BODY;
301                 } else {
302                     pageContext.setAttribute(SPAN_KEY, collectionTag.getSpans().get(name));
303                 }
304             }
305             
306         } catch (ClassCastException JavaDoc e) {
307             throw new JspException JavaDoc("Invalid use of collectionItem tag");
308         } catch (NullPointerException JavaDoc e) {
309             throw new JspException JavaDoc("Invalid use of collectionItem tag");
310         }
311         return EVAL_BODY_INCLUDE;
312     }
313     
314     /**
315      * Helper method to generate the hyperlink associated with a cell.
316      *
317      * @param in_url value of the url tag attribute
318      * @param in_bean current bean in the iteration
319      * @param in_oldParam value of the param attribute
320      * @param in_paramId value of the paramId attribute
321      * @param in_paramName value of the paramName attribute.
322      * @param in_paramProperty value of the paramProperty attribute
323      *
324      * @return the hyperlink
325      */

326     protected String JavaDoc getLink(String JavaDoc in_url, Object JavaDoc in_bean, String JavaDoc in_oldParam, String JavaDoc in_paramId, String JavaDoc in_paramName, String JavaDoc in_paramProperty) throws JspException JavaDoc {
327         if (in_oldParam!=null) {
328             // struts-layout <0.5 way of setting a unique parameter.
329
return url + LayoutUtils.getProperty(in_bean, param);
330         } else {
331             // struts-layout >=0.5 way of setting multiple parameters.
332
Map JavaDoc lc_params = new HashMap JavaDoc();
333             if(in_paramId!= null) {
334                 StringTokenizer JavaDoc lc_paramIdTokens = new StringTokenizer JavaDoc(in_paramId, ",");
335                 StringTokenizer JavaDoc lc_paramPropertyTokens = null;
336                 if (in_paramProperty!=null) {
337                     lc_paramPropertyTokens = new StringTokenizer JavaDoc(in_paramProperty, ",");
338                 }
339                 StringTokenizer JavaDoc lc_paramNameTokens = null;
340                 if (in_paramName!=null) {
341                     lc_paramNameTokens = new StringTokenizer JavaDoc(in_paramName, ",");
342                 }
343                 if (lc_paramPropertyTokens!=null && lc_paramPropertyTokens.countTokens()!=lc_paramIdTokens.countTokens()) {
344                     throw new JspException JavaDoc("paramId (" + paramId + ") and paramProperty (" + paramProperty + ") don't specify the same number of parameters");
345                 }
346                 if (lc_paramNameTokens!=null && lc_paramNameTokens.countTokens()!=lc_paramIdTokens.countTokens()) {
347                     throw new JspException JavaDoc("paramId (" + paramId + ") and paramName (" + paramName + ") don't specify the same number of parameters");
348                 }
349                 
350                 while (lc_paramIdTokens.hasMoreTokens()) {
351                     String JavaDoc lc_paramId = lc_paramIdTokens.nextToken();
352                     Object JavaDoc lc_bean = in_bean;
353                     if (lc_paramNameTokens!=null) {
354                         lc_bean = pageContext.findAttribute(lc_paramNameTokens.nextToken());
355                     }
356                     String JavaDoc lc_paramProperty = null;
357                     if (lc_paramPropertyTokens!=null) {
358                         lc_paramProperty = lc_paramPropertyTokens.nextToken();
359                     }
360                     lc_params.put(NestedHelper.getAdjustedProperty(lc_paramId, pageContext), LayoutUtils.getProperty(lc_bean, lc_paramProperty));
361                 }
362             }
363             String JavaDoc lc_url = LayoutUtils.computeURL(pageContext, forward, url, page, action, null, lc_params, null, false, target);
364             
365             return lc_url;
366             /*
367              StringBuffer lc_buffer = new StringBuffer(url);
368              boolean lc_firstParam = url.indexOf('?')==-1;
369              while (lc_paramIdTokens.hasMoreTokens()) {
370              if (lc_firstParam) {
371              lc_buffer.append("?");
372              lc_firstParam = false;
373              } else {
374              lc_buffer.append("&");
375              }
376              lc_buffer.append(lc_paramIdTokens.nextToken());
377              lc_buffer.append("=");
378              lc_buffer.append(LayoutUtils.getProperty(in_bean, lc_paramPropertyTokens.nextToken()));
379              }
380              return lc_buffer.toString();
381              */

382         }
383     }
384     
385     /**
386      * This method simply uses BeanUtils (indirectly) to return the value of the property 'in_property'
387      * of in_bean. This method is intended to be overrided if needed.
388      */

389     protected Object JavaDoc getPropertyValue(Object JavaDoc in_bean, String JavaDoc in_property) throws JspException JavaDoc {
390         return LayoutUtils.getProperty(in_bean, in_property);
391     }
392     
393     
394     /**
395      * Creation date: (15/06/01 15:43:43)
396      * @return java.lang.String
397      */

398     public java.lang.String JavaDoc getOnclick() {
399         return onclick;
400     }
401     /**
402      * Creation date: (15/06/01 15:38:19)
403      * @return java.lang.String
404      */

405     public java.lang.String JavaDoc getParam() {
406         return param;
407     }
408     /**
409      * Insert the method's description here.
410      * Creation date: (04/10/2001 12:38:46)
411      * @return java.lang.String
412      */

413     public java.lang.String JavaDoc getType() {
414         return type;
415     }
416     /**
417      * Creation date: (15/06/01 15:38:19)
418      * @return java.lang.String
419      */

420     public java.lang.String JavaDoc getUrl() {
421         return url;
422     }
423     public void release() {
424         super.release();
425         property = null;
426         
427         title = null;
428         arg0 = null;
429         arg1 = null;
430         
431         tooltip = null;
432         
433         footer = null;
434         footerArg0 = null;
435         footerArg1= null;
436         
437         name = null;
438         action = null;
439         url = null;
440         forward = null;
441         page = null;
442         param = null;
443         paramId = null;
444         paramName = null;
445         paramProperty = null;
446         onclick = null;
447         sortable = false;
448         type = null;
449         target = null;
450         width = null;
451         styleClass = null;
452         style = null;
453         filter = true;
454         skip = false;
455         mathOperation = null;
456     }
457     protected void reset() {
458         collectionTag = null;
459         
460         type = jspType;
461         jspType = null;
462         
463         footer = jspFooter;
464         jspFooter = null;
465         
466         title = jspTitle;
467         jspTitle = null;
468     }
469     public void setName(String JavaDoc name) {
470         this.name = name;
471     }
472     public String JavaDoc getName() {
473         return name;
474     }
475     /**
476      * Creation date: (15/06/01 15:43:43)
477      * @param newOnclick java.lang.String
478      */

479     public void setOnclick(java.lang.String JavaDoc newOnclick) {
480         onclick = newOnclick;
481     }
482     /**
483      * Creation date: (15/06/01 15:38:19)
484      * @param newParam java.lang.String
485      */

486     public void setParam(java.lang.String JavaDoc newParam) {
487         param = newParam;
488     }
489     /**
490      * Insert the method's description here.
491      * Creation date: (16/02/01 11:11:03)
492      * @param newProperty java.lang.String
493      */

494     public void setProperty(java.lang.String JavaDoc newProperty) {
495         property = newProperty;
496     }
497     public String JavaDoc getProperty() {
498         return property;
499     }
500     public void setSortable(String JavaDoc sortable) {
501         if ("true".equalsIgnoreCase(sortable)) {
502             this.sortable = true;
503         } else {
504             this.sortable = false;
505         }
506     }
507     public String JavaDoc getSortable() {
508         return String.valueOf(sortable);
509     }
510     /**
511      * Insert the method's description here.
512      * Creation date: (16/02/01 11:11:03)
513      * @param newHeader java.lang.String
514      */

515     public void setTitle(java.lang.String JavaDoc newHeader) {
516         title = newHeader;
517     }
518     public String JavaDoc getTitle() {
519         return title;
520     }
521     /**
522      * Insert the method's description here.
523      * Creation date: (04/10/2001 12:38:46)
524      * @param newType java.lang.String
525      */

526     public void setType(java.lang.String JavaDoc newType) {
527         type = newType;
528     }
529     /**
530      * Creation date: (15/06/01 15:38:19)
531      * @param newUrl java.lang.String
532      */

533     public void setUrl(java.lang.String JavaDoc newUrl) {
534         url = newUrl;
535     }
536     public void setHref(String JavaDoc in_href) {
537         url = in_href;
538     }
539     public void setPage(String JavaDoc in_page) {
540         page = in_page;
541     }
542     public void setForward(String JavaDoc in_forward) {
543         forward = in_forward;
544     }
545     /**
546      * Gets the target.
547      * @return Returns a String
548      */

549     public String JavaDoc getTarget() {
550         return target;
551     }
552
553     /**
554      * Sets the target.
555      * @param target The target to set
556      */

557     public void setTarget(String JavaDoc target) {
558         this.target = target;
559     }
560
561     /**
562      * Gets the width.
563      * @return Returns a String
564      */

565     public String JavaDoc getWidth() {
566         return width;
567     }
568
569     /**
570      * Sets the width.
571      * @param width The width to set
572      */

573     public void setWidth(String JavaDoc width) {
574         this.width = width;
575     }
576
577     /**
578      * Gets the paramId.
579      * @return Returns a String
580      */

581     public String JavaDoc getParamId() {
582         return paramId;
583     }
584
585     /**
586      * Sets the paramId.
587      * @param paramId The paramId to set
588      */

589     public void setParamId(String JavaDoc paramId) {
590         this.paramId = paramId;
591     }
592
593     /**
594      * Gets the paramProperty.
595      * @return Returns a String
596      */

597     public String JavaDoc getParamProperty() {
598         return paramProperty;
599     }
600
601     /**
602      * Sets the paramProperty.
603      * @param paramProperty The paramProperty to set
604      */

605     public void setParamProperty(String JavaDoc paramProperty) {
606         this.paramProperty = paramProperty;
607     }
608     
609     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) {
610         return Boolean.FALSE;
611     }
612     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) {
613         return Boolean.FALSE;
614         
615     }
616     /**
617      * Sets the styleClass.
618      * @param styleClass The styleClass to set
619      */

620     public void setStyleClass(String JavaDoc styleClass) {
621         this.styleClass = styleClass;
622     }
623
624     /**
625      * Sets the style.
626      * @param style The style to set
627      */

628     public void setStyle(String JavaDoc style) {
629         this.style = style;
630     }
631
632     /**
633      * Sets the filter.
634      * @param filter The filter to set
635      */

636     public void setFilter(boolean filter) {
637         this.filter = filter;
638     }
639
640     /**
641      * Sets the paramName.
642      * @param paramName The paramName to set
643      */

644     public void setParamName(String JavaDoc paramName) {
645         this.paramName = paramName;
646     }
647
648     /**
649      * @return Returns the arg0.
650      */

651     public String JavaDoc getArg0() {
652         return arg0;
653     }
654     /**
655      * @param arg0 The arg0 to set.
656      */

657     public void setArg0(String JavaDoc arg0) {
658         this.arg0 = arg0;
659     }
660     /**
661      * @return Returns the arg1.
662      */

663     public String JavaDoc getArg1() {
664         return arg1;
665     }
666     /**
667      * @param arg1 The arg1 to set.
668      */

669     public void setArg1(String JavaDoc arg1) {
670         this.arg1 = arg1;
671     }
672     public String JavaDoc getAction() {
673         return action;
674     }
675     public void setAction(String JavaDoc action) {
676         this.action = action;
677     }
678     protected void initDynamicValues() {
679         super.initDynamicValues();
680         
681         jspType = type;
682         type = Expression.evaluate(type, pageContext);
683         
684         jspFooter = footer;
685         footer = Expression.evaluate(footer, pageContext);
686         
687         jspTitle = title;
688         title = Expression.evaluate(title, pageContext);
689     }
690     public void setFooter(String JavaDoc footer) {
691         this.footer = footer;
692     }
693     public void setFooterArg0(String JavaDoc arg0) {
694         footerArg0 = arg0;
695     }
696     public void setFooterArg1(String JavaDoc arg1){
697         footerArg1 = arg1;
698     }
699     /**
700      * @return Returns the mathPattern.
701      */

702     public String JavaDoc getMathPattern() {
703         return mathPattern;
704     }
705     /**
706      * @param mathPattern The mathPattern to set.
707      */

708     public void setMathPattern(String JavaDoc mathPattern) {
709         this.mathPattern = mathPattern;
710     }
711     /**
712      * @param tooltip The tooltip to set.
713      */

714     public void setTooltip(String JavaDoc tooltip) {
715         this.tooltip = tooltip;
716     }
717 }
Popular Tags