1 23 24 package org.objectweb.jorm.generator.lib; 25 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.util.Collection ; 29 30 import org.apache.velocity.VelocityContext; 31 import org.apache.velocity.context.Context; 32 import org.objectweb.jorm.api.PException; 33 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 34 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 35 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 36 import org.objectweb.jorm.metainfo.api.Class; 37 import org.objectweb.jorm.metainfo.api.NameDef; 38 import org.objectweb.jorm.metainfo.api.Package; 39 import org.objectweb.jorm.util.io.api.TargetHolder; 40 import org.objectweb.util.monolog.api.BasicLevel; 41 42 64 public class BindingGenerator extends CommonGenerator { 65 66 74 public void generate(Class clazz, 75 TargetHolder holder, 76 JormCompilerParameter cp, 77 JormCompilerConfigurator jcc) throws PException { 78 String pack = ((Package ) clazz.getParent()).getName(); 79 String fileName = clazz.getName() + "Binding"; 81 if (pack.length() > 0) { 82 fileName = (pack + '.').replace('.', File.separatorChar) + fileName; 83 } 84 logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class"); 85 Collection nds = clazz.getNameDefs(); 86 Class classWithNd = clazz; 87 while(nds.size() < 1 && classWithNd.getInheritedClassNumber() > 0) { 88 classWithNd = (Class ) classWithNd.getSuperClasses().iterator().next(); 89 nds = classWithNd.getNameDefs(); 90 } 91 if (nds.size() < 1) { 92 logger.log(BasicLevel.WARN, "Binding generation: No NameDef defined for the class " + clazz.getFQName()); 93 return; 94 } 95 NameDef nd = (NameDef) nds.iterator().next(); 96 Context ctx = new VelocityContext(); 97 ctx.put("class", clazz); 98 ctx.put("classND", nd); 99 if (pack.length() > 0) { 100 ctx.put("package", pack); 101 } 102 ctx.put("cparam", cp); 103 ctx.put("tools", this); 104 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 105 try { 106 FileWriter fw = holder.getFileWriter(fileName + ".java"); 107 velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Binding.vm").merge(ctx, fw); 108 fw.flush(); 109 fw.close(); 110 } catch (Exception e) { 111 throw new PExceptionCompiler(e, 112 "Problem while writing Binding java file: " 113 + e.getMessage()); 114 } 115 } 116 } 117 | Popular Tags |