<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: boilerplate code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 05 Oct 2008 07:26:20 GMT</pubDate>
    <description>DZone Snippets: boilerplate code</description>
    <item>
      <title>Velocity 101</title>
      <link>http://snippets.dzone.com/posts/show/1589</link>
      <description>// The most basic use of the Velocity library. You pass in a Velocity context&lt;br /&gt;// containing key-value pairs and the name of a Velocity template file. It&lt;br /&gt;// returns a string containing the boilerplate text generated from combining&lt;br /&gt;// the two.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Copyright (c) 2002, John Munsch&lt;br /&gt;// All rights reserved.&lt;br /&gt;//&lt;br /&gt;// Redistribution and use in source and binary forms, with or without &lt;br /&gt;// modification, are permitted provided that the following conditions are met:&lt;br /&gt;//&lt;br /&gt;//     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;//       this list of conditions and the following disclaimer.&lt;br /&gt;// &lt;br /&gt;//     * Redistributions in binary form must reproduce the above copyright &lt;br /&gt;//       notice, this list of conditions and the following disclaimer in the &lt;br /&gt;//       documentation and/or other materials provided with the distribution.&lt;br /&gt;// &lt;br /&gt;//     * Neither the name of the John Munsch nor the names of its contributors &lt;br /&gt;//       may be used to endorse or promote products derived from this software &lt;br /&gt;//       without specific prior written permission.&lt;br /&gt;//&lt;br /&gt;// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" &lt;br /&gt;// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE &lt;br /&gt;// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE &lt;br /&gt;// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE &lt;br /&gt;// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR &lt;br /&gt;// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF &lt;br /&gt;// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS &lt;br /&gt;// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN &lt;br /&gt;// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) &lt;br /&gt;// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE &lt;br /&gt;// POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;// &lt;br /&gt;// To learn more about open source licenses, please visit: &lt;br /&gt;// http://opensource.org/index.php&lt;br /&gt;&lt;br /&gt;package com.johnmunsch.util;&lt;br /&gt;&lt;br /&gt;import java.io.StringWriter;&lt;br /&gt;&lt;br /&gt;import org.apache.log4j.*;&lt;br /&gt;import org.apache.velocity.*;&lt;br /&gt;import org.apache.velocity.app.Velocity;&lt;br /&gt;import org.apache.velocity.exception.*;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Boilerplate puts together context information and a template and produces an&lt;br /&gt; * output string that has all the replacements done in it.&lt;br /&gt; */&lt;br /&gt;public class Boilerplate {&lt;br /&gt;    private static Logger log = Logger.getLogger(Boilerplate.class.getName());&lt;br /&gt;    &lt;br /&gt;    public static String apply(VelocityContext context,&lt;br /&gt;            String templateFilename) throws Exception {&lt;br /&gt;        Template template = null;&lt;br /&gt;        StringWriter sw = new StringWriter();&lt;br /&gt;&lt;br /&gt;        Velocity.init();&lt;br /&gt;&lt;br /&gt;        try {&lt;br /&gt;            template = Velocity.getTemplate(templateFilename);&lt;br /&gt;&lt;br /&gt;            template.merge(context, sw);&lt;br /&gt;        } catch (ResourceNotFoundException rnfe) {&lt;br /&gt;            log.error("Could not find the named template file.", rnfe);&lt;br /&gt;            throw rnfe;&lt;br /&gt;        } catch (ParseErrorException pee) {&lt;br /&gt;            log.error("Error in the template file.", pee);&lt;br /&gt;            throw pee;&lt;br /&gt;        } catch (MethodInvocationException mie) {&lt;br /&gt;            log.error("Error in function called by the template file.", mie);&lt;br /&gt;            throw mie;&lt;br /&gt;        }&lt;br /&gt;       &lt;br /&gt;        return sw.toString();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 03:39:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1589</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
  </channel>
</rss>
