net.sf.nakeduml.javageneration.AbstractTextProducingVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metamodel Show documentation
Show all versions of metamodel Show documentation
A uml code generator and execution engine
The newest version!
package net.sf.nakeduml.javageneration;
import java.io.CharArrayWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Stack;
import net.sf.nakeduml.feature.NakedUmlConfig;
import net.sf.nakeduml.javametamodel.OJPackage;
import net.sf.nakeduml.metamodel.core.INakedElement;
import net.sf.nakeduml.metamodel.core.INakedElementOwner;
import net.sf.nakeduml.metamodel.workspace.INakedModelWorkspace;
import net.sf.nakeduml.textmetamodel.ResourceLoader;
import net.sf.nakeduml.textmetamodel.TextOutputRoot;
import net.sf.nakeduml.textmetamodel.TextWorkspace;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class AbstractTextProducingVisitor extends AbstractJavaProducingVisitor{
protected INakedModelWorkspace workspace;
protected NakedUmlConfig config;
protected TextWorkspace textWorkspace;
protected VelocityEngine ve;
@Override
public void initialize(INakedModelWorkspace workspace,OJPackage javaModel,NakedUmlConfig config,TextWorkspace textWorkspace){
this.initialize(workspace, config, textWorkspace);
super.javaModel = javaModel;
}
public void initialize(INakedModelWorkspace workspace,NakedUmlConfig config,TextWorkspace textWorkspace){
this.workspace = workspace;
this.config = config;
this.textWorkspace = textWorkspace;
this.ve = new VelocityEngine();
Properties velocityProperties = new Properties();
velocityProperties.put("resource.loader", "class");
velocityProperties.put("class.resource.loader.class", ResourceLoader.class.getName());
try{
this.ve.init(velocityProperties);
}catch(Exception e){
if(e instanceof RuntimeException){
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
}
protected void processTemplate(INakedElement element,String templateResource,String destinationExpression,String outputRootName){
Map emptyMap = Collections.emptyMap();
processTemplate(element, templateResource, destinationExpression, outputRootName, emptyMap);
}
protected void processTemplate(INakedElement element,String templateResource,String destinationExpression,String outputRootName,
Map vars){
VelocityContext context = new VelocityContext();
for(Map.Entry var:vars.entrySet()){
context.put(var.getKey(), var.getValue());
}
context.put(element.getMetaClass(), element);
context.put("config", this.config);
context.put("workspace", this.workspace);
context.put("stack1", new Stack