3

I am trying generate an HTML file using XML transform (I have an XSL and XML files as input). I am using the standard API (javax.xml.transform.*) from Java 8 to initialize the transformer:

    String xslFile = "my_file.xsl";

    // Create transformer factory
    TransformerFactory factory = TransformerFactory.newInstance();

    // Use the factory to create a template containing the xsl file
    Templates template = factory.newTemplates(new StreamSource(
            new FileInputStream(xslFile)));

When the XSL file is small, everything works fine. But when I embed a lot of javascript inside the XSL file, I get to following exception on the factory.newTemplates() line :

java.io.UTFDataFormatException: encoded string too long: 98793 bytes
at java.io.DataOutputStream.writeUTF(DataOutputStream.java:364)
at java.io.DataOutputStream.writeUTF(DataOutputStream.java:323)
at com.sun.org.apache.bcel.internal.classfile.ConstantUtf8.dump(ConstantUtf8.java:128)
at com.sun.org.apache.bcel.internal.classfile.ConstantPool.dump(ConstantPool.java:233)
at com.sun.org.apache.bcel.internal.classfile.JavaClass.dump(JavaClass.java:327)
at com.sun.org.apache.bcel.internal.classfile.JavaClass.dump(JavaClass.java:312)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.dumpClass(XSLTC.java:905)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.translate(Stylesheet.java:739)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:493)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:568)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:977)
at gov.nasa.gsfc.drl.rtstps.report.ReportUtil.main(ReportUtil.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Any ideas what is the problem here and are there any workarounds?

  • Do you need to use `DataOutputStream`? I thought you generate HTML (that should be a `Writer` or a plain `OutputStream`). – Thilo Nov 16 '15 at 09:14
  • Okay, that `DataOutputStream` seems to be used internally by the XSLT library. Never mind. – Thilo Nov 16 '15 at 09:17
  • Over here (http://www.oxygenxml.com/forum/topic6330.html) people suggest using a different XSLT implementation (Saxon instead of Xalan, which is supposedly not well maintained). I think you can configure the TransformerFactory to use a third-party library instead of the built-in. – Thilo Nov 16 '15 at 09:20

1 Answers1

1

Ok, to answer my own question: there seems to be a limitation of 64kB per block when writing UTF data using DataOutputStream (because two bytes are used to store the length of the data to be written). The problem in my case was that the static content (embedded javascript) was longer than 64k. So what I did was to brake it down to chunks smaller than 64k by inserting these xsl commands that do nothing:

<xsl:if test="1 > 2">
</xsl:if>