]> source.dussan.org Git - poi.git/commitdiff
Avoid NPE in XWPFTableCell, taken from https://github.com/prasad-babu/poi/tree/WORKIN...
authorDominik Stadler <centic@apache.org>
Thu, 2 Jun 2016 20:09:44 +0000 (20:09 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 2 Jun 2016 20:09:44 +0000 (20:09 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1746625 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
test-data/document/TestTableCellAlign.docx [new file with mode: 0644]

index 4f1a77cfb70c0b729804c85032a474328377e98b..f070b095e7e3ea93867febc6482a48675d93385d 100644 (file)
@@ -163,7 +163,7 @@ public class XWPFTableCell implements IBody, ICell {
     /**\r
      * removes a paragraph of this tablecell\r
      *\r
-     * @param pos\r
+     * @param pos The position in the list of paragraphs, 0-based\r
      */\r
     public void removeParagraph(int pos) {\r
         paragraphs.remove(pos);\r
@@ -234,6 +234,11 @@ public class XWPFTableCell implements IBody, ICell {
         CTTcPr tcpr = ctTc.getTcPr();\r
         if (tcpr != null) {\r
             CTVerticalJc va = tcpr.getVAlign();\r
+            if(va != null) {\r
+                vAlign = stVertAlignTypeMap.get(va.getVal().intValue());\r
+            } else {\r
+                vAlign = XWPFVertAlign.TOP;\r
+            }\r
             if (va != null && va.getVal() != null) {\r
                 vAlign = stVertAlignTypeMap.get(va.getVal().intValue());\r
             }\r
@@ -255,7 +260,7 @@ public class XWPFTableCell implements IBody, ICell {
     /**\r
      * add a new paragraph at position of the cursor\r
      *\r
-     * @param cursor\r
+     * @param cursor The XmlCursor structure created with XmlBeans\r
      * @return the inserted paragraph\r
      */\r
     public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {\r
@@ -417,7 +422,7 @@ public class XWPFTableCell implements IBody, ICell {
     }\r
 \r
     public String getText() {\r
-        StringBuffer text = new StringBuffer();\r
+        StringBuilder text = new StringBuilder();\r
         for (XWPFParagraph p : paragraphs) {\r
             text.append(p.getText());\r
         }\r
@@ -437,7 +442,7 @@ public class XWPFTableCell implements IBody, ICell {
 \r
         StringBuffer text = new StringBuffer();\r
         for (int i = 0; i < bodyElements.size(); i++) {\r
-            boolean isLast = (i == bodyElements.size() - 1) ? true : false;\r
+            boolean isLast = (i == bodyElements.size() - 1);\r
             appendBodyElementText(text, bodyElements.get(i), isLast);\r
         }\r
 \r
@@ -447,7 +452,7 @@ public class XWPFTableCell implements IBody, ICell {
     private void appendBodyElementText(StringBuffer text, IBodyElement e, boolean isLast) {\r
         if (e instanceof XWPFParagraph) {\r
             text.append(((XWPFParagraph) e).getText());\r
-            if (isLast == false) {\r
+            if (!isLast) {\r
                 text.append('\t');\r
             }\r
         } else if (e instanceof XWPFTable) {\r
@@ -456,18 +461,18 @@ public class XWPFTableCell implements IBody, ICell {
                 for (XWPFTableCell cell : row.getTableCells()) {\r
                     List<IBodyElement> localBodyElements = cell.getBodyElements();\r
                     for (int i = 0; i < localBodyElements.size(); i++) {\r
-                        boolean localIsLast = (i == localBodyElements.size() - 1) ? true : false;\r
+                        boolean localIsLast = (i == localBodyElements.size() - 1);\r
                         appendBodyElementText(text, localBodyElements.get(i), localIsLast);\r
                     }\r
                 }\r
             }\r
 \r
-            if (isLast == false) {\r
+            if (!isLast) {\r
                 text.append('\n');\r
             }\r
         } else if (e instanceof XWPFSDT) {\r
             text.append(((XWPFSDT) e).getContent().getText());\r
-            if (isLast == false) {\r
+            if (!isLast) {\r
                 text.append('\t');\r
             }\r
         }\r
@@ -507,7 +512,7 @@ public class XWPFTableCell implements IBody, ICell {
     }\r
 \r
     // Create a map from this XWPF-level enum to the STVerticalJc.Enum values\r
-    public static enum XWPFVertAlign {\r
+    public enum XWPFVertAlign {\r
         TOP, CENTER, BOTH, BOTTOM\r
     }\r
 }\r
index ab2fad84b38574e26c8a54836bfb2aea348a3050..00e34e241e03ff7fbcc7db51c4d8f526c86984d5 100644 (file)
 
 package org.apache.poi.xwpf.usermodel;
 
-import java.util.List;
-
 import junit.framework.TestCase;
-
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
+
+import java.util.List;
 
 public class TestXWPFTableCell extends TestCase {
     @Override
@@ -117,9 +106,23 @@ public class TestXWPFTableCell extends TestCase {
         List<XWPFTableRow> tableRows = table.getRows();
         assertEquals(2, tableRows.size());
 
-        assertNull(tableRows.get(0).getCell(0).getVerticalAlignment());
+        assertEquals(XWPFVertAlign.TOP, tableRows.get(0).getCell(0).getVerticalAlignment());
         assertEquals(XWPFVertAlign.BOTTOM, tableRows.get(0).getCell(1).getVerticalAlignment());
         assertEquals(XWPFVertAlign.CENTER, tableRows.get(1).getCell(0).getVerticalAlignment());
-        assertNull(tableRows.get(1).getCell(1).getVerticalAlignment());
+        assertEquals(XWPFVertAlign.TOP, tableRows.get(1).getCell(1).getVerticalAlignment());
     }
+       
+       public void testCellVerticalAlign2() throws Exception{
+               XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("TestTableCellAlign.docx");
+               List<XWPFTable> tables = docx.getTables();
+        for (XWPFTable table : tables) {
+            List<XWPFTableRow> tableRows = table.getRows();
+            for (XWPFTableRow tableRow : tableRows) {
+                List<XWPFTableCell> tableCells = tableRow.getTableCells();
+                for (XWPFTableCell tableCell : tableCells) {
+                                       assertNotNull(tableCell.getVerticalAlignment());
+                               }
+                       }
+               }                       
+       }
 }
diff --git a/test-data/document/TestTableCellAlign.docx b/test-data/document/TestTableCellAlign.docx
new file mode 100644 (file)
index 0000000..cf40dd2
Binary files /dev/null and b/test-data/document/TestTableCellAlign.docx differ