Skip to content

Commit

Permalink
Fix bug 59322: NullPointerException when converting a certain Word do…
Browse files Browse the repository at this point in the history
…cument to HTML

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866185 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
centic9 committed Aug 31, 2019
1 parent 236eb7b commit bd6e0e0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 43 deletions.
121 changes: 78 additions & 43 deletions src/scratchpad/src/org/apache/poi/hwpf/usermodel/TableCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,132 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand All @@ -50,6 +54,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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
Expand Down Expand Up @@ -892,4 +899,29 @@ public void test61490CellCountInTable() throws Exception {
}
}
}

@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 + " ");
}

}

0 comments on commit bd6e0e0

Please sign in to comment.