From 96959c79f822f4543d55bd3fb346f0e3af0f1d7f Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Fri, 12 Oct 2012 09:32:45 +0000 Subject: [PATCH] preserve leading / trailing spaces in SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1397499 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/xssf/streaming/SheetDataWriter.java | 23 ++++++++++++- .../poi/xssf/streaming/TestSXSSFCell.java | 34 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 928e9c317..a94ba77bf 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52972 - preserve leading / trailing spaces in SXSSF 53965 - Fixed XmlValueOutOfRangeExceptio calling getDataValidations for custom validations with XSSFSheet 53974 - Avoid NPE when constructing HSSFWorbook on Google App Engine 53568 - Fixed null returned by XSSFPicture.getPictureData() diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java index 20a1d317b..649d0b72f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java @@ -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(""); + _out.write(""); outputQuotedString(cell.getStringCellValue()); _out.write(""); break; @@ -209,6 +216,20 @@ public class SheetDataWriter { _out.write(""); } + + /** + * @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) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java index a0668754c..fb45b73bf 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java @@ -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); + } + } }