Below is a sample piece of code that I am using to use an XSL file to generate HTML file. In the stylesheet itself is a recurisive call that converts carriage returns to
tags so the carriage returns carry-over to the HTML doc. I am finding that in some of the large narratives (that contain over 350 carraige returns or so) I am getting a java.lang.StackOverflowError exception and the java app is aborting.
I researching I see that the stack size of the java virual machine is configurable using the -Xss switch. But I can't seem to get that to have any affect. I have tried -Xss1m and -Xss1k and the results are the same.
Any thoughts on how to solve this problem
- Should I apply the stylesheet in a different way?
- How can I set the stack size or is a another parameter need to be set
- Any other thoughts in which I should be intereseted in?
Thanks
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
public class XSLT_test {
public static void main(String[] args) {
try {
File xslFile = new File("c:\\XSLT_Test\\MN0271100_SR.xsl");
File xmlFile = new File("c:\\XSLT_Test\\TM-2005-064310.xml");
StreamSource xmlSource = new StreamSource(xmlFile);
StreamSource xsltSource = new StreamSource(xslFile);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, new StreamResult(new File("c:\\XSLT_Test\\report.html")));
} catch (Exception ex) {
System.out.println("Error");
System.out.println(ex.toString());
}
}
}
Template that is called recurisevly
<xsl:template name="text.wrap">
<!--
TEMPLATE text.wrap
PURPOSE Replace carriage return characters in text with the <br/> tag
so that text wraps naturally
USAGE
<xsl:call-template name="text.wrap"
<xsl:with-param name="texttowrap" select="your-xpath-here"/>
</xsl:call-template>
PARAMS
texttowrap TEXT Text to be wrapped
NOTES Recursive
-->
<xsl:param name="texttowrap"/>
<xsl:variable name="textlength" select="string-length($texttowrap)"/>
<!-- don't waste time if no text supplied or remaining from recursion-->
<xsl:if test="$textlength > 0">
<xsl:choose>
<xsl:when test="contains($texttowrap,$CR)">
<!-- if there is any text to print (i.e. text before the first break) print it
now, add the <br/> tag to break the line -->
<xsl:value-of select="substring-before($texttowrap,$CR)"/>
<br/>
<!-- if any is left, recurse-->
<xsl:if test="string-length(substring-after($texttowrap,$CR)) > 0">
<xsl:call-template name="text.wrap">
<xsl:with-param name="texttowrap" select="substring-after
($texttowrap,$CR)"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<!-- either the text passed has no carriage returns, or recursion has
eliminated all of them in either case, just print the remainder -->
<xsl:value-of select="$texttowrap"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>