01: /**
02: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */package nz.org.take.compiler.reference;
10:
11: import java.io.PrintWriter;
12: import org.apache.velocity.Template;
13: import org.apache.velocity.VelocityContext;
14: import nz.org.take.Comparison;
15: import nz.org.take.Query;
16: import nz.org.take.compiler.CompilerException;
17:
18: /**
19: * Plugin used to generate code for comparison predicates.
20: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
21: */
22:
23: public class CompilerPlugin4Comparisons extends CompilerPlugin {
24:
25: public CompilerPlugin4Comparisons(DefaultCompiler owner) {
26: super (owner);
27:
28: }
29:
30: @Override
31: public void checkPrerequisites(Query q) throws CompilerException {
32: boolean[] in = q.getInputParams();
33: for (boolean f : in) {
34: if (!f)
35: throw new CompilerException(
36: "For queries using comparisons, all parameters have to be input parameters. Query is: "
37: + q);
38: }
39: }
40:
41: @Override
42: public String createMethod(PrintWriter out, Query q)
43: throws CompilerException {
44:
45: Comparison p = (Comparison) q.getPredicate();
46:
47: // load and (lazy) init template
48: String templateName = "Comparison_11.vm";
49: Template template = TemplateManager.getInstance().getTemplate(
50: templateName);
51:
52: // bind template variables
53: Slot[] slots = this .buildSlots(q.getPredicate());
54: String methodName = getMethodName(q);
55: VelocityContext context = new VelocityContext();
56: context.put("query", q);
57: context.put("methodname", methodName);
58: context.put("symbol", p.getSymbol());
59: context.put("slot1", slots[0]);
60: context.put("slot2", slots[1]);
61: context.put("resulttype", getClassName(p));
62: context.put("templatename", templateName);
63:
64: try {
65: template.merge(context, out);
66: } catch (Exception x) {
67: throw new CompilerException(
68: "Problem merging compilation template", x);
69: }
70: return methodName;
71: }
72:
73: @Override
74: public boolean supports(Query q) {
75: // check parameters
76: try {
77: this .checkPrerequisites(q);
78: } catch (CompilerException x) {
79: //this.logger.debug(x.getMessage());
80: return false;
81: }
82: // check predicate
83: return q.getPredicate() instanceof Comparison;
84: }
85:
86: }
|