1 28 29 package org.objectweb.openccm.uml.transformation.rules.codeGenerator; 30 31 import java.io.Writer ; 32 33 import org.apache.velocity.app.Velocity; 34 35 import ispuml.mdaTransformation.RuleContext; 36 import ispuml.mdaTransformation.TransformationException; 37 import ispuml.mdaTransformation.rules.codeGenerator.GenerateAction; 38 import ispuml.mdaTransformation.rules.codeGenerator.VelocityContext; 39 import ispuml.mdaTransformation.rules.codeGenerator.VelocityTemplateDirectives; 40 41 45 public class GenerateMacro extends GenerateAction { 46 47 50 protected String macro; 51 52 56 public String getMacro() { 57 return macro; 58 } 59 60 64 public void setMacro(String macro) { 65 this.macro = macro; 66 } 67 68 75 public Object execute(Object bean, RuleContext request) throws TransformationException { 76 boolean shouldCloseWriter = false; 79 80 Writer writer = (Writer ) request.getAttribute(WRITER_ATTRIBUTE_NAME); 82 if (alwaysCreateWriter || writer == null) { 84 writer = createWriter(bean, request); 85 request.putAttribute(WRITER_ATTRIBUTE_NAME, writer); 86 shouldCloseWriter = (filename != null); 88 } 90 try { 92 VelocityContext context = new VelocityTemplateDirectives(bean, request); 93 if (template != null) { 94 if (macro != null) { 95 org.apache.velocity.app.VelocityEngine engine_ = new org.apache.velocity.app.VelocityEngine(); 97 engine_.setProperty(org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, 98 "org.apache.velocity.runtime.log.NullLogSystem" ); 99 engine_.setProperty(org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER, "classpath"); 100 engine_.setProperty("classpath." + org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER + ".class", 101 org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader.class.getName()); 102 engine_.setProperty(org.apache.velocity.app.VelocityEngine.VM_LIBRARY, template); 103 engine_.init(); 104 boolean res = engine_.invokeVelocimacro(macro, template, new String [0], context, writer); 105 } else { 106 org.apache.velocity.runtime.RuntimeSingleton.setProperty(org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, 107 "org.apache.velocity.runtime.log.NullLogSystem" ); 108 Velocity.init(); 109 Velocity.mergeTemplate(template, "ISO-8859-1", context, writer); 110 } 111 } else if (templateString != null) { 112 Velocity.evaluate(context, writer, "template", templateString); 113 } 115 if (shouldCloseWriter) 117 writer.close(); 118 } catch (java.io.IOException ex) { 119 throw new TransformationException("Can't close writer.", ex); 120 } catch (Exception ex) { 121 throw new TransformationException("Template exception.", ex); 122 } 123 124 return bean; 125 } 126 127 } 128 | Popular Tags |