01: /*
02: * Created on Aug 13, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.io.IOException;
07: import java.io.StringReader;
08: import java.io.StringWriter;
09: import java.util.Map;
10:
11: import freemarker.template.Template;
12:
13: /**
14: * @author Nils Kilden-Pedersen
15: */
16: final class FreeMarkerStatement extends DynamicStatement {
17:
18: private final Template template;
19:
20: /**
21: * Constructor.
22: * @param id
23: * @param statement
24: * @param resultObjectDef
25: */
26: FreeMarkerStatement(String id, String statement,
27: ResultObjectDefinition resultObjectDef) {
28: super (id, resultObjectDef);
29: try {
30: this .template = new Template(id,
31: new StringReader(statement));
32: } catch (IOException e) {
33: throw new ConfigurationException(e);
34: }
35: }
36:
37: /**
38: * @inheritDoc
39: * @throws BrokerException
40: * @see net.sourceforge.orbroker.Statement#getRuntimeStatement(net.sourceforge.orbroker.Parameters, java.util.Map)
41: */
42: ImmutableSQL getRunnableSQL(ConnectionContext context)
43: throws BrokerException {
44: Map rootMap = context.copyParameters();
45: StringWriter writer = new StringWriter();
46: try {
47: this .template.process(rootMap, writer);
48: } catch (Exception e) {
49: throw buildFailedParsingException(e);
50: }
51: logBrokerStatement(writer.getBuffer(),
52: Statement.Type.FREEMARKER);
53: return new SQLParser(writer.getBuffer())
54: .getRunnableStatement(context.getTextReplacements());
55: }
56: }
|