##### Coverage Report - org.melati.app.AbstractTemplateApp

Classes in this File | Line Coverage | Branch Coverage | Complexity
AbstractTemplateApp |
93% | 40/43
92% | 13/14
| 3.2;3.2

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.app.AbstractTemplateApp.html,v $
3 |  |
      * $Revision: 1.1 $
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.util.MelatiConfigurationException;
56 |  |
     import org.melati.util.MelatiException;
57 |  |
58 |  |
     /**
59 |  |
      * Base class to use Melati as an application with a Template Engine.
60 |  |
      *
61 |  |
      * To create your own application simply extend this class,
62 |  |
      * overriding the {@link #doTemplateRequest} method.
63 |  |
      */
64 | 24 |
     public abstract class AbstractTemplateApp extends AbstractPoemApp implements App {
65 |  |
66 |  |
       protected TemplateEngine templateEngine;
67 |  |
68 |  |
69 |  |
70 |  |
       /**
71 |  |
        * {@inheritDoc}
72 |  |
        * @see org.melati.app.AbstractPoemApp#init(java.lang.String[])
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 |  |
        * Fulfill {@link AbstractPoemApp}'s promises.
124 |  |
        *
125 |  |
        * @param melati the {@link Melati}
126 |  |
        * @throws Exception if anything goes wrong
127 |  |
        * @see org.melati.app.AbstractPoemApp#doPoemRequest(org.melati.Melati)
128 |  |
        */
129 |  |
       protected void doPoemRequest(Melati melati) throws Exception {
130 |  |
         /*
131 |  |
         templateEngine = melatiConfig.getTemplateEngine();
132 |  |
         templateEngine.init(melatiConfig);
133 |  |
         melati.setTemplateEngine(templateEngine);
134 |  |
         TemplateContext templateContext = templateEngine.getTemplateContext(melati);
135 |  |
         melati.setTemplateContext(templateContext);
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 |  |
        * The template extension is added in an overridable method
153 |  |
        * to allow the application developer to specify their own template
154 |  |
        * extensions.
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 |  |
        * Override this method to build up your own output.
165 |  |
        *
166 |  |
        * @param melati the current {@link Melati}
167 |  |
        * @param templateContext the current {@link TemplateContext}
168 |  |
        * @return a Template name, possibly excluding extension.
169 |  |
        */
170 |  |
       protected abstract String doTemplateRequest(Melati melati,
171 |  |
                                                   TemplateContext templateContext)
172 |  |
           throws Exception;
173 |  |
     }