commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Marco Tedone" <mted...@jemos.co.uk>
Subject [Jelly] Problems while parsing special characters
Date Sun, 18 Mar 2007 18:46:19 GMT
Hi all,

I'm having a problem with Jelly and generics :)

I wrote a little tool to parse Javadocs and annotations on Java sources. I 
use this tool in conjunction with Jelly for the
auto generation of files starting from Java code (a simplified version of 
XDoclet if you want). I'm not using this
tool to generate Java interfaces starting from Java classes. Look at the 
following class (written just for unit testing):

[code]

package uk.co.jemos.clanker.test.dummyclasses.classes;
import java.util.ArrayList;
/**
* @jemos-generics
* @author mtedone
*
*/
public class GenericsTestClass {
    public ArrayList<String> getStrings() {
        return new ArrayList<String>();
    }
}
[/code]

I encapsulate all methods information in a 'Method' JavaBean, which has got 
a getMethodSignature() which returns
the method signature as a String. In the above scenario, the generated 
interfaces is as follows:

[list]
//Auto-generated by Jemos Clanker. Do not edit
package uk.co.jemos.mbean.ins;
/**
* Defines interface for class uk.co.jemos.mbean.ins.Hello
*/
public interface HelloMBean {
    public ArrayList&lt;String&gt; getHellos();
}
[/list]

I then surfed the net and found a suggestion about the above problem which 
consisted in using the commons-lang class StringEscapeUtils
(http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringEscapeUtils.html)

I then created a new method which returned a version for XML, by adding the 
following at the end of my method (instead of returning the plain string):

return StringEscapeUtils.escapeXml(myString).

Now, whereas my Method JavaBean returns the following string:

[list]
public ArrayList&lt;String&gt; getStrings();
[/list]

which is the correct escaping for XML, my interface is written as follows:

[list]
//Auto-generated by Jemos Clanker. Do not edit
package uk.co.jemos.mbean.ins;
/**
* Defines interface for class uk.co.jemos.mbean.ins.Hello
*/
public interface HelloMBean {
    public ArrayList&amp;lt;String&amp;gt; getHellos();
}
[/list]

instead of printing out the <> correctly, it added an &amp; string to the 
escaped characters.

This is my jelly script:

[list]
<?xml version="1.0"?>
<j:jelly trim="false"
    xmlns:j="jelly:core"
    xmlns:x="jelly:xml"
    xmlns:html="jelly:html">
    //Auto-generated by Jemos Clanker. Do not edit
    package ${packageName};
    /**
    * Defines interface for class ${clazz.fullName}
    */
    public interface ${clazz.name}${suffix} {
    <j:forEach items="${clazz.methods}" var="method">
        <j:if test="${method.hasJavadocTag('@jemos.mbean-method')}">
            ${method.methodSignatureForXml}
        </j:if>
    </j:forEach>
}
</j:jelly>
[/list]

This script is invoked by a Jelly Tag class which I wrote especially to 
generate interfaces:

[list]
private void generateInterfaces(String outDir) throws FormatterException {
    //A logging buffer
    StringBuffer buff = new StringBuffer();
    //The Java interface name
    StringBuffer fileName = null;
    //The Output Stream
    OutputStream os = null;
    //The jelly context
    JellyContext context = new JellyContext();
    //Sets the variables common to all
    context.setVariable("suffix", this.getSuffix());
    context.setVariable("packageName", this.getPackageName());
    //For each class, it sets the context variables and runs the
    //jelly script which contains the interfaces skeleton
    for (Clazz c: this.getClasses()) {
        fileName = new StringBuffer(outDir);
        if (!outDir.endsWith("/")) {
            fileName.append(File.separatorChar);
        }
        fileName.append(c.getName())
        .append(this.getSuffix())
        .append(".java");
        LOG.info("Creating: " + fileName.toString());
        try {
            os = new FileOutputStream(new File(fileName.toString()));
            //Sets the context variables for each class
            context.setVariable("clazz", c);
            //It then invokes the interface jelly skeleton
            //which will generate the interface.
            XMLOutput output = XMLOutput.createXMLOutput(os);
            context.runScript(new File(this.getJellyScript()), output);
        } catch (FileNotFoundException e) {
            buff.append("An error occurred while trying to create")
            .append(" the file output stream. The following file name: ")
            .append(fileName)
            .append(" wasn't found.");
            LOG.severe(buff.toString());
            throw new FormatterException(buff.toString(), e);
        } catch (UnsupportedEncodingException e) {
            buff.append("An error occurred while creating the XMLOutput")
            .append(" object to write the Java interface to. This")
            .append(" is the exception thrown by the application:")
            .append("\n")
            .append(e.getLocalizedMessage());
            LOG.severe(buff.toString());
            throw new FormatterException(buff.toString(), e);
        } catch (JellyException e) {
            buff.append("An error occurred while running the jelly script: 
")
            .append(this.getJellyScript())
            .append(".");
            LOG.severe(buff.toString());
            throw new FormatterException(buff.toString(), e);
        }
    }
}
[/list]

Any help on how to solve this problem would be greatly appreciated.

Regards,

Marco




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message