From 1a25ff4dce37137a7fd1c42af4458046beca0116 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 18 Dec 2018 08:12:42 +0000 Subject: [PATCH] [bug-63013] add XWPFRun setLang method git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849152 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xwpf/usermodel/XWPFRun.java | 14 +++++++++++++- .../apache/poi/xwpf/usermodel/TestXWPFRun.java | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) 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 c971b3a926..65e6d00dd1 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -242,6 +242,18 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { return (String) lang; } + /** + * Set the language tag associated with this run. + * + * @param lang the language tag associated with this run + * @since 4.1.0 + */ + public void setLang(String lang) { + CTRPr pr = getRunProperties(true); + CTLanguage ctLang = pr.isSetLang() ? pr.getLang() : pr.addNewLang(); + ctLang.setVal(lang); + } + /** * Whether the bold property shall be applied to all non-complex script * characters in the contents of this run when displayed in a document @@ -1545,7 +1557,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @param create If true, create the properties, if false, do not. * @return The run properties or null if there are no properties and create is false. */ - private CTRPr getRunProperties(boolean create) { + protected CTRPr getRunProperties(boolean create) { CTRPr pr = run.isSetRPr() ? run.getRPr() : null; if (create && pr == null) { pr = run.addNewRPr(); 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 78596f9a3b..45738116fe 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -442,12 +442,27 @@ public class TestXWPFRun { assertEquals(1, count); sampleDoc.close(); } - + @Test public void testSetGetLang() { XWPFRun run = p.createRun(); assertNull(run.getLang()); + run.setLang("en-CA"); + assertEquals("en-CA", run.getLang()); + + run.setLang("fr-CA"); + assertEquals("fr-CA", run.getLang()); + + run.setLang(null); + assertNull(run.getLang()); + } + + @Test + public void testSetGetLang2() { + XWPFRun run = p.createRun(); + assertNull(run.getLang()); + run.getCTR().addNewRPr().addNewLang().setVal("en-CA"); assertEquals("en-CA", run.getLang()); -- 2.39.5