Apply patch to fix bug 57495: getTableArray method can not get 0 pos table

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734694 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-03-12 11:45:34 +00:00
parent 06083f85f4
commit 2b28cd97a3
6 changed files with 252 additions and 196 deletions

View File

@ -48,12 +48,21 @@ public class Units {
/** /**
* Converts points to EMUs * Converts points to EMUs
* @param points points * @param points points
* @return emus * @return EMUs
*/ */
public static int toEMU(double points){ public static int toEMU(double points){
return (int)Math.rint(EMU_PER_POINT*points); return (int)Math.rint(EMU_PER_POINT*points);
} }
/**
* Converts pixels to EMUs
* @param pixels pixels
* @return EMUs
*/
public static int pixelToEMU(int pixels) {
return pixels*EMU_PER_PIXEL;
}
/** /**
* Converts EMUs to points * Converts EMUs to points
* @param emu emu * @param emu emu

View File

@ -335,7 +335,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
*/ */
@Override @Override
public XWPFTable getTableArray(int pos) { public XWPFTable getTableArray(int pos) {
if (pos > 0 && pos < tables.size()) { if (pos >= 0 && pos < tables.size()) {
return tables.get(pos); return tables.get(pos);
} }
return null; return null;
@ -349,8 +349,11 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
} }
public XWPFFooter getFooterArray(int pos) { public XWPFFooter getFooterArray(int pos) {
if(pos >=0 && pos < footers.size()) {
return footers.get(pos); return footers.get(pos);
} }
return null;
}
/** /**
* @return the list of headers * @return the list of headers
@ -360,8 +363,11 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
} }
public XWPFHeader getHeaderArray(int pos) { public XWPFHeader getHeaderArray(int pos) {
if(pos >=0 && pos < headers.size()) {
return headers.get(pos); return headers.get(pos);
} }
return null;
}
public String getTblStyle(XWPFTable table) { public String getTblStyle(XWPFTable table) {
return table.getStyleID(); return table.getStyleID();

View File

@ -1,3 +1,4 @@
/* ====================================================================
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -111,7 +112,7 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
* @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
*/ */
public XWPFTable getTableArray(int pos) { public XWPFTable getTableArray(int pos) {
if (pos > 0 && pos < tables.size()) { if (pos >= 0 && pos < tables.size()) {
return tables.get(pos); return tables.get(pos);
} }
return null; return null;
@ -180,9 +181,11 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
* @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int pos) * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int pos)
*/ */
public XWPFParagraph getParagraphArray(int pos) { public XWPFParagraph getParagraphArray(int pos) {
if(pos >=0 && pos < paragraphs.size()) {
return paragraphs.get(pos); return paragraphs.get(pos);
} }
return null;
}
/** /**
* get the TableCell which belongs to the TableCell * get the TableCell which belongs to the TableCell
@ -210,6 +213,9 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
return null; return null;
} }
XWPFTableRow tableRow = table.getRow(row); XWPFTableRow tableRow = table.getRow(row);
if(tableRow == null){
return null;
}
return tableRow.getTableCell(cell); return tableRow.getTableCell(cell);
} }

View File

@ -212,9 +212,11 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* the text of the header or footer. * the text of the header or footer.
*/ */
public XWPFParagraph getParagraphArray(int pos) { public XWPFParagraph getParagraphArray(int pos) {
if(pos >= 0 && pos<paragraphs.size()){
return paragraphs.get(pos); return paragraphs.get(pos);
} }
return null;
}
/** /**
* get a List of all Paragraphs * get a List of all Paragraphs
@ -431,8 +433,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
*/ */
public XWPFTable getTableArray(int pos) { public XWPFTable getTableArray(int pos) {
if (pos >= 0 && pos < tables.size()) {
if (pos > 0 && pos < tables.size()) {
return tables.get(pos); return tables.get(pos);
} }
return null; return null;

View File

@ -343,7 +343,7 @@ public class XWPFTableCell implements IBody, ICell {
* @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int) * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int)
*/ */
public XWPFParagraph getParagraphArray(int pos) { public XWPFParagraph getParagraphArray(int pos) {
if (pos > 0 && pos < paragraphs.size()) { if (pos >= 0 && pos < paragraphs.size()) {
return paragraphs.get(pos); return paragraphs.get(pos);
} }
return null; return null;
@ -381,7 +381,7 @@ public class XWPFTableCell implements IBody, ICell {
* @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
*/ */
public XWPFTable getTableArray(int pos) { public XWPFTable getTableArray(int pos) {
if (pos > 0 && pos < tables.size()) { if(pos >= 0 && pos < tables.size()) {
return tables.get(pos); return tables.get(pos);
} }
return null; return null;

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.util.Units;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange; import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;
@ -55,7 +56,6 @@ public class TestXWPFBugs {
doc.close(); doc.close();
} }
@Test @Test
public void bug57312_NullPointException() throws IOException { public void bug57312_NullPointException() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("57312.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("57312.docx");
@ -82,6 +82,40 @@ public class TestXWPFBugs {
} }
} }
@Test
public void bug57495_getTableArrayInDoc() {
XWPFDocument doc =new XWPFDocument();
//let's create a few tables for the test
for(int i=0;i<3;i++) {
doc.createTable(2, 2);
}
XWPFTable table = doc.getTableArray(0);
assertNotNull(table);
//let's check also that returns the correct table
XWPFTable same = doc.getTables().get(0);
assertEquals(table, same);
}
@Test
public void bug57495_getParagraphArrayInTableCell() {
XWPFDocument doc =new XWPFDocument();
//let's create a table for the test
XWPFTable table = doc.createTable(2, 2);
assertNotNull(table);
XWPFParagraph p = table.getRow(0).getCell(0).getParagraphArray(0);
assertNotNull(p);
//let's check also that returns the correct paragraph
XWPFParagraph same = table.getRow(0).getCell(0).getParagraphs().get(0);
assertEquals(p, same);
}
@Test
public void bug57495_convertPixelsToEMUs() {
int pixels = 100;
int expectedEMU = 952500;
int result = Units.pixelToEMU(pixels);
assertEquals(expectedEMU, result);
}
@Test @Test
public void test56392() throws IOException, OpenXML4JException { public void test56392() throws IOException, OpenXML4JException {