/************************************************************************** /* This class creates a bean-class from a velocity template. /* /* Copyright (c) 2004 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.util.*; import java.io.*; import org.apache.velocity.app.*; import org.apache.velocity.*; /** This class creates a bean-class from a velocity template. @version $Revision: 1.7 $ @author $Author: bablokb $ */ public class BeanGen { /** The bean-template. */ private static final String BEAN_TEMPLATE = "bean.vm", TEMPLATE_ENC = "ISO-8859-15"; private VelocityContext iContext; ////////////////////////////////////////////////////////////////////////////// /** Constructor. Intitializes Velocity. */ private BeanGen() throws Exception { Properties prop = new Properties(); prop.put("velocimacro.library","bean-macros.vm"); prop.put("runtime.log","/tmp/velocity.log"); Velocity.init(prop); iContext = new VelocityContext(); } ////////////////////////////////////////////////////////////////////////////// /** Fill the context with all variables. */ private void fillContext() { iContext.put("year", new Integer(Calendar.getInstance().get(Calendar.YEAR))); // add author description Hashtable ad = new Hashtable(); ad.put("name","B.Bablok"); ad.put("email","mail@bablokb.de"); iContext.put("author",ad); // add class description Hashtable cd = new Hashtable(); cd.put("name","Person"); cd.put("package","de.bablokb.beans"); cd.put("qualifier","public final"); cd.put("extends","Object"); cd.put("implements","Serializable"); Hashtable imports = new Hashtable(); imports.put("1","java.io.*"); imports.put("2","java.util.*"); cd.put("imports",imports); Hashtable properties = new Hashtable(); Hashtable prop1 = new Hashtable(); prop1.put("type","String"); prop1.put("name","Name"); properties.put("a",prop1); Hashtable prop2 = new Hashtable(); prop2.put("type","ArrayList"); prop2.put("name","EmailAddresses"); properties.put("b",prop2); cd.put("properties",properties); iContext.put("clazz",cd); } ////////////////////////////////////////////////////////////////////////////// /** Merge template and context. */ private void merge() throws Exception { PrintWriter out = new PrintWriter(System.out); Velocity.mergeTemplate(BEAN_TEMPLATE,TEMPLATE_ENC,iContext,out); out.flush(); } ////////////////////////////////////////////////////////////////////////////// /** The main-method takes a bean-configuration-file as input. */ public static void main(String[] args) { try { BeanGen gen = new BeanGen(); gen.fillContext(); gen.merge(); } catch (Exception e) { e.printStackTrace(); } } }