KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > XmlForEachTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.el;
30
31 import com.caucho.jsp.PageContextImpl;
32 import com.caucho.util.L10N;
33 import com.caucho.xpath.Env;
34 import com.caucho.xpath.Expr;
35 import com.caucho.xpath.XPath;
36
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.tagext.IterationTag JavaDoc;
42 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 /**
48  * Tag representing a "for each" condition.
49  */

50 public class XmlForEachTag extends TagSupport JavaDoc implements IterationTag JavaDoc {
51   private static L10N L = new L10N(XmlIfTag.class);
52   
53   private Expr _select;
54   private String JavaDoc _var;
55   private String JavaDoc _varStatus;
56   private Node JavaDoc _oldEnv;
57
58   private int _begin;
59   private int _end = Integer.MAX_VALUE / 2;
60   private int _step = 1;
61
62   private Iterator JavaDoc _iterator;
63
64   /**
65    * Sets the XPath select value.
66    */

67   public void setSelect(Expr select)
68   {
69     _select = select;
70   }
71
72   /**
73    * Sets the variable which should contain the result of the test.
74    */

75   public void setVar(String JavaDoc var)
76   {
77     _var = var;
78   }
79
80   /**
81    * Sets the variable which should contain the result of the test.
82    */

83   public void setVarStatus(String JavaDoc varStatus)
84   {
85     _varStatus = varStatus;
86   }
87
88   /**
89    * Sets the begin value.
90    */

91   public void setBegin(int begin)
92   {
93     _begin = begin;
94   }
95
96   /**
97    * Sets the end value.
98    */

99   public void setEnd(int end)
100   {
101     _end = end;
102   }
103
104   /**
105    * Sets the step value.
106    */

107   public void setStep(int step)
108   {
109     _step = step;
110   }
111
112   /**
113    * Process the tag.
114    */

115   public int doStartTag()
116     throws JspException JavaDoc
117   {
118     try {
119       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
120       Env env = XPath.createEnv();
121       env.setVarEnv(pageContext.getVarEnv());
122       
123       _oldEnv = pageContext.getNodeEnv();
124       Object JavaDoc obj = _select.evalObject(_oldEnv, env);
125
126       env.free();
127
128       if (obj == null)
129         return SKIP_BODY;
130
131       if (obj instanceof Iterator JavaDoc)
132         _iterator = (Iterator JavaDoc) obj;
133       else if (obj instanceof Collection JavaDoc)
134         _iterator = ((Collection JavaDoc) obj).iterator();
135       else if (obj instanceof NodeList JavaDoc) {
136     NodeList JavaDoc nodeList = (NodeList JavaDoc) obj;
137         ArrayList JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
138
139     for (int i = 0; i < nodeList.getLength(); i++)
140       list.add(nodeList.item(i));
141
142         _iterator = list.iterator();
143       }
144       else {
145         ArrayList JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
146         list.add(obj);
147
148         _iterator = list.iterator();
149       }
150
151       if (! _iterator.hasNext())
152         return SKIP_BODY;
153
154       Object JavaDoc value = _iterator.next();
155
156       if (_var != null)
157         pageContext.setAttribute(_var, value);
158
159       if (value instanceof Node JavaDoc)
160     pageContext.setNodeEnv((Node JavaDoc) value);
161
162       return EVAL_BODY_INCLUDE;
163     } catch (Exception JavaDoc e) {
164       throw new JspException JavaDoc(e);
165     }
166   }
167
168   /**
169    * Process the loop.
170    */

171   public int doAfterBody()
172     throws JspException JavaDoc
173   {
174     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
175     
176     if (! _iterator.hasNext()) {
177       pageContext.setNodeEnv(_oldEnv);
178       return SKIP_BODY;
179     }
180
181     Object JavaDoc value = _iterator.next();
182
183     if (_var != null)
184       pageContext.setAttribute(_var, value);
185     if (value instanceof Node JavaDoc)
186       pageContext.setNodeEnv((Node JavaDoc) value);
187
188     return EVAL_BODY_AGAIN;
189   }
190 }
191
Popular Tags