<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>
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
}\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
_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
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;
/**
*
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);
+ }
+ }
}