From 78e6e00e2d97af7b9df5078566c76f08115278e0 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 16 Sep 2010 16:01:12 +0000 Subject: [PATCH] Fix bug #49941 - Correctly handle space preservation of XSSFRichTextRuns when applying fonts to parts of the string git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@997811 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../xssf/usermodel/XSSFRichTextString.java | 1 + .../poi/xssf/usermodel/TestXSSFBugs.java | 89 ++++++++++++++++++- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8d05120a9..1332578ff 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49941 - Correctly handle space preservation of XSSFRichTextRuns when applying fonts to parts of the string Correct XWPFRun detection of bold/italic in a paragraph with multiple runs of different styles Link XWPFPicture to XWPFRun, so that embedded pictures can be access from where they live in the text stream Improve handling of Hyperlinks inside XWPFParagraph objects through XWPFHyperlinkRun diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 736a15b3a..c406a9b9d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -193,6 +193,7 @@ public class XSSFRichTextString implements RichTextString { c.setT(txt); runs.add(c); pos += txt.length(); + preserveSpaces(c.xgetT()); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 9c451034c..6e9583826 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -26,7 +26,16 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagingURIHelper; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.FormulaError; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; @@ -431,6 +440,84 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); } + /** + * Creating a rich string of "hello world" and applying + * a font to characters 1-5 means we have two strings, + * "hello" and " world". As such, we need to apply + * preserve spaces to the 2nd bit, lest we end up + * with something like "helloworld" ! + */ + public void test49941() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet s = wb.createSheet(); + XSSFRow r = s.createRow(0); + XSSFCell c = r.createCell(0); + + // First without fonts + c.setCellValue( + new XSSFRichTextString(" with spaces ") + ); + assertEquals(" with spaces ", c.getRichStringCellValue().toString()); + assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray()); + assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT()); + // Should have the preserve set + assertEquals( + 1, + c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength() + ); + assertEquals( + "preserve", + c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue() + ); + + // Save and check + wb = XSSFTestDataSamples.writeOutAndReadBack(wb); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell(0); + assertEquals(" with spaces ", c.getRichStringCellValue().toString()); + assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray()); + assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT()); + + // Change the string + c.setCellValue( + new XSSFRichTextString("hello world") + ); + assertEquals("hello world", c.getRichStringCellValue().toString()); + // Won't have preserve + assertEquals( + 0, + c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength() + ); + + // Apply a font + XSSFFont f = wb.createFont(); + f.setBold(true); + c.getRichStringCellValue().applyFont(0, 5, f); + assertEquals("hello world", c.getRichStringCellValue().toString()); + // Does need preserving on the 2nd part + assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray()); + assertEquals( + 0, + c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength() + ); + assertEquals( + 1, + c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength() + ); + assertEquals( + "preserve", + c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue() + ); + + // Save and check + wb = XSSFTestDataSamples.writeOutAndReadBack(wb); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell(0); + assertEquals("hello world", c.getRichStringCellValue().toString()); + } + /** * Repeatedly writing the same file which has styles * TODO Currently failing