Author: jdillon Date: Sat Feb 3 22:54:22 2007 New Revision: 503366 URL: http://svn.apache.org/viewvc?view=rev&rev=503366 Log: Helpers to render templates w/Velocity Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy (with props) geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy (with props) Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy?view=auto&rev=503366 ============================================================================== --- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy (added) +++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy Sat Feb 3 22:54:22 2007 @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// +// $Id$ +// + +package gbuild.system.util + +import gbuild.system.LogSupport + +import org.apache.commons.logging.Log +import org.apache.commons.logging.LogFactory + +import org.apache.commons.lang.time.StopWatch + +import org.apache.velocity.VelocityContext + +/** + * Wrapper around a Velocity template. + */ +class Template extends LogSupport +{ + String name + + Map params = [:] + + private org.apache.velocity.Template template + + def Template(String name, org.apache.velocity.Template template) { + assert name + assert template + + this.name = name + this.template = template + } + + void render(writer) { + assert writer + + log.info "Rendering template: $name" + + def context = new VelocityContext() + + params.each { + context.put(it.key, it.value) + } + + def watch = new StopWatch() + watch.start() + + template.merge(context, writer) + + log.info "Rendered template in $watch" + } +} Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy ------------------------------------------------------------------------------ svn:eol-style = native Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/Template.groovy ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy?view=auto&rev=503366 ============================================================================== --- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy (added) +++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy Sat Feb 3 22:54:22 2007 @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// +// $Id$ +// + +package gbuild.system.util + +import gbuild.system.LogSupport + +import org.apache.commons.lang.time.StopWatch + +import org.apache.velocity.app.VelocityEngine + +/** + * Helper to render templates with Velocity. + */ +class TemplateEngine extends LogSupport +{ + private VelocityEngine velocity = new VelocityEngine() + + // Lazy initialized to allow nicer API to installing libraries + private boolean initialized = false + + def TemplateEngine(basepath) { + def url = getClass().classLoader.getResource(basepath) + assert url + assert url.protocol == 'file' + + def basedir = url.path + log.info "Base dir: $basedir" + + velocity.setProperty('resource.loader', 'file') + velocity.setProperty('file.resource.loader.description', 'Velocity File Resource Loader') + velocity.setProperty('file.resource.loader.class', 'org.apache.velocity.runtime.resource.loader.FileResourceLoader') + velocity.setProperty('file.resource.loader.path', basedir) + velocity.setProperty('file.resource.loader.cache', 'false') + velocity.setProperty('file.resource.loader.modificationCheckInterval', '2') + + velocity.setProperty('runtime.log.logsystem.class', 'org.apache.velocity.runtime.log.NullLogSystem') + } + + def setLibraries(List libs) { + assert libs + assert !initialized + + velocity.setProperty('velocimacro.library', libs.join(',')) + } + + Template getTemplate(name) { + if (!initialized) { + velocity.init() + initialized = true + } + + return new Template(name, velocity.getTemplate(name)) + } +} Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy ------------------------------------------------------------------------------ svn:eol-style = native Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/util/TemplateEngine.groovy ------------------------------------------------------------------------------ svn:mime-type = text/plain