geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r504813 - in /geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template: ./ groovy/ velocity/
Date Thu, 08 Feb 2007 07:43:53 GMT
Author: jdillon
Date: Wed Feb  7 23:43:52 2007
New Revision: 504813

URL: http://svn.apache.org/viewvc?view=rev&rev=504813
Log:
Bring back velocity, organize groovy and velocity template muck together

Added:
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
  (with props)
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
  (with props)
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
  (with props)
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
  (with props)
    geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
  (with props)

Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy?view=auto&rev=504813
==============================================================================
--- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
(added)
+++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
Wed Feb  7 23:43:52 2007
@@ -0,0 +1,83 @@
+/*
+ * 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.template
+
+import gbuild.system.LogSupport
+
+import org.apache.commons.lang.time.StopWatch
+
+/**
+ * Represents a template.
+ */
+abstract class Template extends LogSupport
+{
+    String name
+    
+    Map params = [:]
+    
+    def Template(String name) {
+        assert name
+        
+        this.name = name
+    }
+    
+    protected abstract void doRender(Writer writer)
+    
+    void render(Writer writer) {
+        assert writer
+        
+        log.debug "Rendering template: $name"
+        
+        def watch = new StopWatch()
+        watch.start()
+        
+        doRender(writer)
+        
+        log.debug "Rendered template in $watch"
+    }
+    
+    void render(File file) {
+        assert file
+        
+        //
+        // NOTE: Not using ant here to avoid evil log message
+        //
+        
+        file.parentFile.mkdirs()
+        assert file.parentFile.exists() : "Failed to create directory: ${file.parentFile}"
+        
+        // Fail if we are overriting something
+        assert !file.exists()
+        
+        log.debug "Rendering to: $file"
+        
+        def writer = file.newWriter()
+        try {
+            render(writer)
+        }
+        finally {
+            writer.close()
+        }
+    }
+}

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/Template.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy?view=auto&rev=504813
==============================================================================
--- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
(added)
+++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
Wed Feb  7 23:43:52 2007
@@ -0,0 +1,74 @@
+/*
+ * 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.template.groovy
+
+import gbuild.system.LogSupport
+
+import gbuild.system.template.Template
+
+import groovy.text.SimpleTemplateEngine
+
+/**
+ * Helper to create templates.
+ */
+class TemplateEngine extends LogSupport
+{
+    private SimpleTemplateEngine engine = new SimpleTemplateEngine()
+    
+    private URL baseurl
+    
+    private Map cache = [:]
+    
+    def TemplateEngine(String basepath) {
+        assert basepath
+        
+        if (!basepath.endsWith('/')) {
+            basepath += '/'
+        }
+        
+        baseurl = getClass().classLoader.getResource(basepath)
+        
+        log.debug "Using base URL: $baseurl"
+        
+        assert baseurl
+    }
+    
+    Template getTemplate(String name) {
+        assert name
+        
+        def tmpl = cache[name]
+        if (!tmpl) {
+            def url = new URL(baseurl, name)
+            log.debug "Creating new template: $name, from: $url"
+            
+            tmpl = engine.createTemplate(url)
+            cache[name] = tmpl
+        }
+        else {
+            log.debug "Using cached template: $name"
+        }
+        
+        return new TemplateImpl(name, tmpl)
+    }
+}

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy?view=auto&rev=504813
==============================================================================
--- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
(added)
+++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
Wed Feb  7 23:43:52 2007
@@ -0,0 +1,45 @@
+/*
+ * 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.template.groovy
+
+import gbuild.system.template.Template
+
+/**
+ * Helper to render a template.
+ */
+class TemplateImpl extends Template
+{
+    private groovy.text.Template template
+    
+    def TemplateImpl(String name, groovy.text.Template template) {
+        super(name)
+        
+        assert template
+        this.template = template
+    }
+    
+    protected void doRender(Writer writer) {
+        template.make(params).writeTo(writer)
+    }
+}

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/groovy/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy?view=auto&rev=504813
==============================================================================
--- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
(added)
+++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
Wed Feb  7 23:43:52 2007
@@ -0,0 +1,80 @@
+/*
+ * 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 gbuild.system.template.Template
+
+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) {
+        //
+        // NOTE: Velocity 1.4 does not support URL loading of resources, so they have to
be
+        //       'file' URLs to work
+        //
+        
+        def url = getClass().classLoader.getResource(basepath)
+        assert url
+        assert url.protocol == 'file'
+        
+        def basedir = url.path
+        log.debug "Resources path: $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 TemplateImpl(name, velocity.getTemplate(name))
+    }
+}

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateEngine.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
URL: http://svn.apache.org/viewvc/geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy?view=auto&rev=504813
==============================================================================
--- geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
(added)
+++ geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
Wed Feb  7 23:43:52 2007
@@ -0,0 +1,55 @@
+/*
+ * 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.template.velocity
+
+import gbuild.system.template.Template
+
+import org.apache.velocity.VelocityContext
+
+/**
+ * Wrapper around a Velocity template.
+ */
+class TemplateImpl extends Template
+{
+    private org.apache.velocity.Template template
+    
+    def TemplateImpl(String name, org.apache.velocity.Template template) {
+        super(name)
+        
+        assert template
+        this.template = template
+    }
+    
+    protected void doRender(Writer writer) 
+        assert writer
+        
+        def context = new VelocityContext()
+        
+        params.each {
+            context.put(it.key, it.value)
+        }
+        
+        template.merge(context, writer)
+    }
+}

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/build-support/libraries/system/1/groovy/gbuild/system/template/velocity/TemplateImpl.groovy
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message