From 120608b1f92343159a06e90de9cf14c7aad484ef Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 7 May 2015 13:25:47 +0000 Subject: [PATCH] Start exposing the default paragraph and run styles from XWPFStyles git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678193 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/XWPFDefaultParagraphStyle.java | 4 + .../xwpf/usermodel/XWPFDefaultRunStyle.java | 4 + .../apache/poi/xwpf/usermodel/XWPFStyles.java | 77 +++++++++++++------ .../poi/xwpf/usermodel/TestXWPFStyles.java | 6 +- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java index 3d29f7942..9d16e26da 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java @@ -30,6 +30,10 @@ public class XWPFDefaultParagraphStyle { this.ppr = ppr; } + protected CTPPr getPPr() { + return ppr; + } + public int getSpacingAfter() { if (ppr.isSetSpacing()) return ppr.getSpacing().getAfter().intValue(); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java index 38de75e34..210a95163 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java @@ -30,6 +30,10 @@ public class XWPFDefaultRunStyle { this.rpr = rpr; } + protected CTRPr getRPr() { + return rpr; + } + public int getFontSize() { if (rpr.isSetSz()) return rpr.getSz().getVal().intValue() / 2; 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 06405a63f..cebc63cad 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -34,6 +34,7 @@ import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrDefault; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument; @@ -49,9 +50,12 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults; * information stored in the {@link XWPFRun} */ public class XWPFStyles extends POIXMLDocumentPart{ - private List listStyle = new ArrayList(); private CTStyles ctStyles; - XWPFLatentStyles latentStyles; + private List listStyle = new ArrayList(); + + private XWPFLatentStyles latentStyles; + private XWPFDefaultRunStyle defaultRunStyle; + private XWPFDefaultParagraphStyle defaultParaStyle; /** * Construct XWPFStyles from a package part @@ -104,7 +108,23 @@ public class XWPFStyles extends POIXMLDocumentPart{ } protected void ensureDocDefaults() { - // TODO Refactor from elsewhere + if (! ctStyles.isSetDocDefaults()) { + ctStyles.addNewDocDefaults(); + } + + CTDocDefaults docDefaults = ctStyles.getDocDefaults(); + if (! docDefaults.isSetPPrDefault()) + docDefaults.addNewPPrDefault(); + if (! docDefaults.isSetRPrDefault()) + docDefaults.addNewRPrDefault(); + + CTPPrDefault pprd = docDefaults.getPPrDefault(); + CTRPrDefault rprd = docDefaults.getRPrDefault(); + if (!pprd.isSetPPr()) pprd.addNewPPr(); + if (!rprd.isSetRPr()) rprd.addNewRPr(); + + defaultRunStyle = new XWPFDefaultRunStyle(rprd.getRPr()); + defaultParaStyle = new XWPFDefaultParagraphStyle(pprd.getPPr()); } /** @@ -119,6 +139,17 @@ public class XWPFStyles extends POIXMLDocumentPart{ for(CTStyle style : ctStyles.getStyleArray()) { listStyle.add(new XWPFStyle(style, this)); } + if (ctStyles.isSetDocDefaults()) { + CTDocDefaults docDefaults = ctStyles.getDocDefaults(); + if (docDefaults.isSetRPrDefault() && docDefaults.getRPrDefault().isSetRPr()) { + defaultRunStyle = new XWPFDefaultRunStyle( + docDefaults.getRPrDefault().getRPr()); + } + if (docDefaults.isSetPPrDefault() && docDefaults.getPPrDefault().isSetPPr()) { + defaultParaStyle = new XWPFDefaultParagraphStyle( + docDefaults.getPPrDefault().getPPr()); + } + } } /** @@ -205,33 +236,20 @@ public class XWPFStyles extends POIXMLDocumentPart{ * @param strSpellingLanguage */ public void setSpellingLanguage(String strSpellingLanguage) { - CTDocDefaults docDefaults = null; - CTRPr runProps = null; + ensureDocDefaults(); + CTLanguage lang = null; - - // Just making sure we use the members that have already been defined - if(ctStyles.isSetDocDefaults()) { - docDefaults = ctStyles.getDocDefaults(); - if(docDefaults.isSetRPrDefault()) { - CTRPrDefault RPrDefault = docDefaults.getRPrDefault(); - if(RPrDefault.isSetRPr()) { - runProps = RPrDefault.getRPr(); - if(runProps.isSetLang()) - lang = runProps.getLang(); - } - } + if (defaultRunStyle.getRPr().isSetLang()) { + lang = defaultRunStyle.getRPr().getLang(); + } else { + lang = defaultRunStyle.getRPr().addNewLang(); } - if(docDefaults == null) - docDefaults = ctStyles.addNewDocDefaults(); - if(runProps == null) - runProps = docDefaults.addNewRPrDefault().addNewRPr(); - if(lang == null) - lang = runProps.addNewLang(); - lang.setVal(strSpellingLanguage); lang.setBidi(strSpellingLanguage); } + + // TODO Refactor the others like this /** * Sets the default East Asia spelling language on ctStyles DocDefaults parameter @@ -305,11 +323,20 @@ public class XWPFStyles extends POIXMLDocumentPart{ return null; } + /** + * Get the default style which applies text runs in the document + */ + public XWPFDefaultRunStyle getDefaultRunStyle() { + ensureDocDefaults(); + return defaultRunStyle; + } + /** * Get the default paragraph style which applies to the document */ public XWPFDefaultParagraphStyle getDefaultParagraphStyle() { - return null; // TODO + ensureDocDefaults(); + return defaultParaStyle; } /** 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 578e73f81..c297bc4c3 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -188,6 +188,10 @@ public class TestXWPFStyles extends TestCase { assertEquals(137, styles.getLatentStyles().getNumberOfStyles()); // Check the default styles - // TODO + assertNotNull(styles.getDefaultRunStyle()); + assertNotNull(styles.getDefaultParagraphStyle()); + + assertEquals(11, styles.getDefaultRunStyle().getFontSize()); + assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter()); } }