You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2012/10/12 11:32:45 UTC

svn commit: r1397499 - in /poi/trunk/src: documentation/content/xdocs/status.xml ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java

Author: yegor
Date: Fri Oct 12 09:32:45 2012
New Revision: 1397499

URL: http://svn.apache.org/viewvc?rev=1397499&view=rev
Log:
preserve leading / trailing spaces in SXSSF

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1397499&r1=1397498&r2=1397499&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Oct 12 09:32:45 2012
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="fix">52972 - preserve leading / trailing spaces in SXSSF </action>
           <action dev="poi-developers" type="fix">53965 - Fixed XmlValueOutOfRangeExceptio calling getDataValidations for custom validations with XSSFSheet </action>
           <action dev="poi-developers" type="fix">53974 - Avoid NPE when constructing HSSFWorbook on Google App Engine</action>
           <action dev="poi-developers" type="fix">53568 - Fixed null returned by XSSFPicture.getPictureData()</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java?rev=1397499&r1=1397498&r2=1397499&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java Fri Oct 12 09:32:45 2012
@@ -23,7 +23,10 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.FormulaError;
 import org.apache.poi.ss.util.CellReference;
+import org.apache.xmlbeans.XmlCursor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;
 
+import javax.xml.namespace.QName;
 import java.io.*;
 import java.util.Iterator;
 
@@ -180,7 +183,11 @@ public class SheetDataWriter {
             }
             case Cell.CELL_TYPE_STRING: {
                 _out.write(" t=\"inlineStr\">");
-                _out.write("<is><t>");
+                _out.write("<is><t");
+                if(hasLeadingTrailingSpaces(cell.getStringCellValue())) {
+                    _out.write(" xml:space=\"preserve\"");
+                }
+                _out.write(">");
                 outputQuotedString(cell.getStringCellValue());
                 _out.write("</t></is>");
                 break;
@@ -209,6 +216,20 @@ public class SheetDataWriter {
         _out.write("</c>");
     }
 
+
+    /**
+     * @return  whether the string has leading / trailing spaces that
+     *  need to be preserved with the xml:space=\"preserve\" attribute
+     */
+    boolean hasLeadingTrailingSpaces(String str) {
+        if (str != null && str.length() > 0) {
+            char firstChar = str.charAt(0);
+            char lastChar  = str.charAt(str.length() - 1);
+            return Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar) ;
+        }
+        return false;
+    }
+
     //Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java
     protected void outputQuotedString(String s) throws IOException {
         if (s == null || s.length() == 0) {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java?rev=1397499&r1=1397498&r2=1397499&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java Fri Oct 12 09:32:45 2012
@@ -22,7 +22,14 @@ package org.apache.poi.xssf.streaming;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.SXSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.xmlbeans.XmlCursor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
+
+import javax.xml.namespace.QName;
+import java.io.FileOutputStream;
+import java.io.IOException;
 
 /**
  *
@@ -110,4 +117,31 @@ public class TestSXSSFCell extends BaseT
         assertEquals(xCell.getStringCellValue(), sCell.getStringCellValue());
 
     }
+
+    public void testPreserveSpaces() throws IOException {
+        String[] samplesWithSpaces = {
+                " POI",
+                "POI ",
+                " POI ",
+                "\nPOI",
+                "\n\nPOI \n",
+        };
+        for(String str : samplesWithSpaces){
+            Workbook swb = new SXSSFWorkbook();
+            Cell sCell = swb.createSheet().createRow(0).createCell(0);
+            sCell.setCellValue(str);
+            assertEquals(sCell.getStringCellValue(), str);
+
+            // read back as XSSF and check that xml:spaces="preserve" is set
+            XSSFWorkbook xwb = (XSSFWorkbook)SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
+            XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
+
+            CTRst is = xCell.getCTCell().getIs();
+            XmlCursor c = is.newCursor();
+            c.toNextToken();
+            String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
+            c.dispose();
+            assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
+        }
+    }
 }



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