You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2016/06/02 20:09:44 UTC

svn commit: r1746625 - in /poi/trunk: 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

Author: centic
Date: Thu Jun  2 20:09:44 2016
New Revision: 1746625

URL: http://svn.apache.org/viewvc?rev=1746625&view=rev
Log:
Avoid NPE in XWPFTableCell, taken from https://github.com/prasad-babu/poi/tree/WORKING_BRANCH

Added:
    poi/trunk/test-data/document/TestTableCellAlign.docx
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java?rev=1746625&r1=1746624&r2=1746625&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java Thu Jun  2 20:09:44 2016
@@ -163,7 +163,7 @@ public class XWPFTableCell implements IB
     /**
      * removes a paragraph of this tablecell
      *
-     * @param pos
+     * @param pos The position in the list of paragraphs, 0-based
      */
     public void removeParagraph(int pos) {
         paragraphs.remove(pos);
@@ -234,6 +234,11 @@ public class XWPFTableCell implements IB
         CTTcPr tcpr = ctTc.getTcPr();
         if (tcpr != null) {
             CTVerticalJc va = tcpr.getVAlign();
+            if(va != null) {
+                vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
+            } else {
+                vAlign = XWPFVertAlign.TOP;
+            }
             if (va != null && va.getVal() != null) {
                 vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
             }
@@ -255,7 +260,7 @@ public class XWPFTableCell implements IB
     /**
      * add a new paragraph at position of the cursor
      *
-     * @param cursor
+     * @param cursor The XmlCursor structure created with XmlBeans
      * @return the inserted paragraph
      */
     public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
@@ -417,7 +422,7 @@ public class XWPFTableCell implements IB
     }
 
     public String getText() {
-        StringBuffer text = new StringBuffer();
+        StringBuilder text = new StringBuilder();
         for (XWPFParagraph p : paragraphs) {
             text.append(p.getText());
         }
@@ -437,7 +442,7 @@ public class XWPFTableCell implements IB
 
         StringBuffer text = new StringBuffer();
         for (int i = 0; i < bodyElements.size(); i++) {
-            boolean isLast = (i == bodyElements.size() - 1) ? true : false;
+            boolean isLast = (i == bodyElements.size() - 1);
             appendBodyElementText(text, bodyElements.get(i), isLast);
         }
 
@@ -447,7 +452,7 @@ public class XWPFTableCell implements IB
     private void appendBodyElementText(StringBuffer text, IBodyElement e, boolean isLast) {
         if (e instanceof XWPFParagraph) {
             text.append(((XWPFParagraph) e).getText());
-            if (isLast == false) {
+            if (!isLast) {
                 text.append('\t');
             }
         } else if (e instanceof XWPFTable) {
@@ -456,18 +461,18 @@ public class XWPFTableCell implements IB
                 for (XWPFTableCell cell : row.getTableCells()) {
                     List<IBodyElement> localBodyElements = cell.getBodyElements();
                     for (int i = 0; i < localBodyElements.size(); i++) {
-                        boolean localIsLast = (i == localBodyElements.size() - 1) ? true : false;
+                        boolean localIsLast = (i == localBodyElements.size() - 1);
                         appendBodyElementText(text, localBodyElements.get(i), localIsLast);
                     }
                 }
             }
 
-            if (isLast == false) {
+            if (!isLast) {
                 text.append('\n');
             }
         } else if (e instanceof XWPFSDT) {
             text.append(((XWPFSDT) e).getContent().getText());
-            if (isLast == false) {
+            if (!isLast) {
                 text.append('\t');
             }
         }
@@ -507,7 +512,7 @@ public class XWPFTableCell implements IB
     }
 
     // Create a map from this XWPF-level enum to the STVerticalJc.Enum values
-    public static enum XWPFVertAlign {
+    public enum XWPFVertAlign {
         TOP, CENTER, BOTH, BOTTOM
     }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java?rev=1746625&r1=1746624&r2=1746625&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java Thu Jun  2 20:09:44 2016
@@ -19,23 +19,12 @@
 
 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 T
         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());
+				}
+			}
+		}			
+	}
 }

Added: poi/trunk/test-data/document/TestTableCellAlign.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/TestTableCellAlign.docx?rev=1746625&view=auto
==============================================================================
Binary files poi/trunk/test-data/document/TestTableCellAlign.docx (added) and poi/trunk/test-data/document/TestTableCellAlign.docx Thu Jun  2 20:09:44 2016 differ



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org