Page 1 of 1

Xhtml -> Docx: NPE during loading image with wrong url

PostPosted: Fri Feb 08, 2013 6:32 am
by denys.zhosan
I'm using docx4j in my pet project and I faced with some problem. During converting xhtml code to docx format I've got NullPointerException in some cases.

Test to reproduce issue:
Code: Select all

import org.docx4j.wml.Text;
import org.docx4j.wml.R;
import org.docx4j.wml.P;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.junit.Test;
import static org.junit.Assert.*;

...

@Test public void testLoadingNonExistantImage() throws InvalidFormatException, FileNotFoundException, Docx4JException {
      try {
         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

         String xhtml = "<?xml version=\"1.0\" encoding=\"Cp1252\"?>"
               + "<html><head /><body><h1><b><ceneter>Hello world!</ceneter></b></h1>
"
               + "<img src=\"http://unrealsitename/wrongimage.jpg\" alt=\"non existant image description\" />Some text"
               + "</body></html>";


         List<Object> converted = XHTMLImporter.convert(xhtml, "", wordMLPackage);

         assertTrue(converted.size() > 0);

      } catch (NullPointerException e) {
         fail("Failed due to NPE");
      }
   }

...


I reviewed the code and found that there is no error handling for images with bad urls e.g. non-existing images. XHTMLImporter.java: line 1020
I fixed this problem for my local build. (Patch with tests attached if someone need).

It is possible to get whitespaces warnings from git during patch applying, but it seems this is non crucial issue.

Contributed on the basis of the document at http://dev.plutext.org/docx4j/docx4j_IndividualContributor.docx
Denys.

Re: Xhtml -> Docx: NPE during loading image with wrong url

PostPosted: Sun Feb 10, 2013 7:18 pm
by jason