1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.finalist.jaggenerator.template;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.NodeList;
22 import org.xml.sax.SAXException;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.ArrayList;
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 XMLTAG_JAG_TEMPLATE = "jag-template";
42 private static final String TEMPLATE_XML = "template.xml";
43 private static final String ID_ATTRIBUTE = "id";
44 private static final String NAME_ATTRIBUTE = "name";
45 private static final String[] STRING_ARRAY = new String[0];
46 private static final TemplateConfigParameter[] TCP_ARRAY = new TemplateConfigParameter[0];
47 private static final String XMLTAG_PARAM = "param";
48 private static final String DESCRIPTION_ATTRIBUTE = "description";
49 private static final String TYPE_ATTRIBUTE = "type";
50 private static final String XMLTAG_VALUE = "value";
51 private static final String ENGINE_ATTRIBUTE = "template-engine";
52 /*** The default template engine class. */
53 public static final String DEFAULT_ENGINE_CLASS = "com.finalist.jag.util.VelocityTemplateEngine";
54
55 private String name;
56 private String description;
57 private String engine;
58 private File templateDir;
59 private Document doc;
60 private TemplateConfigParameter[] configParams;
61
62
63 public Template(File templateDir) throws TemplateConfigException {
64 this.templateDir = templateDir;
65 load(new File(templateDir, TEMPLATE_XML));
66 }
67
68 /***
69 * Gets the name of this template.
70 * @return the name.
71 */
72 public String getName() {
73 return name;
74 }
75
76 /***
77 * Gets the base directory of this template.
78 * @return the base directory.
79 */
80 public File getTemplateDir() {
81 return templateDir;
82 }
83
84 /***
85 * Sets the base directory of this template.
86 */
87 public void setTemplateDir(File templateDir) {
88 this.templateDir = templateDir;
89 }
90
91 public String getDescription() {
92 return description;
93 }
94
95 public String getEngine() {
96 return engine;
97 }
98
99 public String 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 toString() {
112 return name;
113 }
114
115
116 /***
117 * Reads in the template information from the XML file.
118 */
119 private void load(File xmlFile) throws TemplateConfigException {
120 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
121 DocumentBuilder builder = null;
122 try {
123 builder = dbf.newDocumentBuilder();
124 doc = builder.parse(xmlFile);
125 Element root = (Element) doc.getElementsByTagName(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
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.getAttribute(DESCRIPTION_ATTRIBUTE));
143 bean.setType(TemplateConfigParameter.getTypeByName(parameter.getAttribute(TYPE_ATTRIBUTE)));
144 ArrayList temp = new ArrayList();
145 NodeList presetValues = parameter.getElementsByTagName(XMLTAG_VALUE);
146 for (int j = 0; j < presetValues.getLength(); j++) {
147 Element value = (Element) presetValues.item(j);
148 temp.add(value.getFirstChild().getNodeValue());
149 }
150 bean.setPresetValues((String[]) temp.toArray(STRING_ARRAY));
151 beans.add(bean);
152 }
153 configParams = (TemplateConfigParameter[]) beans.toArray(TCP_ARRAY);
154
155 } catch (ParserConfigurationException e) {
156 throw new TemplateConfigException("System error reading 'template.xml' : " + e);
157
158 } catch (SAXException e) {
159 throw new TemplateConfigException("The chosen template's 'template.xml' is invalid.");
160
161 } catch (IOException e) {
162 throw new TemplateConfigException("JAG can't locate the template's 'template.xml'.");
163 }
164 }
165
166 }