Author: apetrelli
Date: Sat Feb 27 17:01:49 2010
New Revision: 916989

URL: http://svn.apache.org/viewvc?rev=916989&view=rev
Log:
TILESSB-22
Added generation of tag classes.

Added:
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
   (with props)
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/resources/org/apache/tiles/autotag/jsp/bodyTag.vm
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
   (with props)
    tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
   (with props)
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
   (with props)
Modified:
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/generate/BasicTemplateGenerator.java
    
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/model/TemplateParameter.java

Modified: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/generate/BasicTemplateGenerator.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/generate/BasicTemplateGenerator.java?rev=916989&r1=916988&r2=916989&view=diff
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/generate/BasicTemplateGenerator.java
 (original)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/generate/BasicTemplateGenerator.java
 Sat Feb 27 17:01:49 2010
@@ -30,11 +30,11 @@
         }
     }
 
-    protected void addTemplateSuiteGenerator(TemplateSuiteGenerator generator) 
{
+    public void addTemplateSuiteGenerator(TemplateSuiteGenerator generator) {
         templateSuiteGenerators.add(generator);
     }
 
-    protected void addTemplateClassGenerator(TemplateClassGenerator generator) 
{
+    public void addTemplateClassGenerator(TemplateClassGenerator generator) {
         templateClassGenerators.add(generator);
     }
 }

Modified: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/model/TemplateParameter.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/model/TemplateParameter.java?rev=916989&r1=916988&r2=916989&view=diff
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/model/TemplateParameter.java
 (original)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-core/src/main/java/org/apache/tiles/autotag/model/TemplateParameter.java
 Sat Feb 27 17:01:49 2010
@@ -47,6 +47,10 @@
         return Request.class.getName().equals(type);
     }
 
