Fix bug 59322: NullPointerException when converting a certain Word document to HTML

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866185 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java
index 83a7b0b..3dca841 100644
--- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java
+++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java
@@ -18,97 +18,132 @@
 package org.apache.poi.hwpf.usermodel;
 
 public final class TableCell
-  extends Range
-{
+        extends Range {
   private int _levelNum;
   private TableCellDescriptor _tcd;
   private int _leftEdge;
   private int _width;
 
-    public TableCell( int startIdxInclusive, int endIdxExclusive,
-            TableRow parent, int levelNum, TableCellDescriptor tcd,
-            int leftEdge, int width )
-    {
-        super( startIdxInclusive, endIdxExclusive, parent );
-        _tcd = tcd;
-        _leftEdge = leftEdge;
-        _width = width;
-        _levelNum = levelNum;
+  public TableCell(int startIdxInclusive, int endIdxExclusive,
+                   TableRow parent, int levelNum, TableCellDescriptor tcd,
+                   int leftEdge, int width) {
+    super(startIdxInclusive, endIdxExclusive, parent);
+    _tcd = tcd;
+    _leftEdge = leftEdge;
+    _width = width;
+    _levelNum = levelNum;
+  }
+
+  public boolean isFirstMerged() {
+    if (_tcd == null) {
+      return false;
     }
 
-  public boolean isFirstMerged()
-  {
     return _tcd.isFFirstMerged();
   }
 
-  public boolean isMerged()
-  {
+  public boolean isMerged() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFMerged();
   }
 
-  public boolean isVertical()
-  {
+  public boolean isVertical() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFVertical();
   }
 
-  public boolean isBackward()
-  {
+  public boolean isBackward() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFBackward();
   }
 
-    public boolean isRotateFont()
-  {
+  public boolean isRotateFont() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFRotateFont();
   }
 
-  public boolean isVerticallyMerged()
-  {
+  public boolean isVerticallyMerged() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFVertMerge();
   }
 
-  public boolean isFirstVerticallyMerged()
-  {
+  public boolean isFirstVerticallyMerged() {
+    if (_tcd == null) {
+      return false;
+    }
+
     return _tcd.isFVertRestart();
   }
 
-  public byte getVertAlign()
-  {
+  public byte getVertAlign() {
+    if (_tcd == null) {
+      return 0;
+    }
+
     return _tcd.getVertAlign();
   }
 
-  public BorderCode getBrcTop()
-  {
+  public BorderCode getBrcTop() {
+    if (_tcd == null) {
+      return new BorderCode();
+    }
+
     return _tcd.getBrcTop();
   }
 
-  public BorderCode getBrcBottom()
-  {
+  public BorderCode getBrcBottom() {
+    if (_tcd == null) {
+      return new BorderCode();
+    }
+
     return _tcd.getBrcBottom();
   }
 
-  public BorderCode getBrcLeft()
-  {
+  public BorderCode getBrcLeft() {
+    if (_tcd == null) {
+      return new BorderCode();
+    }
+
     return _tcd.getBrcLeft();
   }
 
-  public BorderCode getBrcRight()
-  {
+  public BorderCode getBrcRight() {
+    if (_tcd == null) {
+      return new BorderCode();
+    }
+
     return _tcd.getBrcRight();
   }
 
-  public int getLeftEdge() // twips
-  {
+  // twips
+  public int getLeftEdge() {
     return _leftEdge;
   }
 
-  public int getWidth() // twips
-  {
+  // twips
+  public int getWidth() {
     return _width;
   }
 
-  /** Returns the TableCellDescriptor for this cell.*/
-  public TableCellDescriptor getDescriptor(){
-  	return _tcd;
+  /**
+   * Returns the TableCellDescriptor for this cell.
+   */
+  public TableCellDescriptor getDescriptor() {
+    return _tcd;
   }
-
 }
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
index 0fb5da3..7518139 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
@@ -24,19 +24,23 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.HWPFDocumentCore;
 import org.apache.poi.hwpf.HWPFOldDocument;
 import org.apache.poi.hwpf.HWPFTestDataSamples;
 import org.apache.poi.hwpf.converter.AbstractWordUtils;
+import org.apache.poi.hwpf.converter.WordToHtmlConverter;
 import org.apache.poi.hwpf.converter.WordToTextConverter;
 import org.apache.poi.hwpf.extractor.Word6Extractor;
 import org.apache.poi.hwpf.extractor.WordExtractor;
@@ -50,6 +54,9 @@
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * Test different problems reported in the Apache Bugzilla
@@ -892,4 +899,29 @@
             }
         }
     }
+
+    @Test
+    public void test60217() throws Exception {
+        File file = new File("/tmp/word-doc-with-revised-table.doc");
+        FileInputStream fileInputStream = new FileInputStream(file);
+        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
+        HWPFDocumentCore hwpfDocumentCore = new HWPFDocument(HWPFDocumentCore.verifyAndBuildPOIFS(fileInputStream));
+        wordToHtmlConverter.processDocument(hwpfDocumentCore);
+        System.out.println(document);
+
+        System.out.println(document.getNodeName() + " -> " + document.getNodeValue());
+
+        printNode(document, " ");
+
+        System.out.println("Process Complete");
+    }
+
+    private void printNode(Node rootNode, String spacer) {
+        System.out.println(spacer + rootNode.getNodeName() + " -> " + rootNode.getNodeValue());
+        NodeList nl = rootNode.getChildNodes();
+        for (int i = 0; i < nl.getLength(); i++)
+            printNode(nl.item(i), spacer + "   ");
+    }
+
 }