1 27 28 package org.objectweb.speedo.generation.generator.lib; 29 30 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 31 import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException; 32 import org.objectweb.speedo.generation.generator.api.VelocityGenerator; 33 import org.objectweb.speedo.api.SpeedoException; 34 import org.objectweb.speedo.metadata.SpeedoClass; 35 import org.objectweb.speedo.metadata.SpeedoIdentity; 36 import org.objectweb.jorm.metainfo.api.Class; 37 import org.objectweb.jorm.metainfo.api.TypedElement; 38 import org.apache.velocity.Template; 39 import org.apache.velocity.app.VelocityEngine; 40 41 import java.util.Collection ; 42 import java.io.File ; 43 44 47 public abstract class AbstractVelocityGenerator 48 extends AbstractGeneratorComponent 49 implements VelocityGenerator { 50 51 public final static String TEMPLATE_DIR 52 = "org.objectweb.speedo.generation.generator"; 53 54 protected VelocityEngine ve = null; 55 56 protected static char fs = File.separatorChar; 57 58 protected Template template = null; 59 60 protected void computeTemplate(String templateFN) throws SpeedoException { 61 try { 62 if (template == null) { 63 try { 64 template = ve.getTemplate(templateFN); 65 } catch (Exception e1) { 66 template = null; 67 } 68 if (template == null) { 69 templateFN = templateFN.replace(fs, '/'); 70 template = ve.getTemplate(templateFN); 71 if (template == null) { 72 throw new SpeedoException("The Speedo template " 73 + templateFN 74 + " is not availlable in the classpath"); 75 } 76 } 77 } 78 } catch (Exception e) { 79 throw new SpeedoGenerationException( 80 "Impossible to fetch the template " + templateFN, e); 81 } 82 } 83 84 91 protected static boolean isClassicalType(String type) { 92 return type.equals("boolean") 93 || type.equals("char") 94 || type.equals("short") 95 || type.equals("int") 96 || type.equals("long") 97 || type.equals("float") 98 || type.equals("double") 99 || type.equals("String") 100 || type.equals("byte"); 101 } 102 103 public boolean isContainerIdField(Class clazz, TypedElement te, SpeedoClass sc) { 104 return sc.identityType == SpeedoIdentity.CONTAINER_ID 105 && clazz 106 .getNameDef(scp.projectName) 107 .getNameRef().getProjection().containsValue(te.getName()); 108 } 109 110 111 112 115 public VelocityEngine getVelocityEngine() { 116 return ve; 117 } 118 119 public void setVelocityEngine(VelocityEngine ve) { 120 this.ve = ve; 121 } 122 123 public Collection getExternalsTemplate() { 124 return null; 125 } 126 127 134 public abstract void generate(SpeedoClass sClass, String fileName) 135 throws SpeedoException; 136 137 140 public abstract boolean init() throws SpeedoException; 141 142 public final void process() throws SpeedoException { 143 throw new SpeedoException("Do not use this method"); 144 } 145 } 146 | Popular Tags |