KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > VelocityGenerator


1 package org.apache.beehive.controls.runtime.generator;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.Template;
30 import org.apache.velocity.app.VelocityEngine;
31 import org.apache.velocity.exception.VelocityException;
32 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
33 import org.apache.velocity.runtime.RuntimeConstants;
34
35 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
36
37 /**
38  * The VelocityGenerator class is an implementation of CodeGenerator that uses standard
39  * Apache Velocity classes from the system classpath.
40  */

41 public class VelocityGenerator extends CodeGenerator
42 {
43     public VelocityGenerator(AnnotationProcessorEnvironment env) throws Exception JavaDoc
44     {
45         super();
46
47         // Create a Velocity engine instance to support codgen
48
_ve = new VelocityEngine();
49         _ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
50         _ve.setProperty("class." + VelocityEngine.RESOURCE_LOADER + ".class",
51                       ClasspathResourceLoader.class.getName());
52         _ve.setProperty("velocimacro.library",
53                       "org/apache/beehive/controls/runtime/generator/ControlMacros.vm");
54
55         // Use the VelocityAptLogSystem to bridge Velocity warnings and errors back to APT
56
VelocityAptLogSystem logger = new VelocityAptLogSystem(env.getMessager());
57         _ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, logger);
58
59         _ve.init();
60     }
61
62     /**
63      * Implementation of the CodeGenerator.generate() method, using standard Velocity
64      * package naming conventions and the system class loader
65      */

66     public void generate(GeneratorOutput genOut) throws CodeGenerationException
67     {
68         //
69
// Create a new VelocityContext
70
//
71
VelocityContext vc = new VelocityContext();
72
73         //
74
// Transfer any code generation properties excepted by the templates into the context
75
//
76
HashMap JavaDoc<String JavaDoc,Object JavaDoc> genContext = genOut.getContext();
77         for(String JavaDoc key : genContext.keySet())
78             vc.put(key, genContext.get(key));
79
80         try
81         {
82             Writer JavaDoc genWriter = genOut.getWriter();
83             Template template = getTemplate(genOut.getTemplateName());
84             template.merge(vc, genWriter);
85             genWriter.close();
86         }
87         catch (RuntimeException JavaDoc re) { throw re; } // never wrap RuntimeExceptions
88
catch (Exception JavaDoc e) { throw new CodeGenerationException(e); }
89     }
90
91     //
92
// Returns the requested template, and caches the result for subsequent requests using the
93
// same template.
94
//
95
public Template getTemplate(String JavaDoc templateName) throws Exception JavaDoc
96     {
97         if (_templateMap.containsKey(templateName))
98             return _templateMap.get(templateName);
99
100         Template t = _ve.getTemplate(templateName);
101         _templateMap.put(templateName, t);
102         return t;
103     }
104
105     private HashMap JavaDoc<String JavaDoc, Template> _templateMap = new HashMap JavaDoc<String JavaDoc, Template>();
106     private VelocityEngine _ve;
107 }
108
Popular Tags