From: Nick Burch Date: Mon, 13 Dec 2010 05:07:19 +0000 (+0000) Subject: Added XSSF EventModel support for inline strings. Adds unit test for this for the... X-Git-Tag: REL_3_8_BETA1~100 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5ff813195543b3afc84e9bd9436665f5b055fbf4;p=poi.git Added XSSF EventModel support for inline strings. Adds unit test for this for the event model extractor, and another for the usermodel extractor which already supported it git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1045020 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index fe9ac0bd46..946e24ea7e 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Added inline string support to XSSF EventModel 50246 - Properly position GutsRecord when reading HSSF workbooks 48539 - Added implementation for MROUND(), VAR() and VARP() 50446 - Code cleanup and optimizations to keep some IDE quiet diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java index c08f14c5d5..1c6018c13b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java @@ -61,6 +61,8 @@ public class XSSFSheetXMLHandler extends DefaultHandler { private boolean vIsOpen; // Set when F start element is seen private boolean fIsOpen; + // Set when an Inline String "is" is seen + private boolean isIsOpen; // Set when a header/footer element is seen private boolean hfIsOpen; @@ -113,13 +115,33 @@ public class XSSFSheetXMLHandler extends DefaultHandler { this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults); } + private boolean isTextTag(String name) { + if("v".equals(name)) { + // Easy, normal v text tag + return true; + } + if("inlineStr".equals(name)) { + // Easy inline string + return true; + } + if("t".equals(name) && isIsOpen) { + // Inline string ... pair + return true; + } + // It isn't a text tag + return false; + } + public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { - if ("inlineStr".equals(name) || "v".equals(name)) { + if (isTextTag(name)) { vIsOpen = true; // Clear contents cache value.setLength(0); + } else if ("is".equals(name)) { + // Inline string outer tag + isIsOpen = true; } else if ("f".equals(name)) { // Clear contents cache formula.setLength(0); @@ -202,7 +224,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { String thisStr = null; // v => contents of a cell - if ("v".equals(name)) { + if (isTextTag(name)) { vIsOpen = false; // Process the value contents as required, now we have it all @@ -225,7 +247,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { break; case INLINE_STRING: - // TODO: have seen an example of this, so it's untested. + // TODO: Can these ever have formatting on them? XSSFRichTextString rtsi = new XSSFRichTextString(value.toString()); thisStr = rtsi.toString(); break; @@ -259,6 +281,8 @@ public class XSSFSheetXMLHandler extends DefaultHandler { output.cell(cellRef, thisStr); } else if ("f".equals(name)) { fIsOpen = false; + } else if ("is".equals(name)) { + isIsOpen = false; } else if ("row".equals(name)) { output.endRow(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java index c1b0247276..eade64f2fe 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java @@ -114,6 +114,28 @@ public final class TestXSSFEventBasedExcelExtractor extends TestCase { )); } + public void testInlineStrings() throws Exception { + XSSFEventBasedExcelExtractor extractor = getExtractor("InlineStrings.xlsx"); + extractor.setFormulasNotResults(true); + String text = extractor.getText(); + + // Numbers + assertTrue("Unable to find expected word in text\n" + text, text.contains("43")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("22")); + + // Strings + assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text")); + + // Inline Strings + assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("And More")); + + // Formulas + assertTrue("Unable to find expected word in text\n" + text, text.contains("A2")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2")); + } + /** * Test that we return pretty much the same as * ExcelExtractor does, when we're both passed diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java index c389e63fa8..2d14cd535d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java @@ -174,4 +174,26 @@ public final class TestXSSFExcelExtractor extends TestCase { assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc")); assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); } + + public void testInlineStrings() { + XSSFExcelExtractor extractor = getExtractor("InlineStrings.xlsx"); + extractor.setFormulasNotResults(true); + String text = extractor.getText(); + + // Numbers + assertTrue("Unable to find expected word in text\n" + text, text.contains("43")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("22")); + + // Strings + assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text")); + + // Inline Strings + assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("And More")); + + // Formulas + assertTrue("Unable to find expected word in text\n" + text, text.contains("A2")); + assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2")); + } }