KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > TemplateStructureFactory


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

32 public class TemplateStructureFactory implements JagParserConstants {
33
34    private TemplateStructure templateTree = null;
35
36
37    /**
38     * Method create
39     *
40     *
41     * @param jagblocks
42     *
43     */

44    public void create(JagBlockCollection jagblocks) {
45
46       templateTree = new TemplateStructure();
47       createTree(templateTree.getRoot(), jagblocks);
48    }
49
50
51    public TemplateStructure getTemplateStructure() {
52       return templateTree;
53    }
54
55
56    /**
57     * Method createParamList
58     *
59     *
60     * @param block
61     *
62     * @return
63     *
64     */

65    protected JagParameter[] createParamList(JagBlock block) {
66
67       Collection paramList = new ArrayList();
68       JagBlock childBlock[] = {block.getFirstChild(), null};
69
70       while (childBlock[0] != null) {
71          if (childBlock[0].getType() == PARAMDEF) {
72             String JavaDoc ident = null, value = null;
73
74             childBlock[1] = childBlock[0].getFirstChild();
75             while (childBlock[1] != null) {
76                if (childBlock[1].getType() == IDENT) {
77                   ident = childBlock[1].getText();
78                }
79                else if (childBlock[1].getType() == STRING) {
80                   value = childBlock[1].getText();
81                }
82                childBlock[1] = childBlock[1].getNextSibling();
83             }
84
85             if ((ident != null) && (value != null)) {
86                paramList.add(new JagParameter(ident, value));
87             }
88          }
89          childBlock[0] = childBlock[0].getNextSibling();
90       }
91
92       return (JagParameter[])
93          paramList.toArray(new JagParameter[paramList.size()]);
94    }
95
96
97    //////////////////////////////////////////////////////////////////////////////////
98
protected void createTree(TemplateTreeItem parentItem, JagBlock block) {
99       JagBlock childBlock = block.getFirstChild();
100
101       for (; childBlock != null; childBlock = childBlock.getNextSibling()) {
102          if (childBlock.getType() == HEADERDEF_BEGIN) {
103             // skip end of line symbol
104
trimFrontEOLSymbol(childBlock.getNextSibling());
105             continue;
106          }
107
108          TemplateTextBlock textBlock = new TemplateTextBlock(childBlock.getText());
109          TemplateTreeItem newItem = new TemplateTreeItem();
110          newItem.setTextBlock(textBlock);
111          parentItem.addChild(newItem);
112
113          if (childBlock.getType() == TEXT) {
114             continue;
115          }
116          else if (childBlock.getType() == TAGSTART) {
117             TemplateTag tag = createTag(newItem, childBlock);
118             if (tag != null)
119                newItem.setTag(tag);
120          }
121       }
122    }
123
124
125    /**
126     * Method trimFrontEOLSymbol
127     *
128     *
129     * @param block
130     *
131     * @return
132     *
133     */

134    protected void trimFrontEOLSymbol(JagBlock block) {
135       if (block == null) return;
136
137       String JavaDoc text = block.getText();
138       if (text.length() > 0 && text.charAt(0) == '\r')
139          text = text.substring(1);
140       if (text.length() > 0 && text.charAt(0) == '\n')
141          text = text.substring(1);
142       block.setText(text);
143    }
144
145
146    /**
147     * Method createTag
148     *
149     *
150     * @param block
151     * @param textBlock
152     *
153     * @return
154     *
155     */

156    protected TemplateTag createTag(TemplateTreeItem parentItem,
157       JagBlock block) {
158
159       TemplateTextBlock textBlock = parentItem.getTextBlock();
160       JagBlock blockChild = block.getFirstChild();
161       String JavaDoc tagName = null;
162       String JavaDoc tagAction = null;
163       JagBlock blockSLIST = null;
164       JagBlock blockTagEnd = null;
165       TemplateTag tag = null;
166
167       while (blockChild != null) {
168          if (blockChild.getType() == TAGNAME) {
169             tagName = blockChild.getText();
170          }
171          else if (blockChild.getType() == TAGACTION) {
172             tagAction = blockChild.getText();
173          }
174          else if (blockChild.getType() == SLIST) {
175             blockSLIST = blockChild;
176          }
177          else if (blockChild.getType() == TAGEND) {
178             blockTagEnd = blockChild;
179          }
180
181          blockChild = blockChild.getNextSibling();
182       }
183
184       if ((tagName != null) && (tagAction != null)) {
185          JagParameter[] paramArray = createParamList(block);
186
187          tag = new TemplateTag(tagName, tagAction, paramArray, textBlock);
188       }
189
190       if (blockSLIST != null) {
191          createTree(parentItem, blockSLIST);
192       }
193
194       if (blockTagEnd != null) {
195          TemplateTextBlock endBuffer = new TemplateTextBlock(blockTagEnd.getText());
196          TemplateTreeItem newItem = new TemplateTreeItem();
197          newItem.setTextBlock(endBuffer);
198          parentItem.addChild(newItem);
199          tag.setClosingTextBuffer(endBuffer);
200       }
201
202       return tag;
203    }
204 }
Popular Tags