From c4290ae2be3d2ab311683a58700ebcb0a1640e84 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 25 Jul 2018 17:21:58 +0000 Subject: [PATCH] [github-117] Add getStyleWithName() to XWPFStyles. This closes #117 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1836649 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFParagraph.java | 9 ++-- .../apache/poi/xwpf/usermodel/XWPFRun.java | 44 +++++++------------ .../apache/poi/xwpf/usermodel/XWPFStyles.java | 17 +++++++ .../poi/xwpf/usermodel/TestXWPFRun.java | 14 ++++++ .../poi/xwpf/usermodel/TestXWPFStyles.java | 12 +++++ 5 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 5fbc7a522..5080cec9f 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -1333,15 +1333,14 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para } /** - * This method provides a style to the paragraph - * This is useful when, e.g. an Heading style has to be assigned + * Set the style ID for the paragraph * - * @param newStyle + * @param styleId ID (not name) of the style to set for the paragraph, e.g. "Heading1" (not "Heading 1"). */ - public void setStyle(String newStyle) { + public void setStyle(String styleId) { CTPPr pr = getCTPPr(); CTString style = pr.getPStyle() != null ? pr.getPStyle() : pr.addNewPStyle(); - style.setVal(newStyle); + style.setVal(styleId); } /** diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index cf9d2c67a..10261c5cd 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -56,33 +56,7 @@ import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture; import org.openxmlformats.schemas.drawingml.x2006.picture.CTPictureNonVisual; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFFCheckBox; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRuby; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRubyContent; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSignedHpsMeasure; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSignedTwipsMeasure; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.InputSource; @@ -1121,6 +1095,22 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public List getEmbeddedPictures() { return pictures; } + + /** + * Set the style ID for the run. + * + * @param styleId ID (not name) of the style to set for the run, e.g. "BoldItalic" (not "Bold Italic"). + */ + public void setStyle(String styleId) { + CTRPr pr = getCTR().getRPr(); + if (null == pr) { + pr = getCTR().addNewRPr(); + } + CTString style = pr.getRStyle() != null ? pr.getRStyle() : pr.addNewRStyle(); + style.setVal(styleId); + } + + /** * Returns the string version of the text and the phonetic string 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 0e6dd268a..fcc484091 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -323,4 +323,21 @@ public class XWPFStyles extends POIXMLDocumentPart { public XWPFLatentStyles getLatentStyles() { return latentStyles; } + + /** + * Get the style with the specified name, if any. + * + * @param styleName The name of the style to get, e.g., "Heading 1" + * @return {@link XWPFStyle} with the specified name, or null if not found. + */ + public XWPFStyle getStyleWithName(String styleName) { + XWPFStyle style = null; + for (XWPFStyle cand : listStyle) { + if (styleName.equals(cand.getName())) { + style = cand; + break; + } + } + return style; + } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index 026cdac58..d3ad99a81 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -673,4 +673,18 @@ public class TestXWPFRun { document.close(); } + + @Test + public void testSetStyleId() throws IOException { + XWPFDocument document = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); + final XWPFRun run = document.createParagraph().createRun(); + + String styleId = "bolditalic"; + run.setStyle(styleId); + String candStyleId = run.getCTR().getRPr().getRStyle().getVal(); + assertNotNull("Expected to find a run style ID", candStyleId); + assertEquals(styleId, candStyleId); + + } + } 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 12dd5db2d..8ac9c083d 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -219,4 +219,16 @@ public final class TestXWPFStyles { doc.close(); } + + @Test + public void testGetStyleByName() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); + XWPFStyles styles = doc.getStyles(); + assertNotNull(styles); + + String styleName = "Normal Table"; + XWPFStyle style = styles.getStyleWithName(styleName); + assertNotNull("Expected to find style \"" + styleName + "\"", style); + assertEquals(styleName, style.getName()); + } }