1 package org.apache.velocity; 2 3 18 19 import java.io.InputStream ; 20 import java.io.Writer ; 21 import java.io.BufferedReader ; 22 import java.io.InputStreamReader ; 23 import java.io.UnsupportedEncodingException ; 24 25 import org.apache.velocity.runtime.resource.Resource; 26 import org.apache.velocity.runtime.parser.ParseException; 27 import org.apache.velocity.runtime.parser.node.SimpleNode; 28 29 import org.apache.velocity.VelocityContext; 30 import org.apache.velocity.context.Context; 31 import org.apache.velocity.context.InternalContextAdapterImpl; 32 33 import org.apache.velocity.exception.ResourceNotFoundException; 34 import org.apache.velocity.exception.ParseErrorException; 35 import org.apache.velocity.exception.MethodInvocationException; 36 37 57 public class Template extends Resource 58 { 59 64 private boolean initialized = false; 65 66 private Exception errorCondition = null; 67 68 69 public Template() 70 { 71 } 72 73 84 public boolean process() 85 throws ResourceNotFoundException, ParseErrorException, Exception 86 { 87 data = null; 88 InputStream is = null; 89 errorCondition = null; 90 91 94 try 95 { 96 is = resourceLoader.getResourceStream(name); 97 } 98 catch( ResourceNotFoundException rnfe ) 99 { 100 103 104 errorCondition = rnfe; 105 throw rnfe; 106 } 107 108 112 113 if (is != null) 114 { 115 118 119 try 120 { 121 BufferedReader br = new BufferedReader ( new InputStreamReader ( is, encoding ) ); 122 123 data = rsvc.parse( br, name); 124 initDocument(); 125 return true; 126 } 127 catch( UnsupportedEncodingException uce ) 128 { 129 String msg = "Template.process : Unsupported input encoding : " + encoding 130 + " for template " + name; 131 132 errorCondition = new ParseErrorException( msg ); 133 throw errorCondition; 134 } 135 catch ( ParseException pex ) 136 { 137 140 141 errorCondition = new ParseErrorException( pex.getMessage() ); 142 throw errorCondition; 143 } 144 catch( Exception e ) 145 { 146 149 150 errorCondition = e; 151 throw e; 152 } 153 finally 154 { 155 158 is.close(); 159 } 160 } 161 else 162 { 163 166 167 errorCondition = new ResourceNotFoundException("Unknown resource error for resource " + name ); 168 throw errorCondition; 169 } 170 } 171 172 178 public void initDocument() 179 throws Exception 180 { 181 184 185 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( new VelocityContext() ); 186 187 try 188 { 189 192 193 ica.pushCurrentTemplateName( name ); 194 195 198 199 ((SimpleNode)data).init( ica, rsvc); 200 } 201 finally 202 { 203 207 208 ica.popCurrentTemplateName(); 209 } 210 211 } 212 213 228 public void merge( Context context, Writer writer) 229 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception 230 { 231 236 237 if (errorCondition != null) 238 { 239 throw errorCondition; 240 } 241 242 if( data != null) 243 { 244 248 249 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context ); 250 251 try 252 { 253 ica.pushCurrentTemplateName( name ); 254 ica.setCurrentResource( this ); 255 256 ( (SimpleNode) data ).render( ica, writer); 257 } 258 finally 259 { 260 263 ica.popCurrentTemplateName(); 264 ica.setCurrentResource( null ); 265 } 266 } 267 else 268 { 269 272 273 String msg = "Template.merge() failure. The document is null, " + 274 "most likely due to parsing error."; 275 276 rsvc.error(msg); 277 throw new Exception (msg); 278 } 279 } 280 } 281 282 283 | Popular Tags |