View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 Tim Joyce
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 Joyce <timj@paneris.org>
42   *     http://paneris.org/
43   *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
44   */
45  
46  package org.melati.test;
47  
48  import java.io.ByteArrayOutputStream;
49  
50  import org.melati.template.ServletTemplateContext;
51  import org.melati.Melati;
52  import org.melati.MelatiConfig;
53  import org.melati.servlet.PathInfoException;
54  import org.melati.servlet.TemplateServlet;
55  import org.melati.PoemContext;
56  import org.melati.template.webmacro.MelatiFastWriter;
57  
58  import org.webmacro.WebMacro;
59  import org.webmacro.WM;
60  import org.webmacro.Context;
61  import org.webmacro.Template;
62  
63  /**
64   * Test WebMacro in standalone mode (outside the servlet API) by expanding a
65   * template to a string and then including it within a template.
66   * 
67   * You would not normally do this this way, a much better approach would be to
68   * use templets.
69   * 
70   * @author Tim Joyce $Revision$
71   */
72  public class WebmacroMelatiServletTest extends TemplateServlet {
73  
74    private static final long serialVersionUID = 8747414296960744530L;
75  
76    protected String doTemplateRequest(Melati melati,
77        ServletTemplateContext templateContext) throws Exception {
78  
79      if (melati.getMethod() != null && melati.getMethod().equals("StandAlone")) {
80        // construct a Melati with a StringWriter instead of a servlet
81        // request and response
82        WebMacro wm = new WM();
83        ByteArrayOutputStream sw = new ByteArrayOutputStream();
84        MelatiFastWriter fmw = new MelatiFastWriter(wm.getBroker(), sw, melati
85            .getEncoding());
86        Melati m = new Melati(new MelatiConfig(), fmw);
87        Context context2 = wm.getContext();
88        context2.put("melati", m);
89        context2.put("ml", m.getMarkupLanguage());
90        Template template = wm.getTemplate("org/melati/test/StandAlone.wm");
91        template.write(fmw.getFastWriter().getOutputStream(), context2);
92        fmw.flush();
93        // write to the StringWriter
94        String out = sw.toString();
95        // finally, put what we have into the original templateContext
96        templateContext.put("StandAlone", out);
97      }
98      return "org/melati/test/WebmacroMelatiServletTest";
99    }
100 
101   /**
102    * Set up the melati context so we don't have to specify the logicaldatabase
103    * on the pathinfo.
104    * 
105    * This is a very good idea when writing your appications where you are
106    * typically only accessing a single database
107    */
108   protected PoemContext poemContext(Melati melati) throws PathInfoException {
109     return poemContextWithLDB(melati, "melatitest");
110   }
111 
112 
113 }