| 1 | |
package org.webslinger.template.velocity; |
| 2 | |
|
| 3 | |
import java.io.StringReader; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import org.apache.bsf.BSFException; |
| 8 | |
import org.apache.bsf.BSFManager; |
| 9 | |
import org.apache.velocity.Template; |
| 10 | |
import org.apache.velocity.VelocityContext; |
| 11 | |
import org.apache.velocity.exception.ParseErrorException; |
| 12 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
| 13 | |
import org.apache.velocity.runtime.parser.ParseException; |
| 14 | |
|
| 15 | |
import org.webslinger.template.TemplateManager; |
| 16 | |
|
| 17 | |
public abstract class LocalVelocityTemplate extends Template { |
| 18 | |
protected final String[] names; |
| 19 | |
protected final String path; |
| 20 | |
protected final String text; |
| 21 | |
|
| 22 | 190 | protected LocalVelocityTemplate(String[] names, String path, String text) { |
| 23 | 190 | this.names = names; |
| 24 | 190 | this.path = path; |
| 25 | 190 | this.text = text; |
| 26 | 190 | this.name = path; |
| 27 | 190 | } |
| 28 | |
|
| 29 | |
public boolean process() throws ResourceNotFoundException, ParseErrorException { |
| 30 | |
try { |
| 31 | 190 | data = rsvc.parse(new StringReader(text), path); |
| 32 | 0 | } catch (ParseException e) { |
| 33 | 0 | throw (ParseErrorException) new ParseErrorException(e.getMessage()).initCause(e); |
| 34 | 190 | } |
| 35 | 190 | initDocument(); |
| 36 | 190 | return true; |
| 37 | |
} |
| 38 | |
|
| 39 | |
public void apply(BSFManager manager, Object body, Object[] args) throws BSFException { |
| 40 | 495 | LocalVelocityContext context = args[1] != null ? new LocalVelocityContext(body, (Map) args[1]) : new LocalVelocityContext(body); |
| 41 | 1980 | for (int i = 2; i < names.length; i++) { |
| 42 | 1485 | context.put(names[i], args[i]); |
| 43 | |
} |
| 44 | 495 | context.put("bsf", manager); |
| 45 | |
try { |
| 46 | 495 | merge(context, (Writer) args[0]); |
| 47 | 0 | } catch (Error e) { |
| 48 | 0 | throw e; |
| 49 | 0 | } catch (RuntimeException e) { |
| 50 | 0 | throw e; |
| 51 | 0 | } catch (Exception e) { |
| 52 | 0 | throw (BSFException) new BSFException(e.getMessage()).initCause(e); |
| 53 | 495 | } |
| 54 | 495 | } |
| 55 | |
} |
| 56 | |
|