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 import java.util.Iterator ; 39 40 59 public class AccessorGenerator extends CommonGenerator { 60 67 public void generate(Class co, 68 TargetHolder holder, 69 JormCompilerParameter cp) throws PException { 70 String packName = ((Package ) co.getParent()).getName(); 72 String fileName = co.getName() + "Accessor"; 73 if (packName != null && packName.length() > 0) { 74 fileName = packName + "." + fileName; 75 fileName = fileName.replace('.', File.separatorChar); 76 } 77 logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " interface"); 78 Context ctx = new VelocityContext(); 79 ctx.put("class", co); 80 CommonHelper ch = new CommonHelper(); 81 ch.setLogger(logger); 82 ctx.put("tools", ch); 83 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 84 85 StringBuffer extension = new StringBuffer ("PAccessor"); 86 Iterator it = co.getSuperClasses().iterator(); 87 if (it.hasNext()) { 88 while(it.hasNext()) { 89 extension.append(", "); 90 extension.append(((Class ) it.next()).getFQName()); 91 extension.append("Accessor"); 92 } 93 } 94 ctx.put("extension", extension); 95 96 try { 97 FileWriter fw = holder.getFileWriter(fileName + ".java"); 99 if (template == null) { 101 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Accessor.vm"); 102 } 103 template.merge(ctx, fw); 104 fw.flush(); 105 fw.close(); 106 } catch (Exception e) { 107 throw new PExceptionCompiler(e, "Problem while generating PAccessor."); 108 } 109 } 110 } 111 | Popular Tags |