1 23 24 package org.objectweb.jorm.generator.lib; 25 26 import org.apache.velocity.VelocityContext; 27 import org.apache.velocity.context.Context; 28 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 29 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 30 import org.objectweb.jorm.metainfo.api.Class; 31 import org.objectweb.jorm.metainfo.api.Package; 32 import org.objectweb.jorm.util.io.api.TargetHolder; 33 import org.objectweb.jorm.api.PException; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 import java.io.File ; 37 import java.io.FileWriter ; 38 39 42 public class SerializableGenerator extends CommonGenerator { 43 50 public void generate(Class co, 51 TargetHolder holder, 52 JormCompilerParameter cp) throws PException { 53 String packName = ((Package ) co.getParent()).getName(); 55 String packroot = packName; 56 String fileName = co.getName() + "Serializable"; 57 if (packName != null && packName.length() > 0) { 58 fileName = packName + "." + fileName; 59 fileName = fileName.replace('.', File.separatorChar); 60 } 61 logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class"); 62 try { 63 FileWriter fw = holder.getFileWriter(fileName + ".java"); 65 Context ctx = new VelocityContext(); 66 ctx.put("class", co); 67 CommonHelper ch = new CommonHelper(); 68 ch.setLogger(logger); 69 ctx.put("tools", ch); 70 if (packName.length() > 0) { 71 ctx.put("packageroot",packroot); 72 } 73 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 74 if (template == null) { 75 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Serializable.vm"); 76 } 77 template.merge(ctx, fw); 78 fw.flush(); 79 fw.close(); 80 } catch (Exception e) { 81 throw new PExceptionCompiler(e, "Problem while generating Serializable."); 82 } 83 } 84 85 } 86 | Popular Tags |