1 /*
2 * Copyright (C) 2017 Tim Pizey
3 *
4 * Part of Melati (http://melati.org), a framework for the rapid
5 * development of clean, maintainable web applications.
6 *
7 * Melati is free software; Permission is granted to copy, distribute
8 * and/or modify this software under the terms either:
9 *
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the License, or (at your option)
12 * any later version,
13 *
14 * or
15 *
16 * b) any version of the Melati Software License, as published
17 * at http://melati.org
18 *
19 * You should have received a copy of the GNU General Public License and
20 * the Melati Software License along with this program;
21 * if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
23 * GNU General Public License and visit http://melati.org to obtain the
24 * Melati Software License.
25 *
26 * Feel free to contact the Developers of Melati (http://melati.org),
27 * if you would like to work out a different arrangement than the options
28 * outlined here. It is our intention to allow Melati to be used by as
29 * wide an audience as possible.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * Contact details for copyright holder:
37 *
38 * Tim Pizey <timp At paneris.org>
39 * http://paneris.org/~timp
40 */
41 package org.melati.app;
42
43 import org.melati.Melati;
44 import org.melati.poem.PoemThread;
45 import org.melati.template.HTMLMarkupLanguage;
46 import org.melati.template.JavaMarkupLanguage;
47 import org.melati.template.TemplateContext;
48 import org.melati.template.webmacro.WebmacroTemplateEngine;
49 import org.melati.util.MelatiException;
50
51 /**
52 * @author timp
53 * @since 2017-01-29
54 *
55 */
56 public class JavinatorApp extends AbstractTemplateApp {
57
58 public JavinatorApp() {
59 super();
60 }
61 public Melati init(String[] args) throws MelatiException {
62 Melati melati = super.init(args);
63 melati.setTemplateEngine(new WebmacroTemplateEngine());
64 melati.setMarkupLanguage(new JavaMarkupLanguage(melati));
65 System.err.println("ml: " + melati.getMarkupLanguage());
66 return melati;
67 }
68 /**
69 * The main method to override.
70 *
71 * @param melati A {@link Melati} with arguments and properties set
72 * @param templateContext A {@link TemplateContext} containing a {@link Melati}
73 * @return the name of a template to expand
74 * @throws Exception if anything goes wrong
75 * @see AbstractTemplateApp#doTemplateRequest
76 * (org.melati.Melati, org.melati.template.ServletTemplateContext)
77 */
78 protected String doTemplateRequest(Melati melati,
79 TemplateContext templateContext) throws Exception {
80 // Turn comments on
81 templateContext.put("comments", "true");
82 templateContext.put("object", melati.getTable().newPersistent());
83 return dsdTemplate(templateContext);
84 }
85
86 /**
87 * @return the DSD template name, the extension is added by the template engine
88 */
89 protected String dsdTemplate(TemplateContext context) {
90 context.put("database", PoemThread.database());
91 // Webmacro security prevents access from within template
92
93 // Note: getPackage() can return null dependant upon
94 // the classloader so we have to chomp the class name
95
96 String c = PoemThread.database().getClass().getName();
97 int dot = c.lastIndexOf('.');
98 String p = c.substring(0, dot);
99
100 context.put("package", p);
101
102 return "org/melati/poem/JdbcPersistent";
103 }
104
105 /**
106 * The main entry point.
107 *
108 * @param args in format <code>db table troid method</code>
109 * where method is a template name
110 */
111 public static void main(String[] args) throws Exception{
112 JavinatorApp me = new JavinatorApp();
113 me.run(args);
114 }
115
116 }