KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public class TemplateHeaderFactory implements JagParserConstants {
34
35    /** Field headers */
36    private TemplateHeaderCollection headers = null;
37
38
39    /**
40     * Method getHeaderCollection
41     *
42     *
43     * @return
44     *
45     */

46    public TemplateHeaderCollection getHeaderCollection() {
47       return headers;
48    }
49
50
51    /**
52     * Method create
53     *
54     *
55     * @param jagblocks
56     *
57     * @return
58     *
59     */

60    public TemplateHeaderCollection create(JagBlockCollection jagblocks) {
61
62       headers = null;
63
64       Collection headerLines = new ArrayList();
65       JagBlock childBlock[] = {jagblocks.getFirstChild(), null, null};
66
67       for (; childBlock[0] != null; childBlock[0] = childBlock[0].getNextSibling()) {
68          if (childBlock[0].getType() != HEADERDEF_BEGIN) {
69             continue;
70          }
71
72          Collection identList = new ArrayList();
73          Collection paramList = new ArrayList();
74
75          childBlock[1] = childBlock[0].getFirstChild();
76
77          while (childBlock[1] != null) {
78             if (childBlock[1].getType() == PARAMDEF) {
79                String JavaDoc ident = null, value = null;
80
81                childBlock[2] = childBlock[1].getFirstChild();
82
83                while (childBlock[2] != null) {
84                   if (childBlock[2].getType() == IDENT) {
85                      ident = childBlock[2].getText();
86                   }
87                   else if (childBlock[2].getType() == STRING) {
88                      value = childBlock[2].getText();
89                   }
90
91                   childBlock[2] = childBlock[2].getNextSibling();
92                }
93
94                if ((ident != null) && (value != null)) {
95                   paramList.add(new JagParameter(ident, value));
96                }
97             }
98             else if (childBlock[1].getType() == IDENT) {
99                identList.add(childBlock[1].getText());
100             }
101
102             childBlock[1] = childBlock[1].getNextSibling();
103          }
104
105          String JavaDoc[] identArray = (String JavaDoc[]) identList.toArray(new String JavaDoc[identList.size()]);
106          JagParameter[] paramArray = (JagParameter[]) paramList.toArray(new JagParameter[paramList.size()]);
107
108          headerLines.add(create(identArray, paramArray));
109       }
110
111       headers = new TemplateHeaderCollection((HeaderLine[]) headerLines
112          .toArray(new HeaderLine[headerLines.size()]));
113
114       return headers;
115    }
116
117
118    /**
119     * Method create
120     *
121     *
122     * @param identArray
123     * @param paramArray
124     *
125     * @return
126     *
127     */

128    protected HeaderLine create(String JavaDoc[] identArray,
129       JagParameter[] paramArray) {
130
131       HeaderLine headerLine = null;
132
133       for (int i = 0; i < identArray.length; i++) {
134          String JavaDoc ident = identArray[i];
135
136          if (ident.equals("taglib")) {
137             headerLine = createUrlHeaderLine(identArray, paramArray);
138
139             if (headerLine != null) {
140                return headerLine;
141             }
142          }
143       }
144
145       if (headerLine == null) {
146          headerLine = new HeaderLine(identArray, paramArray);
147       }
148
149       return headerLine;
150    }
151
152
153    /**
154     * Method createUrlHeaderLine
155     *
156     *
157     * @param identArray
158     * @param paramArray
159     *
160     * @return
161     *
162     */

163    protected HeaderLine createUrlHeaderLine(String JavaDoc[] identArray,
164       JagParameter[] paramArray) {
165
166       // assert(identArray[0].equals("taglib"));
167
String JavaDoc url = null;
168       String JavaDoc prefix = null;
169
170       for (int i = 0; i < paramArray.length; i++) {
171          if ((url == null) && paramArray[i].getIdent().equals("uri")) {
172             url = paramArray[i].getValue();
173          }
174          else if ((prefix == null) && paramArray[i].getIdent().equals("prefix")) {
175             prefix = paramArray[i].getValue();
176          }
177       }
178
179       return ((url != null) && (prefix != null))
180          ? new UrlHeaderLine(identArray, paramArray, url, prefix)
181          : null;
182    }
183 }
Popular Tags