KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > template > Template


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 package com.finalist.jaggenerator.template;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 /**
32  * This class represents a JAG 'generation template'. A generation template is in fact a directory
33  * containing a collection of files and subdirectories. The template directory must have a "template.xml"
34  * configuration file, which contains information about the template and what configuration options are to be
35  * presented to the user.
36  *
37  * @author Michael O'Connor - Finalist IT Group
38  */

39 public class Template {
40
41    private static final String JavaDoc XMLTAG_JAG_TEMPLATE = "jag-template";
42    private static final String JavaDoc TEMPLATE_XML = "template.xml";
43    private static final String JavaDoc ID_ATTRIBUTE = "id";
44    private static final String JavaDoc NAME_ATTRIBUTE = "name";
45    private static final String JavaDoc[] STRING_ARRAY = new String JavaDoc[0];
46    private static final TemplateConfigParameter[] TCP_ARRAY = new TemplateConfigParameter[0];
47    private static final String JavaDoc XMLTAG_PARAM = "param";
48    private static final String JavaDoc DESCRIPTION_ATTRIBUTE = "description";
49    private static final String JavaDoc TYPE_ATTRIBUTE = "type";
50    private static final String JavaDoc XMLTAG_VALUE = "value";
51    private static final String JavaDoc ENGINE_ATTRIBUTE = "template-engine";
52    /** The default template engine class. */
53    public static final String JavaDoc DEFAULT_ENGINE_CLASS = "com.finalist.jag.util.VelocityTemplateEngine";
54
55    private String JavaDoc name;
56    private String JavaDoc description;
57    private String JavaDoc engine;
58    private File JavaDoc templateDir;
59    private Document JavaDoc doc;
60    private TemplateConfigParameter[] configParams;
61
62
63    public Template(File JavaDoc templateDir) throws TemplateConfigException {
64       this.templateDir = templateDir;
65       load(new File JavaDoc(templateDir, TEMPLATE_XML));
66    }
67
68    /**
69     * Gets the name of this template.
70     * @return the name.
71     */

72    public String JavaDoc getName() {
73       return name;
74    }
75
76    /**
77     * Gets the base directory of this template.
78     * @return the base directory.
79     */

80    public File JavaDoc getTemplateDir() {
81       return templateDir;
82    }
83
84     /**
85      * Sets the base directory of this template.
86      */

87     public void setTemplateDir(File JavaDoc templateDir) {
88        this.templateDir = templateDir;
89     }
90
91    public String JavaDoc getDescription() {
92       return description;
93    }
94
95    public String JavaDoc getEngine() {
96       return engine;
97    }
98
99    public String JavaDoc getEngineClass() {
100       return DEFAULT_ENGINE_CLASS;
101    }
102
103    /**
104     * Gets the configuration paramaters defined for this template.
105     * @return the config paramseters.
106     */

107    public TemplateConfigParameter[] getConfigParams() {
108       return configParams;
109    }
110
111    public String JavaDoc toString() {
112       return name;
113    }
114
115
116    /**
117     * Reads in the template information from the XML file.
118     */

119    private void load(File JavaDoc xmlFile) throws TemplateConfigException {
120       DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
121       DocumentBuilder JavaDoc builder = null;
122       try {
123          builder = dbf.newDocumentBuilder();
124          doc = builder.parse(xmlFile);
125          Element JavaDoc root = (Element JavaDoc) doc.getElementsByTagName(XMLTAG_JAG_TEMPLATE).item(0);
126          if (root == null) {
127             throw new SAXException JavaDoc("");
128          }
129
130          name = root.getAttribute(NAME_ATTRIBUTE);
131          description = root.getAttribute(DESCRIPTION_ATTRIBUTE);
132          engine = root.getAttribute(ENGINE_ATTRIBUTE);
133
134          //read in all the config params defined in this template..
135
ArrayList JavaDoc beans = new ArrayList JavaDoc();
136          NodeList JavaDoc params = root.getElementsByTagName(XMLTAG_PARAM);
137          for (int i = 0; i < params.getLength(); i++) {
138             Element JavaDoc parameter = (Element JavaDoc) params.item(i);
139             TemplateConfigParameter bean = new TemplateConfigParameter();
140             bean.setId(parameter.getAttribute(ID_ATTRIBUTE));
141             bean.setName(parameter.getAttribute(NAME_ATTRIBUTE));
142             bean.setDescription(parameter.getAttribute(DESCRIPTION_ATTRIBUTE));
143             bean.setType(TemplateConfigParameter.getTypeByName(parameter.getAttribute(TYPE_ATTRIBUTE)));
144             ArrayList JavaDoc temp = new ArrayList JavaDoc();
145             NodeList JavaDoc presetValues = parameter.getElementsByTagName(XMLTAG_VALUE);
146             for (int j = 0; j < presetValues.getLength(); j++) {
147                Element JavaDoc value = (Element JavaDoc) presetValues.item(j);
148                temp.add(value.getFirstChild().getNodeValue());
149             }
150             bean.setPresetValues((String JavaDoc[]) temp.toArray(STRING_ARRAY));
151             beans.add(bean);
152          }
153          configParams = (TemplateConfigParameter[]) beans.toArray(TCP_ARRAY);
154
155       } catch (ParserConfigurationException JavaDoc e) {
156          throw new TemplateConfigException("System error reading 'template.xml' : " + e);
157
158       } catch (SAXException JavaDoc e) {
159          throw new TemplateConfigException("The chosen template's 'template.xml' is invalid.");
160
161       } catch (IOException JavaDoc e) {
162          throw new TemplateConfigException("JAG can't locate the template's 'template.xml'.");
163       }
164    }
165
166 }
167
Popular Tags