]> source.dussan.org Git - poi.git/commitdiff
[bug-63013] add XWPFRun setLang method
authorPJ Fanning <fanningpj@apache.org>
Tue, 18 Dec 2018 08:12:42 +0000 (08:12 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 18 Dec 2018 08:12:42 +0000 (08:12 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849152 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java

index c971b3a9261f2c66f2043babd547c83d682ee639..65e6d00dd18a56f70e3226f9f55fe1376ca7b447 100644 (file)
@@ -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();
index 78596f9a3b18f78b58d0418ffae91162e242aff3..45738116fe02fe7872ac3ab21b4ea8d009453c93 100644 (file)
@@ -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());