mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
Support all possible alignment values for docx tables. (#848)
Besides the start/center/end values specified in the OOXML standard, Word also uses "left" and "right" as values. We need to support this to prevent POI code from crashing with such documents. Fixes: https://bz.apache.org/bugzilla/show_bug.cgi?id=69744
This commit is contained in:
parent
f3bf31ac5d
commit
cceccc4bae
@ -26,10 +26,12 @@ import java.util.Map;
|
||||
* Sets alignment values allowed for Tables and Table Rows
|
||||
*/
|
||||
public enum TableRowAlign {
|
||||
|
||||
LEFT(STJcTable.INT_START),
|
||||
|
||||
LEFT(STJcTable.INT_LEFT),
|
||||
START(STJcTable.INT_START),
|
||||
CENTER(STJcTable.INT_CENTER),
|
||||
RIGHT(STJcTable.INT_END);
|
||||
RIGHT(STJcTable.INT_RIGHT),
|
||||
END(STJcTable.INT_END);
|
||||
|
||||
private static final Map<Integer, TableRowAlign> imap;
|
||||
|
||||
|
||||
@ -44,6 +44,7 @@ import org.apache.xmlbeans.XmlCursor;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJcTable;
|
||||
|
||||
class TestXWPFBugs {
|
||||
private static final POIDataSamples samples = POIDataSamples.getDocumentInstance();
|
||||
@ -248,4 +249,29 @@ class TestXWPFBugs {
|
||||
assertEquals(ParagraphAlignment.LEFT, leftParagraph.getAlignment()); // LEFT is the real alignment value.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTableRightAlign() throws Exception {
|
||||
// Document contains all possible values for table alignment, including null.
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table-alignment.docx")) {
|
||||
XWPFTable tbl0 = doc.getTableArray(0);
|
||||
assertNull(tbl0.getTableAlignment());
|
||||
assertFalse(tbl0.getCTTbl().getTblPr().isSetJc());
|
||||
XWPFTable tbl1 = doc.getTableArray(1);
|
||||
assertEquals(TableRowAlign.LEFT, tbl1.getTableAlignment());
|
||||
assertEquals(STJcTable.LEFT, tbl1.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
|
||||
XWPFTable tbl2 = doc.getTableArray(2);
|
||||
assertEquals(TableRowAlign.START, tbl2.getTableAlignment());
|
||||
assertEquals(STJcTable.START, tbl2.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
|
||||
XWPFTable tbl3 = doc.getTableArray(3);
|
||||
assertEquals(TableRowAlign.CENTER, tbl3.getTableAlignment());
|
||||
assertEquals(STJcTable.CENTER, tbl3.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
|
||||
XWPFTable tbl4 = doc.getTableArray(4);
|
||||
assertEquals(TableRowAlign.RIGHT, tbl4.getTableAlignment());
|
||||
assertEquals(STJcTable.RIGHT, tbl4.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
|
||||
XWPFTable tbl5 = doc.getTableArray(5);
|
||||
assertEquals(TableRowAlign.END, tbl5.getTableAlignment());
|
||||
assertEquals(STJcTable.END, tbl5.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -571,14 +571,20 @@ class TestXWPFTable {
|
||||
void testSetGetTableAlignment() throws IOException {
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
XWPFTable tbl = doc.createTable(1, 1);
|
||||
tbl.setTableAlignment(TableRowAlign.START);
|
||||
assertEquals(TableRowAlign.START, tbl.getTableAlignment());
|
||||
assertEquals(STJcTable.INT_START, tbl.getTableAlignment().getValue());
|
||||
tbl.setTableAlignment(TableRowAlign.LEFT);
|
||||
assertEquals(TableRowAlign.LEFT, tbl.getTableAlignment());
|
||||
assertEquals(STJcTable.INT_START, tbl.getTableAlignment().getValue());
|
||||
assertEquals(STJcTable.INT_LEFT, tbl.getTableAlignment().getValue());
|
||||
tbl.setTableAlignment(TableRowAlign.CENTER);
|
||||
assertEquals(TableRowAlign.CENTER, tbl.getTableAlignment());
|
||||
assertEquals(STJcTable.INT_CENTER, tbl.getTableAlignment().getValue());
|
||||
tbl.setTableAlignment(TableRowAlign.RIGHT);
|
||||
assertEquals(TableRowAlign.RIGHT, tbl.getTableAlignment());
|
||||
assertEquals(STJcTable.INT_RIGHT, tbl.getTableAlignment().getValue());
|
||||
tbl.setTableAlignment(TableRowAlign.END);
|
||||
assertEquals(TableRowAlign.END, tbl.getTableAlignment());
|
||||
assertEquals(STJcTable.INT_END, tbl.getTableAlignment().getValue());
|
||||
tbl.removeTableAlignment();
|
||||
assertNull(tbl.getTableAlignment());
|
||||
|
||||
BIN
test-data/document/table-alignment.docx
Normal file
BIN
test-data/document/table-alignment.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user