+    public String getGetterSetterSuffix() {
+        return name.substring(0, 1).toUpperCase() + name.substring(1);
+    }
+
     @Override
     public String toString() {
         return "TemplateParameter\n[documentation="

Added: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java?rev=916989&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
 (added)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
 Sat Feb 27 17:01:49 2010
@@ -0,0 +1,48 @@
+package org.apache.tiles.autotag.jsp;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.Writer;
+
+import org.apache.tiles.autotag.core.AutotagRuntimeException;
+import org.apache.tiles.autotag.generate.TemplateClassGenerator;
+import org.apache.tiles.autotag.model.TemplateClass;
+import org.apache.tiles.autotag.model.TemplateSuite;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+
+public class TagClassGenerator implements TemplateClassGenerator {
+
+    @Override
+    public void generate(File directory, String packageName,
+            TemplateSuite suite, TemplateClass clazz) {
+        File dir = new File(directory, packageName.replaceAll("\\.", "/"));
+        dir.mkdirs();
+        File javaFile = new File(dir, clazz.getTagClassPrefix() + "Tag.java");
+        VelocityContext context = new VelocityContext();
+        context.put("packageName", packageName);
+        context.put("suite", suite);
+        context.put("clazz", clazz);
+        try {
+            javaFile.createNewFile();
+            Template template = 
Velocity.getTemplate("/org/apache/tiles/autotag/jsp/bodyTag.vm");
+            Writer writer = new FileWriter(javaFile);
+            try {
+                template.merge(context, writer);
+            } finally {
+                writer.close();
+            }
+        } catch (ResourceNotFoundException e) {
+            throw new AutotagRuntimeException("Cannot get bodyTag.vm 
resource", e);
+        } catch (ParseErrorException e) {
+            throw new AutotagRuntimeException("The tld.vm resource is not 
parseable", e);
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new AutotagRuntimeException("Another generic exception while 
parsing tld.vm", e);
+        }
+    }
+}

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/java/org/apache/tiles/autotag/jsp/TagClassGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/resources/org/apache/tiles/autotag/jsp/bodyTag.vm
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/resources/org/apache/tiles/autotag/jsp/bodyTag.vm?rev=916989&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/resources/org/apache/tiles/autotag/jsp/bodyTag.vm
 (added)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/main/resources/org/apache/tiles/autotag/jsp/bodyTag.vm
 Sat Feb 27 17:01:49 2010
@@ -0,0 +1,76 @@
+package ${packageName};
+#*
+ * $Id: tiles-jsp.tld 836180 2009-11-14 14:00:02Z apetrelli $
+ *
+ * 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.
+ *#
+import java.io.IOException;
+
+import org.apache.tiles.autotag.core.runtime.ModelBody;
+import org.apache.tiles.request.Request;
+
+/**
+ * ${clazz.documentation}
+ */
+public class ${clazz.tagClassPrefix}Tag extends 
Body#if(!${clazz.hasBody()})less#{end}Tag {
+
+    /**
+     * The template model.
+     */
+    private ${clazz.name} model = new ${clazz.name}();
+
+#foreach($parameter in ${clazz.parameters})
+    /**
+     * ${parameter.documentation}
+     */
+    private ${parameter.type} ${parameter.name};
+
+#end
+#foreach($parameter in ${clazz.parameters})
+    /**
+     * Getter for ${parameter.name} property.
+     *
+     * @return ${parameter.documentation}
+     */
+    public ${parameter.type} #if(${parameter.type} == 
'boolean')is#{else}get#end${parameter.getterSetterSuffix}() {
+        return ${parameter.name};
+    }
+
+    /**
+     * Setter for ${parameter.name} property.
+     *
+     * @param ${parameter.name}
+     * ${parameter.documentation}
+     */
+    public void set${parameter.getterSetterSuffix}(${parameter.type} 
${parameter.name}) {
+        this.${parameter.name} = ${parameter.name};
+    }
+
+#end
+    /** {...@inheritdoc} */
+    @Override
+    public void execute(Request request#if(${clazz.hasBody()}), ModelBody 
modelBody#end) throws IOException {
+        model.execute(
+#foreach($parameter in ${clazz.parameters})
+            ${parameter.name},
+#end
+            request#if(${clazz.hasBody()}), modelBody#end
+
+        );
+    }
+}

Added: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java?rev=916989&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
 (added)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
 Sat Feb 27 17:01:49 2010
@@ -0,0 +1,116 @@
+/**
+ *
+ */
+package org.apache.tiles.autotag.jsp;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.tiles.autotag.core.runtime.ModelBody;
+import org.apache.tiles.autotag.model.TemplateClass;
+import org.apache.tiles.autotag.model.TemplateMethod;
+import org.apache.tiles.autotag.model.TemplateParameter;
+import org.apache.tiles.autotag.model.TemplateSuite;
+import org.apache.tiles.request.Request;
+import org.apache.velocity.app.Velocity;
+import org.junit.Test;
+
+/**
+ * Tests {...@link TagClassGenerator}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class TagClassGeneratorTest {
+
+    /**
+     * Test method for {...@link TagClassGenerator#generate(File, String, 
TemplateSuite, TemplateClass)}.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void testGenerate() throws Exception {
+        TagClassGenerator generator = new TagClassGenerator();
+        File file = File.createTempFile("autotag", null);
+        file.delete();
+        file.mkdir();
+        file.deleteOnExit();
+        TemplateSuite suite = new TemplateSuite("tldtest", "Test for TLD 
docs.");
+        suite.getCustomVariables().put("taglibURI", 
"http://www.initrode.net/tags/test";);
+
+        List<TemplateParameter> params = new ArrayList<TemplateParameter>();
+        TemplateParameter param = new TemplateParameter("one", 
"java.lang.String", true);
+        param.setDocumentation("Parameter one.");
+        params.add(param);
+        param = new TemplateParameter("two", "int", false);
+        param.setDocumentation("Parameter two.");
+        params.add(param);
+        param = new TemplateParameter("three", "boolean", false);
+        param.setDocumentation("Parameter three.");
+        params.add(param);
+        param = new TemplateParameter("request", Request.class.getName(), 
false);
+        param.setDocumentation("The request.");
+        params.add(param);
+        param = new TemplateParameter("modelBody", ModelBody.class.getName(), 
false);
+        param.setDocumentation("The body.");
+        params.add(param);
+        TemplateMethod executeMethod = new TemplateMethod("execute", params);
+
+        TemplateClass clazz = new 
TemplateClass("org.apache.tiles.autotag.template.DoStuffTemplate",
+                "doStuff", "DoStuff", executeMethod);
+        clazz.setDocumentation("Documentation of the DoStuff class.");
+
+        Properties props = new Properties();
+        InputStream propsStream = 
getClass().getResourceAsStream("/org/apache/tiles/autotag/jsp/velocity.properties");
+        props.load(propsStream);
+        propsStream.close();
+        Velocity.init(props);
+
+        generator.generate(file, "org.apache.tiles.autotag.jsp.test", suite, 
clazz);
+
+        InputStream expected = 
getClass().getResourceAsStream("/org/apache/tiles/autotag/jsp/test/DoStuffTag.java");
+        File effectiveFile = new File(file, 
"/org/apache/tiles/autotag/jsp/test/DoStuffTag.java");
+        assertTrue(effectiveFile.exists());
+        InputStream effective = new FileInputStream(effectiveFile);
+        assertTrue(IOUtils.contentEquals(effective, expected));
+
+        suite.addTemplateClass(clazz);
+        params = new ArrayList<TemplateParameter>();
+        param = new TemplateParameter("one", "java.lang.Double", true);
+        param.setDocumentation("Parameter one.");
+        params.add(param);
+        param = new TemplateParameter("two", "float", false);
+        param.setDocumentation("Parameter two.");
+        params.add(param);
+        param = new TemplateParameter("three", "java.util.Date", false);
+        param.setDocumentation("Parameter three.");
+        params.add(param);
+        param = new TemplateParameter("request", Request.class.getName(), 
false);
+        param.setDocumentation("The request.");
+        params.add(param);
+        executeMethod = new TemplateMethod("execute", params);
+
+        clazz = new 
TemplateClass("org.apache.tiles.autotag.template.DoStuffNoBodyTemplate",
+                "doStuffNoBody", "DoStuffNoBody", executeMethod);
+        clazz.setDocumentation("Documentation of the DoStuffNoBody class.");
+
+        suite.addTemplateClass(clazz);
+
+        generator.generate(file, "org.apache.tiles.autotag.jsp.test", suite, 
clazz);
+
+        expected = 
getClass().getResourceAsStream("/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java");
+        effectiveFile = new File(file, 
"/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java");
+        assertTrue(effectiveFile.exists());
+        effective = new FileInputStream(effectiveFile);
+        assertTrue(IOUtils.contentEquals(effective, expected));
+
+        FileUtils.deleteDirectory(file);
+    }
+
+}

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/java/org/apache/tiles/autotag/jsp/TagClassGeneratorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java?rev=916989&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
 (added)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
 Sat Feb 27 17:01:49 2010
@@ -0,0 +1,100 @@
+package org.apache.tiles.autotag.jsp.test;
+
+import java.io.IOException;
+
+import org.apache.tiles.autotag.core.runtime.ModelBody;
+import org.apache.tiles.request.Request;
+
+/**
+ * Documentation of the DoStuffNoBody class.
+ */
+public class DoStuffNoBodyTag extends BodylessTag {
+
+    /**
+     * The template model.
+     */
+    private org.apache.tiles.autotag.template.DoStuffNoBodyTemplate model = 
new org.apache.tiles.autotag.template.DoStuffNoBodyTemplate();
+
+    /**
+     * Parameter one.
+     */
+    private java.lang.Double one;
+
+    /**
+     * Parameter two.
+     */
+    private float two;
+
+    /**
+     * Parameter three.
+     */
+    private java.util.Date three;
+
+    /**
+     * Getter for one property.
+     *
+     * @return Parameter one.
+     */
+    public java.lang.Double getOne() {
+        return one;
+    }
+
+    /**
+     * Setter for one property.
+     *
+     * @param one
+     * Parameter one.
+     */
+    public void setOne(java.lang.Double one) {
+        this.one = one;
+    }
+
+    /**
+     * Getter for two property.
+     *
+     * @return Parameter two.
+     */
+    public float getTwo() {
+        return two;
+    }
+
+    /**
+     * Setter for two property.
+     *
+     * @param two
+     * Parameter two.
+     */
+    public void setTwo(float two) {
+        this.two = two;
+    }
+
+    /**
+     * Getter for three property.
+     *
+     * @return Parameter three.
+     */
+    public java.util.Date getThree() {
+        return three;
+    }
+
+    /**
+     * Setter for three property.
+     *
+     * @param three
+     * Parameter three.
+     */
+    public void setThree(java.util.Date three) {
+        this.three = three;
+    }
+
+    /** {...@inheritdoc} */
+    @Override
+    public void execute(Request request) throws IOException {
+        model.execute(
+            one,
+            two,
+            three,
+            request
+        );
+    }
+}

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffNoBodyTag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
URL: 
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java?rev=916989&view=auto
==============================================================================
--- 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
 (added)
+++ 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
 Sat Feb 27 17:01:49 2010
@@ -0,0 +1,100 @@
+package org.apache.tiles.autotag.jsp.test;
+
+import java.io.IOException;
+
+import org.apache.tiles.autotag.core.runtime.ModelBody;
+import org.apache.tiles.request.Request;
+
+/**
+ * Documentation of the DoStuff class.
+ */
+public class DoStuffTag extends BodyTag {
+
+    /**
+     * The template model.
+     */
+    private org.apache.tiles.autotag.template.DoStuffTemplate model = new 
org.apache.tiles.autotag.template.DoStuffTemplate();
+
+    /**
+     * Parameter one.
+     */
+    private java.lang.String one;
+
+    /**
+     * Parameter two.
+     */
+    private int two;
+
+    /**
+     * Parameter three.
+     */
+    private boolean three;
+
+    /**
+     * Getter for one property.
+     *
+     * @return Parameter one.
+     */
+    public java.lang.String getOne() {
+        return one;
+    }
+
+    /**
+     * Setter for one property.
+     *
+     * @param one
+     * Parameter one.
+     */
+    public void setOne(java.lang.String one) {
+        this.one = one;
+    }
+
+    /**
+     * Getter for two property.
+     *
+     * @return Parameter two.
+     */
+    public int getTwo() {
+        return two;
+    }
+
+    /**
+     * Setter for two property.
+     *
+     * @param two
+     * Parameter two.
+     */
+    public void setTwo(int two) {
+        this.two = two;
+    }
+
+    /**
+     * Getter for three property.
+     *
+     * @return Parameter three.
+     */
+    public boolean isThree() {
+        return three;
+    }
+
+    /**
+     * Setter for three property.
+     *
+     * @param three
+     * Parameter three.
+     */
+    public void setThree(boolean three) {
+        this.three = three;
+    }
+
+    /** {...@inheritdoc} */
+    @Override
+    public void execute(Request request, ModelBody modelBody) throws 
IOException {
+        model.execute(
+            one,
+            two,
+            three,
+            request, modelBody
+        );
+    }
+}

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/sandbox/trunk/tiles-autotag/tiles-autotag-jsp/src/test/resources/org/apache/tiles/autotag/jsp/test/DoStuffTag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL


Reply via email to