KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > YuzuTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import java.lang.reflect.*;
39 import java.util.*;
40 import java.util.regex.*;
41 import java.beans.*;
42 import java.io.*;
43 import java.net.*;
44 import javax.servlet.*;
45 import javax.servlet.http.*;
46 import javax.servlet.jsp.*;
47 import javax.servlet.jsp.tagext.*;
48 import javax.servlet.jsp.jstl.core.Config;
49 import org.apache.taglibs.standard.tag.common.core.Util;
50 import com.micronova.util.*;
51 import com.micronova.util.servlet.*;
52
53 import javax.xml.parsers.*;
54 import javax.xml.transform.*;
55 import javax.xml.transform.dom.*;
56 import javax.xml.transform.stream.*;
57 import org.w3c.dom.*;
58
59 /**
60
61 Base class for YUZU tag library.
62
63 */

64
65 public class YuzuTag extends YuzuRoot implements TryCatchFinally
66 {
67     /** Object indicating 'default value' */
68
69     public static final String JavaDoc DEFAULT = "default";
70
71     /** name of the call stack in requestScope */
72
73     public static final String JavaDoc CALLSTACK = "com.micronova.jsp.tag.YuzuTag.callStack";
74
75     /** standard callstack fields */
76
77     public static final String JavaDoc TAG = "tag";
78     public static final String JavaDoc PAGECONTEXT = "pageContext";
79     public static final String JavaDoc HIDDEN = "hidden";
80
81     /** root pageContext in requestScope, if any */
82
83     public static final String JavaDoc ROOTPAGECONTEXT = "com.micronova.jsp.tag.YuzuTag.rootPageContext";
84
85     /** name of this class (used for parent tag search) */
86
87     public static final String JavaDoc CLASSNAME = "com.micronova.jsp.tag.YuzuTag";
88
89     /** page-scoped variable name for the 'tagvalue' */
90
91     public static final String JavaDoc VALUEVAR = "_";
92
93     /** special target name used for returning a value */
94
95     public static final String JavaDoc RETURNTARGET = "return";
96
97     /** 'value' of this tag (either set by the code or 'value' attribute) */
98
99     protected Object JavaDoc _value;
100
101     /** default value set by 'default' attribute */
102
103     protected Object JavaDoc _defaultValue;
104
105     /** test expression for 'default' attribute */
106
107     protected String JavaDoc _test;
108
109     /** variable name for assignment */
110
111     protected String JavaDoc _var;
112
113     /** target Object for assignment */
114
115     protected Object JavaDoc _target;
116
117     /** attribute name to be set */
118
119     protected String JavaDoc _attribute;
120
121     /** property name to be set */
122
123     protected String JavaDoc _property;
124
125     /** scope of the variable for assignment */
126
127     protected int _scope;
128     
129     /** class name to be instantiated */
130
131     protected String JavaDoc _className;
132
133     /** codec used on exporting */
134
135     protected String JavaDoc _exportCodec;
136
137     /** codec used on importing */
138
139     protected String JavaDoc _importCodec;
140
141     /** codecs used on processing */
142
143     protected String JavaDoc _processCodec;
144
145     /** codec used on preparation */
146
147     protected String JavaDoc _prepareCodec;
148
149     /** codec used on cleanup */
150
151     protected String JavaDoc _cleanupCodec;
152
153     /** EL expression specifing value to be assigned */
154
155     protected String JavaDoc _assign;
156
157     /** codec used on assignment */
158
159     protected String JavaDoc _assignCodec;
160
161     /** EL expression specifying value to be exported */
162
163     protected String JavaDoc _export;
164
165     /** export mode; 'always' or 'default' */
166
167     protected String JavaDoc _doesExport;
168
169     /** comma-separated list of local variables */
170
171     protected String JavaDoc _local;
172
173     /** local variable map */
174
175     protected Map _localMap;
176
177     /** set to true while inside tag body */
178
179     protected boolean _isInBody;
180
181     /** saves current value of VALUEVAR */
182
183     protected Object JavaDoc _valueVarSaved;
184
185     /** set to true if the tag has body */
186
187     protected boolean _hasBody;
188
189     /** set to true if tagValue is assigned */
190
191     protected boolean _isAssigned;
192
193     /** obtain configuration named configName, or defaultConfiguration if not found */
194
195     protected Object JavaDoc getConfiguration(String JavaDoc configName, Object JavaDoc defaultConfiguration)
196     {
197         Object JavaDoc configuration = Config.find(pageContext, configName);
198
199         if (configuration == null)
200         {
201             configuration = defaultConfiguration;
202         }
203
204         return configuration;
205     }
206
207     /** throws a JspException. If given e is a JspException, it is thrown as-is; otherwise a new JspException with rootCause = e is thrown. */
208
209     public void rethrow(Throwable JavaDoc e) throws JspException
210     {
211         if (e instanceof JspException)
212         {
213             throw (JspException)e;
214         }
215         else
216         {
217             throw new JspException(e);
218         }
219     }
220
221     /** constructor */
222
223     public YuzuTag()
224     {
225         super();
226
227         init();
228     }
229     
230     /** initializer. Since tags may be re-used by the container, this is called both before and after tag processing. */
231
232     protected void init()
233     {
234         _value = null;
235         _defaultValue = null;
236         _test = null;
237         _var = null;
238         _target = null;
239         _property = null;
240         _scope = PageContext.PAGE_SCOPE;
241         _attribute = null;
242         _className = null;
243  
244         _local = null;
245         _localMap = null;
246         _valueVarSaved = null;
247
248         _export = DEFAULT;
249         _assign = DEFAULT;
250         _doesExport = DEFAULT;
251
252         _assignCodec = null;
253         _exportCodec = null;
254         _importCodec = null;
255         _processCodec = null;
256         _prepareCodec = null;
257         _cleanupCodec = null;
258
259         _isInBody = false;
260         _hasBody = false;
261         _isAssigned = false;
262     }
263
264     /** called after tag processing is done (in doFinally()) */
265
266     protected void cleanup()
267     {
268         String JavaDoc cleanupCodec = _cleanupCodec;
269
270         if (cleanupCodec != null)
271         {
272             try
273             {
274                 applyCodec(cleanupCodec, _value);
275             }
276             catch (Exception JavaDoc e)
277             {
278                 // exception is ignored
279
}
280         }
281     }
282
283     /** applies codecs to an Object */
284
285     protected Object JavaDoc applyCodec(String JavaDoc codecs, Object JavaDoc object) throws Exception JavaDoc
286     {
287         return EL.applyCodec(pageContext, codecs, object);
288     }
289
290     /** sets a page attribute named 'name' to 'value'. When 'value' is null, then the attribute is removed. */
291
292     protected void setPageAttribute(String JavaDoc name, Object JavaDoc value)
293     {
294         EL.setPageAttribute(pageContext, name, value);
295     }
296
297     /** gets the value of a page attribute named 'name' */
298
299     protected Object JavaDoc getPageAttribute(String JavaDoc name)
300     {
301         return EL.getPageAttribute(pageContext, name);
302     }
303
304     public static Object JavaDoc getRequestAttribute(PageContext pageContext, String JavaDoc name)
305     {
306         return pageContext.getRequest().getAttribute(name);
307     }
308
309     /*** sets a scoped attribute named 'name' to 'value'. When 'value' is null, then the attribute is removed. */
310
311     public static void setScopedAttribute(PageContext pageContext, String JavaDoc name, Object JavaDoc value, int scope)
312     {
313         if (scope == PageContext.SESSION_SCOPE)
314         {
315             HttpSession session = ((HttpServletRequest)pageContext.getRequest()).getSession();
316
317             if (value == null)
318             {
319                 session.removeAttribute(name);
320             }
321             else
322             {
323                 session.setAttribute(name, value);
324             }
325         }
326         else
327         {
328             if (value == null)
329             {
330                 pageContext.removeAttribute(name, scope);
331             }
332             else
333             {
334                 pageContext.setAttribute(name, value, scope);
335             }
336         }
337     }
338
339     /** saves 'local' variables */
340
341     protected void saveLocal() throws Exception JavaDoc
342     {
343         _valueVarSaved = getPageAttribute(VALUEVAR);
344
345         String JavaDoc local = _local;
346
347         if (local != null)
348         {
349             Map localMap = new HashMap();
350             String JavaDoc[] localArray = local.split("[ ,\t\r\n]+");
351
352             for (int i = 0; i < localArray.length; i ++)
353             {
354                 String JavaDoc key = localArray[i];
355
356                 Object JavaDoc value = getPageAttribute(key);
357
358                 if (value != null)
359                 {
360                     localMap.put(key, getPageAttribute(key));
361                 }
362             }
363
364             _localMap = localMap;
365         }
366     }
367
368     /** restores local variables */
369
370     protected void restoreLocal()
371     {
372         try
373         {
374             setPageAttribute(VALUEVAR, _valueVarSaved);
375
376             Map localMap = _localMap;
377             
378             if (localMap != null)
379             {
380                 Iterator iterator = localMap.entrySet().iterator();
381
382                 while (iterator.hasNext())
383                 {
384                     Map.Entry entry = (Map.Entry)iterator.next();
385
386                     setPageAttribute(entry.getKey().toString(), entry.getValue());
387                 }
388             }
389         }
390         catch (Exception JavaDoc e)
391         {
392         }
393     }
394
395     /** instantiate value with given className and optional constructor arguments */
396
397     protected Object JavaDoc instantiateValue(String JavaDoc className) throws Exception JavaDoc
398     {
399         if (className.indexOf(':') < 0)
400         {
401             Class JavaDoc c = TypeUtil.forName(className);
402
403             return c.newInstance();
404         }
405         else
406         {
407             List partList = StringUtil.split(className, ':');
408             className = partList.get(0).toString();
409             int argCount = partList.size() - 1;
410             Object JavaDoc[] args = new Object JavaDoc[argCount];
411             for (int i = 0; i < argCount; i ++)
412             {
413                 args[i] = evaluateExpression("constructorArgument", partList.get(i + 1).toString(), Object JavaDoc.class);
414             }
415
416             Class JavaDoc c = TypeUtil.forName(className);
417
418             return TypeUtil.newInstance(c, args);
419         }
420     }
421
422     /** prepares tagValue before tag processing starts. The returned value is assigned to "_value" when tag evaluation starts. */
423
424     protected Object JavaDoc prepareValue(Object JavaDoc tagValue) throws Exception JavaDoc
425     {
426         if (tagValue == null)
427         {
428             String JavaDoc className = _className;
429         
430             if (className != null)
431             {
432                 tagValue = instantiateValue(className);
433             }
434         }
435         
436         String JavaDoc prepareCodec = _prepareCodec;
437         
438         if (prepareCodec != null)
439         {
440             tagValue = applyCodec(prepareCodec, tagValue);
441         }
442
443         return tagValue;
444     }
445
446     /** gets a request-scope stack */
447
448     public static List getStack(PageContext pageContext, String JavaDoc name)
449     {
450         List list = (List)getRequestAttribute(pageContext, name);
451
452         if (list == null)
453         {
454             list = new ArrayList();
455
456             NestedMap rootEntry = new NestedMap();
457             rootEntry.put(PAGECONTEXT, getRequestAttribute(pageContext, ROOTPAGECONTEXT));
458
459             list.add(rootEntry);
460
461             setScopedAttribute(pageContext, name, list, PageContext.REQUEST_SCOPE);
462         }
463
464         return list;
465     }
466
467     /** push to a request-scope stack */
468
469     public static List pushStack(PageContext pageContext, String JavaDoc name, Object JavaDoc object)
470     {
471         List list = getStack(pageContext, name);
472         list.add(object);
473         return list;
474     }
475
476     /** pop from a request-scope stack */
477
478     public static Object JavaDoc popStack(PageContext pageContext, String JavaDoc name)
479     {
480         Object JavaDoc object = null;
481
482         List list = getStack(pageContext, name);
483
484         if (!(list.isEmpty()))
485         {
486             object = list.remove(list.size() - 1);
487         }
488
489         return object;
490     }
491
492     /** get call stack */
493
494     public static List getCallStack(PageContext pageContext)
495     {
496         return getStack(pageContext, CALLSTACK);
497     }
498
499     /** push call stack */
500
501     public List pushCallStack()
502     {
503         NestedMap stackEntry = new NestedMap();
504
505         stackEntry.put(TAG, this);
506         stackEntry.put(PAGECONTEXT, pageContext);
507
508         return pushStack(pageContext, CALLSTACK, stackEntry);
509     }
510
511     /** pull call stack */
512
513     public NestedMap popCallStack()
514     {
515         return (NestedMap)popStack(pageContext, CALLSTACK);
516     }
517
518     /** called before proecessing the body */
519
520     protected void initBody() throws Exception JavaDoc
521     {
522         _isInBody = true;
523
524         pushCallStack();
525     }
526
527     /** called after processing the body */
528
529     protected void afterBody() throws Exception JavaDoc
530     {
531         popCallStack();
532
533         _isInBody = false;
534         _hasBody = true;
535     }
536
537     public void doInitBody() throws JspException
538     {
539         try
540         {
541             super.doInitBody();
542
543             initBody();
544         }
545         catch (Exception JavaDoc e)
546         {
547             rethrow(e);
548         }
549     }
550
551     public int doAfterBody() throws JspException
552     {
553         try
554         {
555             afterBody();
556
557             return super.doAfterBody();
558         }
559         catch (Exception JavaDoc e)
560         {
561             rethrow(e);
562         }
563
564         // dummy
565

566         return 0;
567     }
568
569     public int doStartTag() throws JspException
570     {
571         try
572         {
573             doPrepare();
574
575             saveLocal();
576
577             setPageAttribute(VALUEVAR, _value);
578         }
579         catch (Exception JavaDoc e)
580         {
581             rethrow(e);
582         }
583
584         return EVAL_BODY_BUFFERED;
585     }
586
587     public int doEndTag() throws JspException
588     {
589         try
590         {
591             Object JavaDoc importedValue = doImport(_value);
592
593             /** apply default */
594
595             Object JavaDoc processValue = doDefault(importedValue, _defaultValue, _test);
596
597             /** process value */
598
599             Object JavaDoc processedValue = doProcess(processValue);
600
601             /** assign value */
602
603             boolean isAssigned = doAssign(processedValue);
604
605             _isAssigned = isAssigned;
606
607             /** export value */
608
609             if (doesExport(processedValue, isAssigned))
610             {
611                 doExport(processedValue);
612             }
613         }
614         catch (Throwable JavaDoc e)
615         {
616             rethrow(e);
617         }
618
619         return EVAL_PAGE;
620     }
621
622     /** returns true if tag body needs to be imported. Default implementation returns true if value is null. */
623
624     protected boolean doesImport(Object JavaDoc value)
625     {
626         return (value == null);
627     }
628
629
630     /** imports body into an Object applying importCodec if doesImport() returns true, otherwise returns value itself. */
631
632     protected Object JavaDoc importBody(Object JavaDoc value) throws Exception JavaDoc
633     {
634         if (doesImport(value))
635         {
636             String JavaDoc bodyString = bodyContent.getString();
637
638             if (!isEmptyString(bodyString))
639             {
640                 String JavaDoc importCodec = _importCodec;
641
642                 if (importCodec != null)
643                 {
644                     return applyCodec(importCodec, bodyString);
645                 }
646                 else
647                 {
648                     return bodyString;
649                 }
650             }
651         }
652
653         return value;
654     }
655
656     /** prepares value, called upon tag opening */
657
658     protected void doPrepare() throws Exception JavaDoc
659     {
660         _value = prepareValue(_value);
661     }
662
663     /** apply default logic */
664
665     protected Object JavaDoc doDefault(Object JavaDoc value, Object JavaDoc defaultValue, String JavaDoc test) throws Exception JavaDoc
666     {
667         if (test == null)
668         {
669             if (isEmptyString(value))
670             {
671                 value = defaultValue;
672             }
673         }
674         else
675         {
676             setPageAttribute(VALUEVAR, value);
677
678             if (!TypeUtil.isTrue(EL.applyCodec(pageContext, test, value)))
679             {
680                 value = defaultValue;
681             }
682         }
683
684         return value;
685     }
686
687     /** does 'import' stage and returns 'importedValue'. */
688
689     protected Object JavaDoc doImport(Object JavaDoc tagValue) throws Exception JavaDoc
690     {
691         Object JavaDoc importedValue = tagValue;
692
693         if (_hasBody)
694         {
695             importedValue = importBody(tagValue);
696         }
697
698         return importedValue;
699     }
700
701     /** If true, then exports the value Default implementation returns true if isAssigned is false (i.e., "export when not assigned") */
702    
703     protected boolean doesExport(Object JavaDoc tagValue, boolean isAssigned)
704     {
705         return (("always".equals(_doesExport)) || (!isAssigned));
706     }
707     
708     /** exports given value */
709
710     protected void exportValue(Object JavaDoc tagValue) throws Exception JavaDoc
711     {
712         if (tagValue instanceof Node)
713         {
714             Node node = (Node)tagValue;
715
716             Transformer transformer = TransformerFactory.newInstance().newTransformer();
717             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
718
719             StringWriter writer = new StringWriter();
720
721             Source source = new DOMSource(node);
722             Result result = new StreamResult(writer);
723             
724             transformer.transform(source, result);
725         
726             pageContext.getOut().print(writer.toString());
727         }
728         else if (tagValue instanceof Reader)
729         {
730             IOUtil.copy((Reader)tagValue, pageContext.getOut());
731         }
732         else
733         {
734             pageContext.getOut().print(tagValue);
735         }
736     }
737
738     /** returns value to be exported */
739
740     protected Object JavaDoc getExportValue(Object JavaDoc tagValue) throws Exception JavaDoc
741     {
742         String JavaDoc export = _export;
743
744         if ((export == null) || (export.length() == 0))
745         {
746             return null;
747         }
748
749         if ("_assign".equals(export))
750         {
751             export = _assign;
752         }
753
754         if (export != DEFAULT)
755         {
756             tagValue = evaluateExpression("export", export, Object JavaDoc.class);
757         }
758
759         if (tagValue != null)
760         {
761             String JavaDoc exportCodec = _exportCodec;
762
763             if (exportCodec != null)
764             {
765                 tagValue = applyCodec(exportCodec, tagValue);
766             }
767         }
768
769         return tagValue;
770     }
771
772     /** does 'Export' stage */
773
774     protected void doExport(Object JavaDoc tagValue) throws Exception JavaDoc
775     {
776         setPageAttribute(VALUEVAR, tagValue);
777
778         Object JavaDoc exportObject = getExportValue(tagValue);
779
780         if (exportObject != null)
781         {
782             exportValue(exportObject);
783         }
784     }
785
786     /** returns the closest ancestor tag of given class using callStack for yuzu tag descendants */
787
788     protected Object JavaDoc getAncestorTag(String JavaDoc className)
789     {
790         try
791         {
792             Class JavaDoc c = Class.forName(className);
793             Class JavaDoc yuzuClass = Class.forName(CLASSNAME);
794
795             if (yuzuClass.isAssignableFrom(c))
796             {
797                 List callStack = getCallStack(pageContext);
798
799                 for (int i = callStack.size(); --i >=0 ;)
800                 {
801                     NestedMap entry = (NestedMap)callStack.get(i);
802
803                     YuzuTag parent = (YuzuTag)entry.get(TAG);
804                     boolean isHidden = TypeUtil.isTrue(entry.get(HIDDEN));
805
806                     if ((c.isInstance(parent)) && (!isHidden))
807                     {
808                         return parent;
809                     }
810                 }
811             }
812             else
813             {
814                 Tag parent = this;
815
816                 while ((parent = parent.getParent()) != null)
817                 {
818                     if (c.isInstance(parent))
819                     {
820                         return parent;
821                     }
822                 }
823             }
824         }
825         catch (Exception JavaDoc e)
826         {
827             // ignored
828
}
829             
830         return null;
831     }
832
833     /** tries to set target property when property is not empty. Returns true if assigned, otherwise false */
834
835     protected boolean setTargetProperty(Object JavaDoc targetObject, String JavaDoc targetProperty, Object JavaDoc assignValue) throws Exception JavaDoc
836     {
837         if (!isEmptyString(targetProperty))
838         {
839             BeanUtil.setProperty(targetObject, targetProperty, assignValue);
840             return true;
841         }
842         else
843         {
844             return false;
845         }
846     }
847
848     /** does 'Assign' stage */
849
850     protected boolean doAssign(Object JavaDoc assignValue) throws Exception JavaDoc
851     {
852         setPageAttribute(VALUEVAR, assignValue);
853
854         String JavaDoc assign = _assign;
855
856         if ("_export".equals(assign))
857         {
858             assign = _export;
859         }
860
861         if (assign != DEFAULT)
862         {
863             assignValue = evaluateExpression("assign", assign, Object JavaDoc.class);
864         }
865
866         String JavaDoc assignCodec = _assignCodec;
867
868         if (assignCodec != null)
869         {
870             assignValue = applyCodec(assignCodec, assignValue);
871         }
872
873         boolean isAssigned = false;
874
875         String JavaDoc attribute = _attribute;
876
877         if (attribute != null)
878         {
879             Object JavaDoc ancestorTag = getAncestorTag(CLASSNAME);
880
881             if (ancestorTag != null)
882             {
883                 isAssigned = isAssigned || setTargetProperty(ancestorTag, attribute, assignValue);
884
885                 if ("value".equals(attribute))
886                 {
887                     _valueVarSaved = assignValue;
888                 }
889             }
890         }
891
892         Object JavaDoc targetObject = _target;
893         String JavaDoc property = _property;
894
895         if (RETURNTARGET.equals(targetObject))
896         {
897             HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
898
899             if (request instanceof DispatchHttpServletRequest)
900             {
901                 Object JavaDoc dispatchObject = ((DispatchHttpServletRequest)request).getDispatchObject();
902                 targetObject = TypeUtil.isNestedMap(dispatchObject);
903                 property = "@_return.value";
904
905                 if (assignValue == null)
906                 {
907                     assignValue = "";
908                 }
909             }
910             else
911             {
912                 targetObject = null;
913                 property = null;
914             }
915         }
916
917         if (targetObject instanceof String JavaDoc)
918         {
919             targetObject = evaluateExpression("target", EL.replaceEvalEscape(targetObject.toString()), Object JavaDoc.class);
920         }
921
922         String JavaDoc targetProperty = null;
923
924         List assignValueList = null;
925
926         if (property != null)
927         {
928             targetProperty = property;
929
930             if (targetObject == null)
931             {
932                 targetObject = getAncestorTag(CLASSNAME);
933
934                 if (targetObject != null)
935                 {
936                     targetObject = ((YuzuTag)targetObject)._value;
937                 }
938             }
939
940             if (targetObject != null)
941             {
942                 List targetPropertyList = null;
943
944                 if (!targetProperty.startsWith("#"))
945                 {
946                     targetPropertyList = StringUtil.split(targetProperty, ',');
947                 }
948
949                 if ((targetPropertyList != null) && (targetPropertyList.size() > 1))
950                 {
951                     Iterator targetPropertyIterator = targetPropertyList.iterator();
952
953                     assignValueList = TypeUtil.isList(assignValue);
954
955                     if (assignValueList != null)
956                     {
957                         Iterator assignValueIterator = assignValueList.iterator();
958
959                         while (targetPropertyIterator.hasNext())
960                         {
961                             String JavaDoc subTargetProperty = (String JavaDoc)targetPropertyIterator.next();
962                             Object JavaDoc subAssignValue = null;
963
964                             if (assignValueIterator.hasNext())
965                             {
966                                 subAssignValue = assignValueIterator.next();
967                             }
968
969                             if (!"".equals(subTargetProperty))
970                             {
971                                 isAssigned = isAssigned || setTargetProperty(targetObject, subTargetProperty, subAssignValue);
972                             }
973                         }
974                     }
975                     else
976                     {
977                         while (targetPropertyIterator.hasNext())
978                         {
979                             String JavaDoc subTargetProperty = (String JavaDoc)targetPropertyIterator.next();
980                             if (!("".equals(subTargetProperty)))
981                             {
982                                 isAssigned = isAssigned || setTargetProperty(targetObject, subTargetProperty, assignValue);
983                             }
984                         }
985
986                     }
987                 }
988                 else
989                 {
990                     isAssigned = isAssigned || setTargetProperty(targetObject, targetProperty, assignValue);
991                 }
992             }
993         }
994
995         String JavaDoc var = _var;
996         int scope = _scope;
997
998         if (!isEmptyString(var))
999         {
1000            List varList = StringUtil.split(var, ',');
1001
1002            if (varList.size() > 1)
1003            {
1004                Iterator varListIterator = varList.iterator();
1005
1006                if (assignValueList == null)
1007                {
1008                    assignValueList = TypeUtil.isList(assignValue);
1009                }
1010                
1011                if (assignValueList != null)
1012                {
1013                    Iterator assignValueIterator = assignValueList.iterator();
1014
1015                    while (varListIterator.hasNext())
1016                    {
1017                        String JavaDoc subVar = (String JavaDoc)varListIterator.next();
1018                        
1019                        Object JavaDoc subAssignValue = null;
1020                        
1021                        if (assignValueIterator.hasNext())
1022                        {
1023                            subAssignValue = assignValueIterator.next();
1024                        }
1025                        
1026                        if (!("".equals(subVar)))
1027                        {
1028                            setScopedAttribute(pageContext, subVar, subAssignValue, scope);
1029                            isAssigned = true;
1030                        }
1031                    }
1032                }
1033                else
1034                {
1035                    while (varListIterator.hasNext())
1036                    {
1037                        String JavaDoc subVar = (String JavaDoc)varListIterator.next();
1038                        
1039                        if (!("".equals(subVar)))
1040                        {
1041                            setScopedAttribute(pageContext, subVar, assignValue, scope);
1042                            isAssigned = true;
1043                        }
1044                    }
1045                }
1046            }
1047            else
1048            {
1049                setScopedAttribute(pageContext, var, assignValue, scope);
1050                isAssigned = true;
1051            }
1052        }
1053
1054        return isAssigned;
1055    }
1056
1057    /** processes given value and returns 'processedValue' */
1058
1059    protected Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
1060    {
1061        return tagValue;
1062    }
1063
1064    /* does 'Process' stage */
1065    
1066    protected Object JavaDoc doProcess(Object JavaDoc processValue) throws Exception JavaDoc
1067    {
1068        Object JavaDoc object = processValue(processValue);
1069        
1070        String JavaDoc processCodec = _processCodec;
1071        
1072        if (processCodec != null)
1073        {
1074            object = applyCodec(processCodec, object);
1075        }
1076
1077        return object;
1078    }
1079
1080    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc
1081    {
1082        if (_isInBody)
1083        {
1084            popCallStack();
1085        }
1086
1087        throw t;
1088    }
1089
1090    public void doFinally()
1091    {
1092        cleanup();
1093
1094        restoreLocal();
1095
1096        init();
1097    }
1098
1099    /** Returns true if given object is 'empty' (null, "", empty Collection, or empty array */
1100
1101    public static boolean isEmpty(Object JavaDoc object)
1102    {
1103        return TypeUtil.isEmpty(object);
1104    }
1105
1106    /** Returns true if given object is an empty string (null or "") */
1107
1108    public static boolean isEmptyString(Object JavaDoc object)
1109    {
1110        return TypeUtil.isEmptyString(object);
1111    }
1112
1113    /** Attribute evaluator to be called inside attribute setters.
1114        To allow nested attribute setters, returns given expression as-is
1115        if called inside the body (when _isInBody is true) */

1116
1117    protected Object JavaDoc evaluateAttribute(String JavaDoc name, Object JavaDoc expression, Class JavaDoc valueClass) throws Exception JavaDoc
1118    {
1119        if (_isInBody)
1120        {
1121            return expression;
1122        }
1123        else if (expression != null)
1124        {
1125            return evaluateAttributeExpression(name, expression, valueClass);
1126        }
1127        else
1128        {
1129            return null;
1130        }
1131    }
1132
1133    public void setVar(Object JavaDoc expression) throws Exception JavaDoc
1134    {
1135        _var = (String JavaDoc)evaluateAttribute("var", expression, String JavaDoc.class);
1136    }
1137
1138    public void setScope(Object JavaDoc expression) throws Exception JavaDoc
1139    {
1140        _scope = Util.getScope((String JavaDoc)evaluateAttribute("scope", expression, String JavaDoc.class));
1141    }
1142
1143    public void setTarget(Object JavaDoc expression) throws Exception JavaDoc
1144    {
1145        _target = evaluateAttribute("target", expression, Object JavaDoc.class);
1146    }
1147
1148    public void setProperty(Object JavaDoc expression) throws Exception JavaDoc
1149    {
1150        _property = (String JavaDoc)evaluateAttribute("property", expression, String JavaDoc.class);
1151    }
1152
1153    public void setValue(Object JavaDoc expression) throws Exception JavaDoc
1154    {
1155        _value = evaluateAttribute("value", expression, Object JavaDoc.class);
1156    }
1157
1158    public Object JavaDoc getValue()
1159    {
1160        return _value;
1161    }
1162
1163    public void setDefault(Object JavaDoc expression) throws Exception JavaDoc
1164    {
1165        _defaultValue = evaluateAttribute("default", expression, Object JavaDoc.class);
1166    }
1167
1168    public void setDefaultValue(Object JavaDoc expression) throws Exception JavaDoc
1169    {
1170        setDefault(expression);
1171    }
1172
1173    public void setClassName(Object JavaDoc expression) throws Exception JavaDoc
1174    {
1175        _className = (String JavaDoc)evaluateAttribute("className", expression, EL.class);
1176    }
1177
1178    public void setTest(Object JavaDoc expression) throws Exception JavaDoc
1179    {
1180        _test = (String JavaDoc)evaluateAttribute("test", expression, EL.class);
1181    }
1182
1183    public void setAttribute(Object JavaDoc expression) throws Exception JavaDoc
1184    {
1185        _attribute = (String JavaDoc)evaluateAttribute("attribute", expression, String JavaDoc.class);
1186    }
1187
1188    public void setLocal(Object JavaDoc expression) throws Exception JavaDoc
1189    {
1190        _local = (String JavaDoc)evaluateAttribute("local", expression, String JavaDoc.class);
1191    }
1192
1193    public void setExport(Object JavaDoc expression) throws Exception JavaDoc
1194    {
1195        _export = (String JavaDoc)evaluateAttribute("export", expression, EL.class);
1196    }
1197
1198    public void setAssign(Object JavaDoc expression) throws Exception JavaDoc
1199    {
1200        _assign = (String JavaDoc)evaluateAttribute("assign", expression, EL.class);
1201    }
1202
1203    public void setExportCodec(Object JavaDoc expression) throws Exception JavaDoc
1204    {
1205        _exportCodec = (String JavaDoc)evaluateAttribute("exportCodec", expression, EL.class);
1206    }
1207
1208    public void setAssignCodec(Object JavaDoc expression) throws Exception JavaDoc
1209    {
1210        _assignCodec = (String JavaDoc)evaluateAttribute("assignCodec", expression, EL.class);
1211    }
1212
1213    public void setImportCodec(Object JavaDoc expression) throws Exception JavaDoc
1214    {
1215        _importCodec = (String JavaDoc)evaluateAttribute("importCodec", expression, EL.class);
1216    }
1217
1218    public void setProcessCodec(Object JavaDoc expression) throws Exception JavaDoc
1219    {
1220        _processCodec = (String JavaDoc)evaluateAttribute("processCodec", expression, EL.class);
1221    }
1222
1223    public void setCodec(Object JavaDoc expression) throws Exception JavaDoc
1224    {
1225        setProcessCodec(expression);
1226    }
1227
1228    public void setPrepareCodec(Object JavaDoc expression) throws Exception JavaDoc
1229    {
1230        _prepareCodec = (String JavaDoc)evaluateAttribute("prepareCodec", expression, EL.class);
1231    }
1232
1233    public void setCleanupCodec(Object JavaDoc expression) throws Exception JavaDoc
1234    {
1235        _cleanupCodec = (String JavaDoc)evaluateAttribute("cleanupCodec", expression, EL.class);
1236    }
1237
1238    public void setDoesExport(Object JavaDoc expression) throws Exception JavaDoc
1239    {
1240        _doesExport = (String JavaDoc)evaluateAttribute("doesExport", expression, String JavaDoc.class);
1241    }
1242}
1243
Popular Tags