From 9a755850e9ad663b7c71b0a6171b2b85d6e7b6d0 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 7 May 2015 12:37:43 +0000 Subject: [PATCH] Begin on test for going from xwpf text to style git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678179 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFStyles.java | 11 +++-- .../poi/xwpf/usermodel/TestXWPFStyles.java | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index 824d39d71..9c6319c56 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -43,8 +43,10 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLanguage; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults; /** - * @author Philipp Epp - * + * Holds details of built-in, default and user styles, which + * apply to tables / paragraphs / lists etc. + * Text within one of those with custom stylings has the style + * information stored in the {@link XWPFRun} */ public class XWPFStyles extends POIXMLDocumentPart{ @@ -83,8 +85,6 @@ public class XWPFStyles extends POIXMLDocumentPart{ } catch (XmlException e) { throw new POIXMLException("Unable to read styles", e); } - - } @Override @@ -154,6 +154,9 @@ public class XWPFStyles extends POIXMLDocumentPart{ } return null; } + public int getNumberOfStyles() { + return listStyle.size(); + } /** * get the styles which are related to the parameter style and their relatives diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index c29d50208..3e7d54b90 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -138,4 +138,52 @@ public class TestXWPFStyles extends TestCase { styles = docIn.getStyles(); assertTrue(styles.styleExist(strStyleId)); } + + public void testEasyAccessToStyles() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); + XWPFStyles styles = doc.getStyles(); + assertNotNull(styles); + + // Has 3 paragraphs on page one, a break, and 3 on page 2 + assertEquals(7, doc.getParagraphs().size()); + + // Check the first three have no run styles, just default paragraph style + for (int i=0; i<3; i++) { + XWPFParagraph p = doc.getParagraphs().get(i); + assertEquals(null, p.getStyle()); + assertEquals(null, p.getStyleID()); + assertEquals(1, p.getRuns().size()); + + XWPFRun r = p.getRuns().get(0); + assertEquals(null, r.getColor()); + assertEquals(null, r.getFontFamily()); + assertEquals(null, r.getFontName()); + assertEquals(-1, r.getFontSize()); + } + + // On page two, has explicit styles, but on runs not on + // the paragraph itself + for (int i=4; i<7; i++) { + XWPFParagraph p = doc.getParagraphs().get(i); + assertEquals(null, p.getStyle()); + assertEquals(null, p.getStyleID()); + assertEquals(1, p.getRuns().size()); + + XWPFRun r = p.getRuns().get(0); + assertEquals("Arial Black", r.getFontFamily()); + assertEquals("Arial Black", r.getFontName()); + assertEquals(16, r.getFontSize()); + assertEquals("548DD4", r.getColor()); + } + + // Check the document styles + // Should have a style defined for each type + assertEquals(4, styles.getNumberOfStyles()); + assertNotNull(styles.getStyle("Normal")); + assertNotNull(styles.getStyle("DefaultParagraphFont")); + assertNotNull(styles.getStyle("TableNormal")); + assertNotNull(styles.getStyle("NoList")); + + // TODO Check latent and default + } }