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