git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1397499 13f79535-47bb-0310-9956-ffa450edef68tags/3.10-beta1
@@ -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> |
@@ -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) { |
@@ -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); | |||
} | |||
} | |||
} |