From 5e33bd4b29ea4ca3071e7f486589e01e341cc179 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 1 Jun 2019 01:09:40 +0000 Subject: [PATCH] [github-151] XWPFRun: allow style retrieval. This closes #151 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1860464 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFRun.java | 19 +++++++++++++++++++ .../poi/xwpf/usermodel/TestXWPFRun.java | 2 ++ 2 files changed, 21 insertions(+) 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 04b4bea38f..bfeb12d725 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -1213,6 +1213,25 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { style.setVal(styleId); } + /** + * Return this run's style ID. If this run has no style (no run properties or properties without a style), + * an empty string is returned. + * + * @since 4.1.1 + */ + public String getStyle() { + CTRPr pr = getCTR().getRPr(); + if (pr == null) { + return ""; + } + + CTString style = pr.getRStyle(); + if (style == null) { + return ""; + } + + return style.getVal(); + } /** 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 492aad3106..49f03ef79f 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -783,6 +783,8 @@ public class TestXWPFRun { assertNotNull("Expected to find a run style ID", candStyleId); assertEquals(styleId, candStyleId); + assertEquals(styleId, run.getStyle()); + document.close(); }