001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.generator;
020:
021: import java.io.Writer;
022: import java.util.HashMap;
023:
024: import org.apache.velocity.VelocityContext;
025: import org.apache.velocity.Template;
026: import org.apache.velocity.app.VelocityEngine;
027: import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
028:
029: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
030:
031: /**
032: * The VelocityGenerator class is an implementation of CodeGenerator that uses standard
033: * Apache Velocity classes from the system classpath.
034: */
035: public class VelocityGenerator extends CodeGenerator {
036: public VelocityGenerator(AnnotationProcessorEnvironment env)
037: throws Exception {
038: super ();
039:
040: // Create a Velocity engine instance to support codgen
041: _ve = new VelocityEngine();
042: _ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
043: _ve.setProperty("class." + VelocityEngine.RESOURCE_LOADER
044: + ".class", ClasspathResourceLoader.class.getName());
045: _ve
046: .setProperty("velocimacro.library",
047: "org/apache/beehive/controls/runtime/generator/ControlMacros.vm");
048:
049: // Use the VelocityAptLogSystem to bridge Velocity warnings and errors back to APT
050: VelocityAptLogSystem logger = new VelocityAptLogSystem(env
051: .getMessager());
052: _ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, logger);
053:
054: _ve.init();
055: }
056:
057: /**
058: * Implementation of the CodeGenerator.generate() method, using standard Velocity
059: * package naming conventions and the system class loader
060: */
061: public void generate(GeneratorOutput genOut)
062: throws CodeGenerationException {
063: //
064: // Create a new VelocityContext
065: //
066: VelocityContext vc = new VelocityContext();
067:
068: //
069: // Transfer any code generation properties excepted by the templates into the context
070: //
071: HashMap<String, Object> genContext = genOut.getContext();
072: for (String key : genContext.keySet())
073: vc.put(key, genContext.get(key));
074:
075: try {
076: Writer genWriter = genOut.getWriter();
077: Template template = getTemplate(genOut.getTemplateName());
078: template.merge(vc, genWriter);
079: genWriter.close();
080: }
081: // never wrap RuntimeException
082: catch (RuntimeException re) {
083: throw re;
084: } catch (Exception e) {
085: throw new CodeGenerationException(e);
086: }
087: }
088:
089: //
090: // Returns the requested template, and caches the result for subsequent requests using the
091: // same template.
092: //
093: public Template getTemplate(String templateName) throws Exception {
094: if (_templateMap.containsKey(templateName))
095: return _templateMap.get(templateName);
096:
097: Template t = _ve.getTemplate(templateName);
098: _templateMap.put(templateName, t);
099: return t;
100: }
101:
102: private HashMap<String, Template> _templateMap = new HashMap<String, Template>();
103: private VelocityEngine _ve;
104: }
|