Coverage Report - org.melati.app.AbstractTemplateApp
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTemplateApp
93%
44/47
91%
11/12
3.2
 
 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  12
 public abstract class AbstractTemplateApp extends AbstractPoemApp implements App {
 66  
 
 67  
   protected TemplateEngine templateEngine;
 68  
   
 69  
   
 70  
 
 71  
   /** 
 72  
    * {@inheritDoc}
 73  
    * @see org.melati.app.AbstractPoemApp#init(java.lang.String[])
 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  
    * Fulfill {@link AbstractPoemApp}'s promises.
 127  
    * 
 128  
    * @param melati the {@link Melati} 
 129  
    * @throws Exception if anything goes wrong 
 130  
    * @see org.melati.app.AbstractPoemApp#doPoemRequest(org.melati.Melati)
 131  
    */
 132  
   protected void doPoemRequest(Melati melati) throws Exception {
 133  
     /* 
 134  
     templateEngine = melatiConfig.getTemplateEngine();
 135  
     templateEngine.init(melatiConfig);
 136  
     melati.setTemplateEngine(templateEngine);
 137  
     TemplateContext templateContext = templateEngine.getTemplateContext(melati); 
 138  
     melati.setTemplateContext(templateContext);
 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  
    * The template extension is added in an overridable method
 156  
    * to allow the application developer to specify their own template
 157  
    * extensions.
 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  
    * Override this method to build up your own output.
 168  
    *
 169  
    * @param melati the current {@link Melati}
 170  
    * @param templateContext the current {@link TemplateContext}
 171  
    * @return a Template name, possibly excluding extension.
 172  
    */
 173  
   protected abstract String doTemplateRequest(Melati melati, 
 174  
                                               TemplateContext templateContext)
 175  
       throws Exception;
 176  
 }