You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by "Thomas Fischer (JIRA)" <ji...@apache.org> on 2007/10/01 22:52:50 UTC

[jira] Updated: (TORQUE-71) custom velocity renderer to have nicely trimmed generated code

     [ https://issues.apache.org/jira/browse/TORQUE-71?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Fischer updated TORQUE-71:
---------------------------------

    Attachment: TorqueDataModelTask.java

The provided patch did not work, it threw an InstantiationException on a changed loader. Here is a new patch. Also, the handling of encoding in the filter was improved in the patch

> custom velocity renderer to have nicely trimmed generated code
> --------------------------------------------------------------
>
>                 Key: TORQUE-71
>                 URL: https://issues.apache.org/jira/browse/TORQUE-71
>             Project: Torque
>          Issue Type: Improvement
>          Components: Generator
>            Reporter: Thoralf Rickert
>            Priority: Minor
>         Attachments: patch.txt, TorqueDataModelTask.java
>
>
> The current Torque templates have spaces and tabs in front of Velocity commands (#foreach, #set, #end,...). The Velocity renderer puts this spaces and tabs into the generated code - that is the reason why the code in generated Base* class files looks sometimes a little bit ugly. If we remove the leading spaces in front of Velocity commands we can avoid this problem.
> My solution is to override the rendering method in org.apache.velocity.texen.ant.TexenTask that Torque uses to render the templates and replace the template loader with our own. Therefor we can remove the leading spaces.
>   /**
>    * This method filters the template and replaces some
>    * unwanted characters. For example it removes leading
>    * spaces in front of velocity commands and replaces
>    * tabs with spaces to prevent bounces in different
>    * code editors with different tab-width-setting.
>    */
>   protected byte[] filter(String template, String encoding) throws Exception {
>     StringReader stringReader = new StringReader(template);
>     LineNumberReader lineNumberReader = new LineNumberReader(stringReader);
>     String line = null;
>     ByteArrayOutputStream baos = new ByteArrayOutputStream();
>     PrintStream ps = new PrintStream(baos,true,encoding);
>     while ((line = lineNumberReader.readLine()) != null) {
>       // remove leading spaces in front of velocity commands and comments
>       line = line.replaceAll("^\\s*#", "#");
>       // replace tabs with spaces to prevent bounces in editors
>       line = line.replaceAll("\t","    ");
>       ps.println(line);
>     }
>     ps.flush();
>     ps.close();
>     return baos.toByteArray();
>   }
> The only problem is, that this would generate an error if you use empty statements in #if or #foreach commands, for example
>   #if (something)
>   #end
> This would create an error in velocity's render engine if you remove the leading spaces - because empty statements are not allowed in Velocity. But this can be avoided by add a single new line:
>   #if (something)
>   #end
> A patch for the current generator comes in the next days.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org