From d4df69570c52452815060fb0c52c8bfd33c32f89 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 4 Jan 2018 23:32:14 +0000 Subject: [PATCH] [github-86] expose language for Text Runs. Thanks to Geoff Baskwill. This closes #86 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1820242 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFRun.java | 11 +++++++++++ .../poi/xwpf/usermodel/TestXWPFRun.java | 19 +++++++++++++++++++ 2 files changed, 30 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 b00ffd731..fdfe2cc6f 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -248,6 +248,17 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { ); } + /** + * Get the language tag associated with this run, if any. + * + * @return the language tag associated with this run, if any + */ + public String getLang() { + CTRPr pr = run.getRPr(); + Object lang = pr == null || !pr.isSetLang() ? null : pr.getLang().getVal(); + return (String)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 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 a603ab276..459271ec2 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -18,6 +18,7 @@ package org.apache.poi.xwpf.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -37,11 +38,14 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties; import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLang; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLanguage; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLang; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; @@ -452,6 +456,21 @@ public class TestXWPFRun { assertEquals(1, count); sampleDoc.close(); } + + @Test + public void testSetGetLang() throws IOException { + XWPFRun run = p.createRun(); + assertNull(run.getLang()); + + run.getCTR().addNewRPr().addNewLang().setVal("en-CA"); + assertEquals("en-CA", run.getLang()); + + run.getCTR().getRPr().getLang().setVal("fr-CA"); + assertEquals("fr-CA", run.getLang()); + + run.getCTR().getRPr().getLang().setVal(null); + assertNull(run.getLang()); + } @Test public void testSetGetHighlight() throws IOException {