docx4j-ImportXHTML icon indicating copy to clipboard operation
docx4j-ImportXHTML copied to clipboard

docx4j-ImportXHTML crashes when placing <table> with “align=center” inside an <li>

Open pleft opened this issue 8 years ago • 0 comments

The following code crashes in latest (3.3.1) docx4j-ImportXHTML.

public class PlainTests {

    static final String TEST_STRING = "<html>" +
            "<body>" +
            "<ul><li>" +
            "<table align=\"center\" border=\"1\">" +
            "<tr><td>1</td></tr>" +
            "</table>" +
            "</li></ul>" +
            "</body>" +
            "</html>";
    public static void main(String[] args) throws ParserConfigurationException, Docx4JException {

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, true);

        XHTMLImporterImpl XHTMLImporterForContent = new XHTMLImporterImpl(wordMLPackage);
        wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporterForContent.convert(TEST_STRING, null));
        wordMLPackage.save(new File("test.docx"));
    }
}

removing the align="center" works fine.

The exception I am getting is:

Exception in thread "main" java.lang.ClassCastException: org.docx4j.org.xhtmlrenderer.css.constants.IdentValue cannot be cast to org.docx4j.org.xhtmlrenderer.css.style.derived.LengthValue at org.docx4j.convert.in.xhtml.ListHelper.getAbsoluteListItemIndent(ListHelper.java:306) at org.docx4j.convert.in.xhtml.TableHelper.setupTblPr(TableHelper.java:118) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:981) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1240) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1240) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1240) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:1240) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.traverse(XHTMLImporterImpl.java:814) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:692) at PlainTests.main(PlainTests.java:28) 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:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

And by debugging I can see that it crashes because it tries to cast in ListHelper.java:306 an IdentValue ("auto") to LengthValue like the screenshot:

2016-11-14 12_15_53-evaluate expression

pleft avatar Nov 14 '16 10:11 pleft