1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2005 Tim Pizey
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * Tim Pizey <timp At paneris.org>
42 * http://paneris.org/~timp
43 */
44
45 package org.melati.app;
46
47 import java.io.IOException;
48 import java.util.Hashtable;
49
50
51 import org.melati.Melati;
52 import org.melati.poem.util.ArrayUtils;
53 import org.melati.template.TemplateEngine;
54 import org.melati.template.TemplateContext;
55 import org.melati.template.TemplateEngineException;
56 import org.melati.util.MelatiConfigurationException;
57 import org.melati.util.MelatiException;
58
59 /**
60 * Base class to use Melati as an application with a Template Engine.
61 *
62 * To create your own application simply extend this class,
63 * overriding the {@link #doTemplateRequest} method.
64 */
65 public abstract class AbstractTemplateApp extends AbstractPoemApp implements App {
66
67 protected TemplateEngine templateEngine;
68
69 /**
70 * {@inheritDoc}
71 * @see org.melati.app.AbstractPoemApp#init(java.lang.String[])
72 */
73 public Melati init(String[] args) throws MelatiException {
74 Melati melati = super.init(args);
75 templateEngine = melatiConfig.getTemplateEngine();
76 melati.setTemplateEngine(templateEngine);
77 TemplateContext templateContext;
78 templateEngine.init(melatiConfig);
79 try {
80 templateContext = templateEngine.getTemplateContext();
81 } catch (TemplateEngineException e) {
82 try {
83 super.term(melati);
84 } catch (IOException ee) {
85 ee = null;
86 }
87 throw new MelatiConfigurationException(
88 "Have you configured a template engine? " +
89 "org.melati.MelatiConfig.templateEngine currently set to " +
90 templateEngine.getClass().getName());
91 }
92 melati.setTemplateContext(templateContext);
93 String[] argsWithoutOutput = melati.getArguments();
94 Hashtable<String, String> form = new Hashtable<String, String>();
95 if (argsWithoutOutput.length > 4) {
96 loadForm(form,(String[])ArrayUtils
97 .section(argsWithoutOutput, 4, argsWithoutOutput.length));
98 }
99 templateContext = melati.getTemplateContext();
100 templateContext.put("Form", form);
101
102 return melati;
103 }
104
105 private void loadForm(Hashtable<String, String> form, String[] tuples) {
106 if (tuples.length != ((tuples.length/2)*2))
107 throw new InvalidArgumentsException (tuples,
108 new RuntimeException("Number of paired arguments is not even:" + tuples.length));
109 boolean isValue = false;
110 String name = "";
111 for (int i = 0; i < tuples.length; i++) {
112 if (isValue) {
113 form.put(name, tuples[i]);
114 isValue = false;
115 } else {
116 name = tuples[i];
117 isValue = true;
118 }
119 }
120
121 }
122
123 /**
124 * Fulfill {@link AbstractPoemApp}'s promises.
125 *
126 * @param melati the {@link Melati}
127 * @throws Exception if anything goes wrong
128 * @see org.melati.app.AbstractPoemApp#doPoemRequest(org.melati.Melati)
129 */
130 protected void doPoemRequest(Melati melati) throws Exception {
131 /*
132 templateEngine = melatiConfig.getTemplateEngine();
133 templateEngine.init(melatiConfig);
134 melati.setTemplateEngine(templateEngine);
135 TemplateContext templateContext = templateEngine.getTemplateContext(melati);
136 melati.setTemplateContext(templateContext);
137 */
138 TemplateContext templateContext = melati.getTemplateContext();
139 templateContext.put("melati", melati);
140 templateContext.put("ml", melati.getMarkupLanguage());
141
142 String templateName = doTemplateRequest(melati,templateContext);
143
144 if (templateName == null)
145 templateName = this.getClass().getName().replace('.', '/');
146 templateName = addExtension(templateName);
147 templateEngine.expandTemplate(melati.getWriter(),
148 templateName,
149 templateContext);
150 }
151
152 /**
153 * The template extension is added in an overridable method
154 * to allow the application developer to specify their own template
155 * extensions.
156 */
157 protected String addExtension(String templateName) {
158 if (!templateName.endsWith(templateEngine.templateExtension()))
159 return templateName + templateEngine.templateExtension();
160 else
161 return templateName;
162 }
163
164 /**
165 * Override this method to build up your own output.
166 *
167 * @param melati the current {@link Melati}
168 * @param templateContext the current {@link TemplateContext}
169 * @return a Template name, possibly excluding extension.
170 */
171 protected abstract String doTemplateRequest(Melati melati,
172 TemplateContext templateContext)
173 throws Exception;
174 }