001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017: package com.finalist.jaggenerator.template;
018:
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.NodeList;
022: import org.xml.sax.SAXException;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.ArrayList;
030:
031: /**
032: * This class represents a JAG 'generation template'. A generation template is in fact a directory
033: * containing a collection of files and subdirectories. The template directory must have a "template.xml"
034: * configuration file, which contains information about the template and what configuration options are to be
035: * presented to the user.
036: *
037: * @author Michael O'Connor - Finalist IT Group
038: */
039: public class Template {
040:
041: private static final String XMLTAG_JAG_TEMPLATE = "jag-template";
042: private static final String TEMPLATE_XML = "template.xml";
043: private static final String ID_ATTRIBUTE = "id";
044: private static final String NAME_ATTRIBUTE = "name";
045: private static final String[] STRING_ARRAY = new String[0];
046: private static final TemplateConfigParameter[] TCP_ARRAY = new TemplateConfigParameter[0];
047: private static final String XMLTAG_PARAM = "param";
048: private static final String DESCRIPTION_ATTRIBUTE = "description";
049: private static final String TYPE_ATTRIBUTE = "type";
050: private static final String XMLTAG_VALUE = "value";
051: private static final String ENGINE_ATTRIBUTE = "template-engine";
052: /** The default template engine class. */
053: public static final String DEFAULT_ENGINE_CLASS = "com.finalist.jag.util.VelocityTemplateEngine";
054:
055: private String name;
056: private String description;
057: private String engine;
058: private File templateDir;
059: private Document doc;
060: private TemplateConfigParameter[] configParams;
061:
062: public Template(File templateDir) throws TemplateConfigException {
063: this .templateDir = templateDir;
064: load(new File(templateDir, TEMPLATE_XML));
065: }
066:
067: /**
068: * Gets the name of this template.
069: * @return the name.
070: */
071: public String getName() {
072: return name;
073: }
074:
075: /**
076: * Gets the base directory of this template.
077: * @return the base directory.
078: */
079: public File getTemplateDir() {
080: return templateDir;
081: }
082:
083: /**
084: * Sets the base directory of this template.
085: */
086: public void setTemplateDir(File templateDir) {
087: this .templateDir = templateDir;
088: }
089:
090: public String getDescription() {
091: return description;
092: }
093:
094: public String getEngine() {
095: return engine;
096: }
097:
098: public String getEngineClass() {
099: return DEFAULT_ENGINE_CLASS;
100: }
101:
102: /**
103: * Gets the configuration paramaters defined for this template.
104: * @return the config paramseters.
105: */
106: public TemplateConfigParameter[] getConfigParams() {
107: return configParams;
108: }
109:
110: public String toString() {
111: return name;
112: }
113:
114: /**
115: * Reads in the template information from the XML file.
116: */
117: private void load(File xmlFile) throws TemplateConfigException {
118: DocumentBuilderFactory dbf = DocumentBuilderFactory
119: .newInstance();
120: DocumentBuilder builder = null;
121: try {
122: builder = dbf.newDocumentBuilder();
123: doc = builder.parse(xmlFile);
124: Element root = (Element) doc.getElementsByTagName(
125: XMLTAG_JAG_TEMPLATE).item(0);
126: if (root == null) {
127: throw new SAXException("");
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 beans = new ArrayList();
136: NodeList params = root.getElementsByTagName(XMLTAG_PARAM);
137: for (int i = 0; i < params.getLength(); i++) {
138: Element parameter = (Element) 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
143: .getAttribute(DESCRIPTION_ATTRIBUTE));
144: bean.setType(TemplateConfigParameter
145: .getTypeByName(parameter
146: .getAttribute(TYPE_ATTRIBUTE)));
147: ArrayList temp = new ArrayList();
148: NodeList presetValues = parameter
149: .getElementsByTagName(XMLTAG_VALUE);
150: for (int j = 0; j < presetValues.getLength(); j++) {
151: Element value = (Element) presetValues.item(j);
152: temp.add(value.getFirstChild().getNodeValue());
153: }
154: bean.setPresetValues((String[]) temp
155: .toArray(STRING_ARRAY));
156: beans.add(bean);
157: }
158: configParams = (TemplateConfigParameter[]) beans
159: .toArray(TCP_ARRAY);
160:
161: } catch (ParserConfigurationException e) {
162: throw new TemplateConfigException(
163: "System error reading 'template.xml' : " + e);
164:
165: } catch (SAXException e) {
166: throw new TemplateConfigException(
167: "The chosen template's 'template.xml' is invalid.");
168:
169: } catch (IOException e) {
170: throw new TemplateConfigException(
171: "JAG can't locate the template's 'template.xml'.");
172: }
173: }
174:
175: }
|