KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
21
22 import com.finalist.jag.util.*;
23 import com.finalist.jag.JagException;
24 import com.finalist.jag.template.*;
25 import com.finalist.jag.taglib.*;
26
27 /**
28  * Class TagEngine
29  *
30  *
31  * @author Wendel D. de Witte
32  * @version %I%, %G%
33  */

34 public class TagEngine implements TagConstants {
35
36    /** Field pageContext */
37    private PageContext pageContext = null;
38
39    /** Field processMap */
40    private HashMap processMap = null;
41
42    /** Field libraryMap */
43    private HashMap libraryMap = new HashMap();
44
45    /** Field stackValidatorList */
46    private List stackValidatorList = new LinkedList();
47
48    /** Field bodyContents */
49    private Stack bodyContents = new Stack();
50
51    /** Field templateData */
52    private TemplateStructure templateData = null;
53
54    /** Field currentBody */
55    private String JavaDoc currentBody;
56
57
58    /**
59     * Constructor TagEngine
60     *
61     *
62     */

63    public TagEngine() {
64    }
65
66
67    /**
68     * Method processTags
69     *
70     *
71     * @param pageContext
72     * @param templateData
73     *
74     */

75    public void processTags(PageContext pageContext) {
76       this.processMap = new HashMap();
77       this.pageContext = pageContext;
78       this.templateData = pageContext.getTemplateData();
79
80       processChildTags(templateData.getRoot());
81    }
82
83
84    /**
85     * Method processChildTags
86     *
87     *
88     * @param parentNode
89     *
90     */

91    private void processChildTags(TemplateTreeItem parentNode) {
92       TreeItem childNode = parentNode.getFirstChild();
93
94       for (; childNode != null; childNode = childNode.getNextSibling()) {
95          TemplateTag tagRef = ((TemplateTreeItem) childNode).getTag();
96          if ((tagRef == null) || tagRef.isProcessed())
97             continue;
98
99          tagRef.setPageContext(pageContext);
100
101          try {
102             if (!processTag((TemplateTreeItem) childNode))
103                return;
104          }
105          catch (JagException exc) {
106             Log.log(exc.getMessage());
107          }
108       }
109    }
110
111
112    /**
113     * Method processTag
114     *
115     *
116     * @param tagItemNode
117     *
118     * @return
119     *
120     * @throws JagException
121     *
122     */

123    public boolean processTag(TemplateTreeItem tagItemNode)
124       throws JagException {
125       TagProcess tagProcess = getTagProcess(tagItemNode);
126       tagProcess.setBodyText(getBodyText(tagItemNode));
127
128       switch (tagProcess.process()) {
129          case EVAL_BODY_TAG:
130             // Continue evaluating the body of a tag.
131
putBodyContent(tagItemNode);
132             processChildTags(tagItemNode);
133             processTag(tagItemNode);
134             break;
135          case SKIP_CLEAR_BODY:
136             // clear the clearBodyText.
137
clearBodyText(tagItemNode);
138          case SKIP_BODY:
139             // Continue evaluating the tag.
140
popBodyContent(tagItemNode);
141             processTag(tagItemNode);
142             break;
143          case EVAL_PAGE:
144             // Continue evaluating the page.
145
break;
146          case SKIP_PAGE:
147             // Skip the rest of the page.
148
return false;
149       }
150       return true;
151    }
152
153
154    /**
155     * Method getTagProcess
156     *
157     *
158     * @param tagref
159     *
160     * @return
161     *
162     * @throws JagException
163     *
164     */

165    protected TagProcess getTagProcess(TemplateTreeItem tagItemNode)
166       throws JagException {
167       TemplateTag tagref = tagItemNode.getTag();
168       TagProcess tagProcess = (TagProcess) processMap.get(tagref);
169
170       if (tagProcess == null) {
171          PageContext pageContext = tagref.getPageContext();
172          TemplateHeaderCollection headers = pageContext.getHeaderCollection();
173          UrlHeaderLine urlHeader = headers.getHeaderUrl(tagref.getTagLib());
174          TagLibrary tagLibrary = getLibrary(urlHeader.getUrl());
175          TagDef tagdef = tagLibrary.getTagDef(tagref);
176
177          tagref.setTagDefinition(tagdef);
178          tagProcess = new TagProcess(tagref);
179          processMap.put(tagref, tagProcess);
180          pushBodyContent(tagItemNode);
181       }
182       return tagProcess;
183    }
184
185
186    /**
187     * Method getBodyText
188     *
189     *
190     * @param tagItemNode
191     *
192     */

193    protected String JavaDoc getBodyText(TemplateTreeItem tagItemNode) {
194       TemplateStructure bodyStructure = new TemplateStructure();
195       bodyStructure.setRoot(tagItemNode);
196       StringBuffer JavaDoc s = bodyStructure.getTextBlockList().getStringBuffer();
197       return new String JavaDoc(s);
198    }
199
200
201    /**
202     * Method clearBodyText
203     *
204     *
205     * @return
206     *
207     */

208    protected void clearBodyText(TemplateTreeItem parentItem) {
209       TemplateStructure bodyStructure = new TemplateStructure();
210       bodyStructure.setRoot(parentItem);
211       bodyStructure.clearBodyText();
212    }
213
214
215    /**
216     * Method getLibrary
217     *
218     *
219     * @param url
220     *
221     * @return
222     *
223     * @throws JagException
224     *
225     */

226    protected TagLibrary getLibrary(String JavaDoc url) throws JagException {
227       TagLibrary library = (TagLibrary) libraryMap.get(url);
228       if (library == null) {
229          TagLibraryLoader libLoader = new TagLibraryLoader(new java.io.File JavaDoc(url));
230          library = libLoader.getTagLibrary();
231          Exception JavaDoc exc = libLoader.getException();
232
233          if (exc != null)
234             throw new JagException(exc.toString());
235          libraryMap.put(url, library);
236       }
237       return library;
238    }
239
240
241    /**
242     * Method pushBodyContent
243     *
244     *
245     * @param tagItemNode
246     *
247     */

248    protected void pushBodyContent(TemplateTreeItem tagItemNode) {
249       TemplateTag tagRef = tagItemNode.getTag();
250       TreeItem childNode = tagItemNode.getFirstChild();
251
252       if (childNode == null)
253          return;
254       if (tagRef.getClosingTextBuffer() == null)
255          return;
256
257       TemplateTextBlock closingTextBlock = tagRef.getClosingTextBuffer();
258       TemplateTreeItem firstBlock = (TemplateTreeItem) childNode;
259       TemplateTreeItem closeBlock = templateData.getTemplateTreeItem(closingTextBlock);
260       TemplateTreeItem lastBlock = (TemplateTreeItem) closeBlock.getPrevSibling();
261       TemplateStructure bodyStructure = templateData.cut(firstBlock, lastBlock);
262
263       BodyContent tagBody = new BodyContent();
264       tagBody.setBodyStructure(bodyStructure);
265       bodyContents.push(tagBody);
266       stackValidatorList.add(tagItemNode);
267    }
268
269
270    /**
271     * Method putBodyContent
272     *
273     *
274     * @param parentItem
275     *
276     */

277    protected void putBodyContent(TemplateTreeItem parentItem) {
278       BodyContent bodyContent = peekBodyContent();
279       TemplateStructure bodyData = bodyContent.getBodyStructure();
280       TemplateTreeItem rootItem = bodyData.getRoot();
281       TreeItem childItem = rootItem.getFirstChild();
282       TreeItem closingItem = parentItem.disconnectLastChild();
283       TemplateTreeItem textItem = new TemplateTreeItem();
284
285       parentItem.addChild(childItem);
286       parentItem.addChild(textItem);
287       parentItem.addChild(closingItem);
288
289       if (parentItem.getTag() != null)
290          parentItem.getTag().setTextBuffer(textItem.getTextBlock());
291    }
292
293
294    /**
295     * Method peekBodyContent
296     *
297     *
298     * @return
299     *
300     */

301    protected BodyContent peekBodyContent() {
302       BodyContent copyBody = new BodyContent((BodyContent) bodyContents.peek());
303       return copyBody;
304    }
305
306
307    /**
308     * Method popBodyContent
309     *
310     *
311     * @return
312     *
313     */

314    protected BodyContent popBodyContent(TemplateTreeItem parentItem) {
315       if (stackValidatorList.remove(parentItem)) {
316          return (BodyContent) bodyContents.pop();
317       }
318       // prevention against nullpointers;
319
return new BodyContent();
320    }
321
322 }
Popular Tags