1 package org.objectweb.celtix.tools.generators; 2 3 import java.io.IOException ; 4 import java.io.Writer ; 5 import java.util.Calendar ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 import java.util.logging.Logger ; 10 11 import org.apache.velocity.Template; 12 import org.apache.velocity.VelocityContext; 13 import org.apache.velocity.app.Velocity; 14 import org.objectweb.celtix.common.i18n.Message; 15 import org.objectweb.celtix.common.logging.LogUtils; 16 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 17 import org.objectweb.celtix.tools.common.ToolConstants; 18 import org.objectweb.celtix.tools.common.ToolException; 19 import org.objectweb.celtix.tools.common.model.JavaModel; 20 import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector; 21 import org.objectweb.celtix.tools.utils.FileWriterUtil; 22 import org.objectweb.celtix.version.Version; 23 24 public abstract class AbstractGenerator { 25 26 public static final String TEMPLATE_BASE = "org/objectweb/celtix/tools/generators/wsdl2/templates"; 27 private static final Logger LOG = LogUtils.getL7dLogger(AbstractGenerator.class); 28 protected ProcessorEnvironment env; 29 protected JavaModel javaModel; 30 protected Map <String , Object > attributes = new HashMap <String , Object >(); 31 protected String name; 32 protected ClassCollector collector; 33 public AbstractGenerator() { 34 35 } 36 public AbstractGenerator(JavaModel jmodel, ProcessorEnvironment penv) { 37 javaModel = jmodel; 38 this.env = penv; 39 collector = (ClassCollector)env.get(ToolConstants.GENERATED_CLASS_COLLECTOR); 40 } 41 42 public abstract boolean passthrough(); 43 44 public abstract void generate() throws ToolException; 45 46 protected void doWrite(String templateName, Writer outputs) throws ToolException { 47 Template tmpl = null; 48 try { 49 tmpl = Velocity.getTemplate(templateName); 50 } catch (Exception e) { 51 Message msg = new Message("TEMPLATE_MISSING", LOG, templateName); 52 throw new ToolException(msg, e); 53 } 54 55 VelocityContext ctx = new VelocityContext(); 56 57 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 58 String key = (String )iter.next(); 59 ctx.put(key, attributes.get(key)); 60 } 61 62 VelocityWriter writer = new VelocityWriter(outputs); 63 try { 64 tmpl.merge(ctx, writer); 65 writer.close(); 66 } catch (Exception e) { 67 Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS", LOG); 68 throw new ToolException(msg, e); 69 } 70 } 71 72 protected boolean isCollision(String packageName, String filename) throws ToolException { 73 return isCollision(packageName, filename, ".java"); 74 } 75 76 protected boolean isCollision(String packageName, String filename, String ext) throws ToolException { 77 FileWriterUtil fw = new FileWriterUtil((String )env.get(ToolConstants.CFG_OUTPUTDIR)); 78 return fw.isCollision(packageName, filename + ext); 79 } 80 81 protected Writer parseOutputName(String packageName, String filename, String ext) throws ToolException { 82 FileWriterUtil fw = null; 83 Writer writer = null; 84 85 fw = new FileWriterUtil((String )env.get(ToolConstants.CFG_OUTPUTDIR)); 86 try { 87 writer = fw.getWriter(packageName, filename + ext); 88 } catch (IOException ioe) { 89 Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, packageName + "." + filename + ext); 90 throw new ToolException(msg, ioe); 91 } 92 93 return writer; 94 } 95 96 protected Writer parseOutputName(String packageName, String filename) throws ToolException { 97 if (ToolConstants.CLT_GENERATOR.equals(name)) { 99 collector.addClientClassName(packageName , filename , packageName + "." + filename); 100 } 101 102 if (ToolConstants.FAULT_GENERATOR.equals(name)) { 103 collector.addExceptionClassName(packageName , filename , packageName + "." + filename); 104 } 105 if (ToolConstants.SERVICE_GENERATOR.equals(name)) { 106 collector.addServiceClassName(packageName , filename , packageName + "." + filename); 107 } 108 if (ToolConstants.SVR_GENERATOR.equals(name)) { 109 collector.addServiceClassName(packageName , filename , packageName + "." + filename); 110 111 } 112 113 114 return parseOutputName(packageName, filename, ".java"); 115 } 116 117 protected void setAttributes(String n, Object value) { 118 attributes.put(n, value); 119 } 120 121 protected void setCommonAttributes() { 122 attributes.put("currentdate", Calendar.getInstance().getTime()); 123 attributes.put("version", Version.getCurrentVersion()); 124 } 125 126 protected void clearAttributes() { 127 attributes.clear(); 128 } 129 130 public void setEnvironment(ProcessorEnvironment penv) { 131 this.env = penv; 132 133 } 134 135 public ProcessorEnvironment getEnvironment() { 136 return this.env; 137 } 138 139 public String getName() { 140 return this.name; 141 } 142 } 143 | Popular Tags |