1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
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 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 12 | public abstract class AbstractTemplateApp extends AbstractPoemApp implements App { |
66 | |
|
67 | |
protected TemplateEngine templateEngine; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public Melati init(String[] args) throws MelatiException { |
76 | 12 | Melati melati = super.init(args); |
77 | 11 | templateEngine = melatiConfig.getTemplateEngine(); |
78 | 11 | melati.setTemplateEngine(templateEngine); |
79 | 11 | TemplateContext templateContext = null; |
80 | 11 | templateEngine.init(melatiConfig); |
81 | |
try { |
82 | 11 | templateContext = templateEngine.getTemplateContext(); |
83 | 1 | } catch (TemplateEngineException e) { |
84 | |
try { |
85 | 1 | super.term(melati); |
86 | 0 | } catch (IOException ee) { |
87 | 0 | ee = null; |
88 | 1 | } |
89 | 1 | throw new MelatiConfigurationException( |
90 | |
"Have you configured a template engine? " + |
91 | |
"org.melati.MelatiConfig.templateEngine currently set to " + |
92 | 1 | templateEngine.getClass().getName()); |
93 | 10 | } |
94 | 10 | melati.setTemplateContext(templateContext); |
95 | 10 | String[] argsWithoutOutput = melati.getArguments(); |
96 | 10 | Hashtable<String, String> form = new Hashtable<String, String>(); |
97 | 10 | if (argsWithoutOutput.length > 4) { |
98 | 8 | loadForm(form,(String[])ArrayUtils |
99 | 4 | .section(argsWithoutOutput, 4, argsWithoutOutput.length)); |
100 | |
} |
101 | 9 | templateContext = melati.getTemplateContext(); |
102 | 9 | templateContext.put("Form", form); |
103 | |
|
104 | 9 | return melati; |
105 | |
} |
106 | |
|
107 | |
private void loadForm(Hashtable<String, String> form, String[] tuples) { |
108 | 4 | if (tuples.length != ((tuples.length/2)*2)) |
109 | 1 | throw new InvalidArgumentsException (tuples, |
110 | |
new RuntimeException("Number of paired arguments is not even:" + tuples.length)); |
111 | 3 | boolean isValue = false; |
112 | 3 | String name = ""; |
113 | 11 | for (int i = 0; i < tuples.length; i++) { |
114 | 8 | if (isValue) { |
115 | 4 | form.put(name, tuples[i]); |
116 | 4 | isValue = false; |
117 | |
} else { |
118 | 4 | name = tuples[i]; |
119 | 4 | isValue = true; |
120 | |
} |
121 | |
} |
122 | |
|
123 | 3 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
protected void doPoemRequest(Melati melati) throws Exception { |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 8 | TemplateContext templateContext = melati.getTemplateContext(); |
141 | 8 | templateContext.put("melati", melati); |
142 | 8 | templateContext.put("ml", melati.getMarkupLanguage()); |
143 | |
|
144 | 8 | String templateName = doTemplateRequest(melati,templateContext); |
145 | |
|
146 | 8 | if (templateName == null) |
147 | 2 | templateName = this.getClass().getName().replace('.', '/'); |
148 | 8 | templateName = addExtension(templateName); |
149 | 8 | templateEngine.expandTemplate(melati.getWriter(), |
150 | |
templateName, |
151 | |
templateContext); |
152 | 7 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
protected String addExtension(String templateName) { |
160 | 8 | if (!templateName.endsWith(templateEngine.templateExtension())) |
161 | 8 | return templateName + templateEngine.templateExtension(); |
162 | |
else |
163 | 0 | return templateName; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected abstract String doTemplateRequest(Melati melati, |
174 | |
TemplateContext templateContext) |
175 | |
throws Exception; |
176 | |
} |