1 package org.apache.velocity.runtime.directive; 2 3 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 22 import org.apache.velocity.context.InternalContextAdapter; 23 24 import org.apache.velocity.Template; 25 import org.apache.velocity.runtime.RuntimeConstants; 26 import org.apache.velocity.runtime.parser.node.Node; 27 import org.apache.velocity.runtime.parser.node.SimpleNode; 28 29 import org.apache.velocity.exception.MethodInvocationException; 30 import org.apache.velocity.exception.ParseErrorException; 31 import org.apache.velocity.exception.ResourceNotFoundException; 32 33 56 public class Parse extends InputBase 57 { 58 private boolean ready = false; 59 60 63 public String getName() 64 { 65 return "parse"; 66 } 67 68 71 public int getType() 72 { 73 return LINE; 74 } 75 76 81 public boolean render( InternalContextAdapter context, 82 Writer writer, Node node) 83 throws IOException , ResourceNotFoundException, ParseErrorException, 84 MethodInvocationException 85 { 86 89 if ( node.jjtGetChild(0) == null) 90 { 91 rsvc.error( "#parse() error : null argument" ); 92 return false; 93 } 94 95 98 Object value = node.jjtGetChild(0).value( context ); 99 100 if ( value == null) 101 { 102 rsvc.error( "#parse() error : null argument" ); 103 return false; 104 } 105 106 109 String arg = value.toString(); 110 111 115 116 Object [] templateStack = context.getTemplateNameStack(); 117 118 if ( templateStack.length >= 119 rsvc.getInt(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, 20) ) 120 { 121 StringBuffer path = new StringBuffer (); 122 123 for( int i = 0; i < templateStack.length; ++i) 124 { 125 path.append( " > " + templateStack[i] ); 126 } 127 128 rsvc.error( "Max recursion depth reached (" + 129 templateStack.length + ")" + " File stack:" + path ); 130 return false; 131 } 132 133 136 137 Template t = null; 138 139 try 140 { 141 t = rsvc.getTemplate( arg, getInputEncoding(context) ); 142 } 143 catch ( ResourceNotFoundException rnfe ) 144 { 145 148 149 rsvc.error("#parse(): cannot find template '" + arg + 150 "', called from template " + 151 context.getCurrentTemplateName() + " at (" + 152 getLine() + ", " + getColumn() + ")" ); 153 throw rnfe; 154 } 155 catch ( ParseErrorException pee ) 156 { 157 161 162 rsvc.error("#parse(): syntax error in #parse()-ed template '" + 163 arg + "', called from template " + 164 context.getCurrentTemplateName() + " at (" + 165 getLine() + ", " + getColumn() + ")" ); 166 167 throw pee; 168 } 169 catch ( Exception e) 170 { 171 rsvc.error("#parse() : arg = " + arg + ". Exception : " + e); 172 return false; 173 } 174 175 178 try 179 { 180 context.pushCurrentTemplateName(arg); 181 ((SimpleNode) t.getData()).render( context, writer ); 182 } 183 catch ( Exception e ) 184 { 185 188 189 if ( e instanceof MethodInvocationException) 190 { 191 throw (MethodInvocationException) e; 192 } 193 194 rsvc.error( "Exception rendering #parse( " + arg + " ) : " + e ); 195 return false; 196 } 197 finally 198 { 199 context.popCurrentTemplateName(); 200 } 201 202 return true; 203 } 204 } 205 206 | Popular Tags |