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
This commit is contained in:
Nick Burch 2010-09-16 16:01:12 +00:00
parent d791eb68a9
commit 78e6e00e2d
3 changed files with 90 additions and 1 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-beta3" date="2010-??-??">
<action dev="poi-developers" type="fix">49941 - Correctly handle space preservation of XSSFRichTextRuns when applying fonts to parts of the string</action>
<action dev="poi-developers" type="fix">Correct XWPFRun detection of bold/italic in a paragraph with multiple runs of different styles</action>
<action dev="poi-developers" type="add">Link XWPFPicture to XWPFRun, so that embedded pictures can be access from where they live in the text stream</action>
<action dev="poi-developers" type="fix">Improve handling of Hyperlinks inside XWPFParagraph objects through XWPFHyperlinkRun</action>

View File

@ -193,6 +193,7 @@ public class XSSFRichTextString implements RichTextString {
c.setT(txt);
runs.add(c);
pos += txt.length();
preserveSpaces(c.xgetT());
}
}

View File

@ -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