From 09b6775689767f96b241ee437d05886d9710e800 Mon Sep 17 00:00:00 2001 From: hykes Date: Tue, 31 Jul 2018 15:00:02 +0800 Subject: [PATCH] add GetPackage directive --- .../hykes/codegen/directive/GetPackage.java | 41 +++++++++++++++++++ .../hykes/codegen/utils/VelocityUtil.java | 1 + 2 files changed, 42 insertions(+) create mode 100644 src/com/github/hykes/codegen/directive/GetPackage.java diff --git a/src/com/github/hykes/codegen/directive/GetPackage.java b/src/com/github/hykes/codegen/directive/GetPackage.java new file mode 100644 index 0000000..8481110 --- /dev/null +++ b/src/com/github/hykes/codegen/directive/GetPackage.java @@ -0,0 +1,41 @@ +package com.github.hykes.codegen.directive; + +import org.apache.velocity.context.InternalContextAdapter; +import org.apache.velocity.exception.MethodInvocationException; +import org.apache.velocity.exception.ParseErrorException; +import org.apache.velocity.exception.ResourceNotFoundException; +import org.apache.velocity.runtime.directive.Directive; +import org.apache.velocity.runtime.parser.node.Node; + +import java.io.IOException; +import java.io.Writer; + +/** + * 拼接元素 + * ${Append '#' 'ABC' '%'} => #ABC% + * @author hehaiyangwork@gmail.com + * @date 2017/12/19 + */ +public class GetPackage extends Directive { + @Override + public String getName() { + return "GetPackage"; + } + + @Override + public int getType() { + return LINE; + } + + @Override + public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { + String clazz = (String) node.jjtGetChild(0).value(context); + if (context.containsKey(clazz)) { + String packagePath = context.get(clazz).toString(); + packagePath = new StringBuilder("").append(packagePath).toString(); + writer.write(packagePath); + return true; + } + return false; + } +} diff --git a/src/com/github/hykes/codegen/utils/VelocityUtil.java b/src/com/github/hykes/codegen/utils/VelocityUtil.java index 2364c7e..51aee00 100644 --- a/src/com/github/hykes/codegen/utils/VelocityUtil.java +++ b/src/com/github/hykes/codegen/utils/VelocityUtil.java @@ -29,6 +29,7 @@ public class VelocityUtil { VELOCITY_ENGINE.loadDirective("com.github.hykes.codegen.directive.Append"); VELOCITY_ENGINE.loadDirective("com.github.hykes.codegen.directive.Split"); VELOCITY_ENGINE.loadDirective("com.github.hykes.codegen.directive.ImportPackage"); + VELOCITY_ENGINE.loadDirective("com.github.hykes.codegen.directive.GetPackage"); Thread.currentThread().setContextClassLoader(classLoader); }