1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.io.IOException ; 23 import java.io.StringWriter ; 24 import java.io.Writer ; 25 26 import org.apache.velocity.context.InternalContextAdapter; 27 import org.apache.velocity.exception.MethodInvocationException; 28 import org.apache.velocity.exception.ParseErrorException; 29 import org.apache.velocity.exception.ResourceNotFoundException; 30 import org.apache.velocity.runtime.directive.Directive; 31 import org.apache.velocity.runtime.parser.node.ASTDirective; 32 import org.apache.velocity.runtime.parser.node.Node; 33 34 49 public class ChainDirective extends Directive { 50 51 public String getName() { 52 return "chain"; 53 } 54 55 public int getType() { 56 return BLOCK; 57 } 58 59 public boolean render(InternalContextAdapter context, Writer writer, Node node) 60 throws 61 IOException , 62 ResourceNotFoundException, 63 ParseErrorException, 64 MethodInvocationException { 65 66 int size = node.jjtGetNumChildren(); 67 if (size == 0) { 68 return true; 69 } 70 71 Node block = node.jjtGetChild(node.jjtGetNumChildren() - 1); 73 String join = (size > 1) ? (String ) node.jjtGetChild(0).value(context) : ""; 74 String prefix = (size > 2) ? (String ) node.jjtGetChild(1).value(context) : ""; 75 76 StringWriter childWriter = new StringWriter (30); 78 79 int len = block.jjtGetNumChildren(); 80 int includedChunks = 0; 81 for (int i = 0; i < len; i++) { 82 Node child = block.jjtGetChild(i); 83 84 if (child instanceof ASTDirective 86 && "chunk".equals(((ASTDirective) child).getDirectiveName())) { 87 88 if (child.jjtGetNumChildren() < 2 89 || child.jjtGetChild(0).evaluate(context)) { 90 91 if (includedChunks > 0) { 92 childWriter.write(join); 93 } 94 95 includedChunks++; 96 } 97 } 98 99 child.render(context, childWriter); 100 } 101 102 if (includedChunks > 0) { 103 childWriter.flush(); 104 writer.write(prefix); 105 writer.write(childWriter.toString()); 106 } 107 108 return true; 109 } 110 } 111 | Popular Tags |