]> source.dussan.org Git - poi.git/commitdiff
preserve leading / trailing spaces in SXSSF
authorYegor Kozlov <yegor@apache.org>
Fri, 12 Oct 2012 09:32:45 +0000 (09:32 +0000)
committerYegor Kozlov <yegor@apache.org>
Fri, 12 Oct 2012 09:32:45 +0000 (09:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1397499 13f79535-47bb-0310-9956-ffa450edef68

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

index 928e9c3172d9fae7a25fe711e5e91ced6bf6179b..a94ba77bfaca45febe4473414d25ef2608c13df8 100644 (file)
@@ -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>
index 20a1d317bd79e606ccd7e894755a4a77f896cf27..649d0b72fca1d3db5d6938f7db9f473954a1858a 100644 (file)
@@ -23,7 +23,10 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;\r
 import org.apache.poi.ss.usermodel.FormulaError;\r
 import org.apache.poi.ss.util.CellReference;\r
+import org.apache.xmlbeans.XmlCursor;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;\r
 \r
+import javax.xml.namespace.QName;\r
 import java.io.*;\r
 import java.util.Iterator;\r
 \r
@@ -180,7 +183,11 @@ public class SheetDataWriter {
             }\r
             case Cell.CELL_TYPE_STRING: {\r
                 _out.write(" t=\"inlineStr\">");\r
-                _out.write("<is><t>");\r
+                _out.write("<is><t");\r
+                if(hasLeadingTrailingSpaces(cell.getStringCellValue())) {\r
+                    _out.write(" xml:space=\"preserve\"");\r
+                }\r
+                _out.write(">");\r
                 outputQuotedString(cell.getStringCellValue());\r
                 _out.write("</t></is>");\r
                 break;\r
@@ -209,6 +216,20 @@ public class SheetDataWriter {
         _out.write("</c>");\r
     }\r
 \r
+\r
+    /**\r
+     * @return  whether the string has leading / trailing spaces that\r
+     *  need to be preserved with the xml:space=\"preserve\" attribute\r
+     */\r
+    boolean hasLeadingTrailingSpaces(String str) {\r
+        if (str != null && str.length() > 0) {\r
+            char firstChar = str.charAt(0);\r
+            char lastChar  = str.charAt(str.length() - 1);\r
+            return Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar) ;\r
+        }\r
+        return false;\r
+    }\r
+\r
     //Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java\r
     protected void outputQuotedString(String s) throws IOException {\r
         if (s == null || s.length() == 0) {\r
index a0668754c5bab9d5c23a60a1acc3e4d17ca1e7fd..fb45b73bf02ffbac8b6209ce4a38de5b23264e54 100644 (file)
@@ -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 BaseTestCell {
         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);
+        }
+    }
 }