Issue Details (XML | Word | Printable)

Key: TORQUE-71
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Thoralf Rickert
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Torque

custom velocity renderer to have nicely trimmed generated code

Created: 03/Dec/06 12:14 PM   Updated: 12/Oct/07 08:24 PM
Return to search
Component/s: Generator
Affects Version/s: None
Fix Version/s: 3.3-RC3

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works patch.txt 2006-12-05 02:26 PM Thoralf Rickert 30 kB
Java Source File Licensed for inclusion in ASF works TorqueDataModelTask.java 2007-10-01 08:52 PM Thomas Fischer 24 kB

Resolution Date: 12/Oct/07 08:24 PM


 Description  « Hide
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.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Thoralf Rickert added a comment - 05/Dec/06 02:26 PM
Okay, here is a first patch. I didn't test it, because I don't have a suitable test environment. What I did was to change the TorqueDataModelTask class. I've copied! the execute method from velocity-TexenTask class. Then I've changed the TemplateLoader for FileResourceLoader and ClasspathResourceLoader with my own loaders (which extends File- and ClasspathResourceLoader) and call the filter method (it uses streams now).

I'm not sure if the code compiles and runs because the Task uses two different StringUtils classes and I'm not sure, if I checked everything. AND I'm not sure if the template loader has any problems with the template syntax (see discussion in July). Please report any problems - I'll try to find the problem.

We use this filter here since last year in our own TemplateLoader which get's the templates from a database - so I'm sure that it works in principle but there could be one or more templates that has to be changed.

Thomas Fischer added a comment - 01/Oct/07 08:52 PM
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

Thomas Fischer added a comment - 01/Oct/07 09:07 PM
The patch still needs to be javadoced

Thomas Fischer added a comment - 12/Oct/07 08:24 PM
Patch applied, thanks