KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > TagProcess


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag;
19
20
21 import com.finalist.jag.template.*;
22 import com.finalist.jag.template.parser.*;
23 import com.finalist.jag.taglib.*;
24 import org.apache.commons.beanutils.ConvertUtils;
25 import org.apache.commons.beanutils.PropertyUtils;
26
27
28
29 /**
30  * Class TagProcess
31  *
32  *
33  * @author Wendel D. de Witte
34  * @version %I%, %G%
35  */

36 public class TagProcess implements TagConstants {
37
38    /** Field tagInstance */
39    private TagSupport tagInstance = null;
40
41    /** Field tagRef */
42    private TemplateTag tagRef = null;
43
44    /** Field process */
45    private int process = EVAL_PAGE;
46
47    /** Field bodyText */
48    private String JavaDoc bodyText;
49
50
51    /**
52     * Constructor TagProcess
53     *
54     *
55     * @param tagRef
56     *
57     */

58    public TagProcess(TemplateTag tagRef) {
59       this.tagRef = tagRef;
60    }
61
62
63    /**
64     * Method process
65     *
66     *
67     * @return
68     *
69     * @throws JagException
70     *
71     */

72    public int process() throws JagException {
73
74       if (tagInstance == null) {
75          try {
76             TagDef tagDef = tagRef.getTagDefinition();
77             Object JavaDoc object =
78                Class.forName(tagDef.getTagClass()).newInstance();
79
80             tagInstance = (TagSupport) object;
81
82             // set support properties
83
tagInstance.setPageContext(tagRef.getPageContext());
84
85             // Set tag properties.
86
JagParameter[] params = tagRef.getParamArray();
87
88             for (int i = 0; i < params.length; i++) {
89                String JavaDoc sIdent = params[i].getIdent();
90                String JavaDoc sValue = params[i].getValue();
91                Object JavaDoc value = ConvertUtils.convert(sValue, String JavaDoc.class);
92                PropertyUtils.setProperty(tagInstance, sIdent, value);
93             }
94
95             // Validate required properties.
96
AttributeDef[] tagAttributes = tagDef.getAttributeDefArray();
97
98             for (int i = 0; i < tagAttributes.length; i++) {
99                if (!tagAttributes[i].getRequired()) {
100                   continue;
101                }
102
103                String JavaDoc sPropertyName = tagAttributes[i].getName();
104                Object JavaDoc value = PropertyUtils.getProperty(tagInstance, sPropertyName);
105
106                if ((value == null) || (value.toString().length() < 1)) {
107                   throw new JagException("ERROR: Can't find '" + sPropertyName + "' in " + tagInstance);
108                }
109             }
110          }
111          catch (Exception JavaDoc exc) {
112             throw new JagException(exc.toString());
113          }
114
115          if (tagInstance == null) {
116             return getProcess();
117          }
118
119          if (!tagRef.isProcessed()) {
120             tagRef.setProcessed(true);
121             tagRef.clearCloseTag();
122             tagRef.clearTag();
123          }
124       }
125       // Reset writer. TagEngine has the option to change the TextBuffer of the tag.
126
JagTextBlockWriter writer = new JagTextBlockWriter(tagRef.getTextBuffer());
127       tagInstance.setWriter(writer);
128
129       process(tagInstance);
130
131       return getProcess();
132    }
133
134
135    /**
136     * Method process
137     *
138     *
139     * @param tagInstance
140     *
141     * @throws JagException
142     *
143     */

144    public void process(TagSupport tagInstance) throws JagException {
145
146       if (process == EVAL_PAGE) {
147          process = tagInstance.doStartTag();
148
149          if (process == SKIP_PAGE) {
150             return;
151          }
152       }
153
154       if ((process != SKIP_BODY && process != SKIP_CLEAR_BODY)
155          && (tagInstance instanceof TagBodySupport)) {
156          TagBodySupport bodyTag = (TagBodySupport) tagInstance;
157
158          bodyTag.setBodyText(bodyText);
159
160          if (process == EVAL_PAGE) {
161             bodyTag.doInitBodyTag();
162          }
163
164          process = bodyTag.doAfterBodyTag();
165
166          if (process == EVAL_BODY_TAG) {
167             return;
168          }
169
170          if (process == SKIP_BODY || process == SKIP_CLEAR_BODY) {
171             return;
172          }
173       }
174
175       process = tagInstance.doEndTag();
176
177       tagInstance.release();
178    }
179
180
181    /**
182     * Method getProcess
183     *
184     *
185     * @return
186     *
187     */

188    public int getProcess() {
189       return process;
190    }
191
192
193    /**
194     * Method setBodyText
195     *
196     *
197     * @param bodyText
198     *
199     */

200    public void setBodyText(String JavaDoc bodyText) {
201       this.bodyText = bodyText;
202    }
203 }
204
205 ;
Popular Tags