Skip to content

Commit

Permalink
add GetPackage directive
Browse files Browse the repository at this point in the history
  • Loading branch information
hykes committed Jul 31, 2018
1 parent 879c4e5 commit 09b6775
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/com/github/hykes/codegen/directive/GetPackage.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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;
}
}
1 change: 1 addition & 0 deletions src/com/github/hykes/codegen/utils/VelocityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 09b6775

Please sign in to comment.