1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.io.IOException ; 59 import java.io.StringWriter ; 60 import java.io.Writer ; 61 62 import org.apache.velocity.context.InternalContextAdapter; 63 import org.apache.velocity.exception.MethodInvocationException; 64 import org.apache.velocity.exception.ParseErrorException; 65 import org.apache.velocity.exception.ResourceNotFoundException; 66 import org.apache.velocity.runtime.directive.Directive; 67 import org.apache.velocity.runtime.parser.node.ASTDirective; 68 import org.apache.velocity.runtime.parser.node.Node; 69 70 85 public class ChainDirective extends Directive { 86 87 public String getName() { 88 return "chain"; 89 } 90 91 public int getType() { 92 return BLOCK; 93 } 94 95 public boolean render(InternalContextAdapter context, Writer writer, Node node) 96 throws 97 IOException , 98 ResourceNotFoundException, 99 ParseErrorException, 100 MethodInvocationException { 101 102 int size = node.jjtGetNumChildren(); 103 if (size == 0) { 104 return true; 105 } 106 107 Node block = node.jjtGetChild(node.jjtGetNumChildren() - 1); 109 String join = (size > 1) ? (String ) node.jjtGetChild(0).value(context) : ""; 110 String prefix = (size > 2) ? (String ) node.jjtGetChild(1).value(context) : ""; 111 112 StringWriter childWriter = new StringWriter (30); 114 115 int len = block.jjtGetNumChildren(); 116 int includedChunks = 0; 117 for (int i = 0; i < len; i++) { 118 Node child = block.jjtGetChild(i); 119 120 if (child instanceof ASTDirective 122 && "chunk".equals(((ASTDirective) child).getDirectiveName())) { 123 124 if (child.jjtGetNumChildren() < 2 125 || child.jjtGetChild(0).evaluate(context)) { 126 127 if (includedChunks > 0) { 128 childWriter.write(join); 129 } 130 131 includedChunks++; 132 } 133 } 134 135 child.render(context, childWriter); 136 } 137 138 if (includedChunks > 0) { 139 childWriter.flush(); 140 writer.write(prefix); 141 writer.write(childWriter.toString()); 142 } 143 144 return true; 145 } 146 } 147 | Popular Tags |