1 2 package org.apache.velocity.runtime.directive; 3 4 19 20 import java.io.Writer ; 21 import java.io.IOException ; 22 23 import org.apache.velocity.context.InternalContextAdapter; 24 import org.apache.velocity.runtime.RuntimeServices; 25 import org.apache.velocity.runtime.RuntimeConstants; 26 import org.apache.velocity.runtime.parser.ParserTreeConstants; 27 import org.apache.velocity.runtime.parser.node.Node; 28 import org.apache.velocity.runtime.resource.Resource; 29 30 import org.apache.velocity.exception.MethodInvocationException; 31 import org.apache.velocity.exception.ResourceNotFoundException; 32 33 68 public class Include extends InputBase 69 { 70 private String outputMsgStart = ""; 71 private String outputMsgEnd = ""; 72 73 76 public String getName() 77 { 78 return "include"; 79 } 80 81 84 public int getType() 85 { 86 return LINE; 87 } 88 89 93 public void init(RuntimeServices rs, InternalContextAdapter context, 94 Node node) 95 throws Exception 96 { 97 super.init( rs, context, node ); 98 99 103 outputMsgStart = rsvc.getString(RuntimeConstants.ERRORMSG_START); 104 outputMsgStart = outputMsgStart + " "; 105 106 outputMsgEnd = rsvc.getString(RuntimeConstants.ERRORMSG_END ); 107 outputMsgEnd = " " + outputMsgEnd; 108 } 109 110 115 public boolean render(InternalContextAdapter context, 116 Writer writer, Node node) 117 throws IOException , MethodInvocationException, 118 ResourceNotFoundException 119 { 120 123 124 int argCount = node.jjtGetNumChildren(); 125 126 for( int i = 0; i < argCount; i++) 127 { 128 131 132 Node n = node.jjtGetChild(i); 133 134 if ( n.getType() == ParserTreeConstants.JJTSTRINGLITERAL || 135 n.getType() == ParserTreeConstants.JJTREFERENCE ) 136 { 137 if (!renderOutput( n, context, writer )) 138 outputErrorToStream( writer, "error with arg " + i 139 + " please see log."); 140 } 141 else 142 { 143 rsvc.error("#include() error : invalid argument type : " 144 + n.toString()); 145 outputErrorToStream( writer, "error with arg " + i 146 + " please see log."); 147 } 148 } 149 150 return true; 151 } 152 153 161 private boolean renderOutput( Node node, InternalContextAdapter context, 162 Writer writer ) 163 throws IOException , MethodInvocationException, 164 ResourceNotFoundException 165 { 166 String arg = ""; 167 168 if ( node == null ) 169 { 170 rsvc.error("#include() error : null argument"); 171 return false; 172 } 173 174 177 Object value = node.value( context ); 178 if ( value == null) 179 { 180 rsvc.error("#include() error : null argument"); 181 return false; 182 } 183 184 187 arg = value.toString(); 188 189 Resource resource = null; 190 191 try 192 { 193 resource = rsvc.getContent(arg, getInputEncoding(context)); 194 } 195 catch ( ResourceNotFoundException rnfe ) 196 { 197 200 201 rsvc.error("#include(): cannot find resource '" + arg + 202 "', called from template " + 203 context.getCurrentTemplateName() + " at (" + 204 getLine() + ", " + getColumn() + ")" ); 205 throw rnfe; 206 } 207 208 catch (Exception e) 209 { 210 rsvc.error("#include(): arg = '" + arg + 211 "', called from template " + 212 context.getCurrentTemplateName() + " at (" + 213 getLine() + ", " + getColumn() + ") : " + e); 214 } 215 216 if ( resource == null ) 217 return false; 218 219 writer.write((String )resource.getData()); 220 return true; 221 } 222 223 228 private void outputErrorToStream( Writer writer, String msg ) 229 throws IOException 230 { 231 if ( outputMsgStart != null && outputMsgEnd != null) 232 { 233 writer.write(outputMsgStart); 234 writer.write(msg); 235 writer.write(outputMsgEnd); 236 } 237 return; 238 } 239 } 240 | Popular Tags |