You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2018/07/25 17:21:58 UTC

svn commit: r1836649 - in /poi/trunk/src/ooxml: java/org/apache/poi/xwpf/usermodel/ testcases/org/apache/poi/xwpf/usermodel/

Author: fanningpj
Date: Wed Jul 25 17:21:58 2018
New Revision: 1836649

URL: http://svn.apache.org/viewvc?rev=1836649&view=rev
Log:
[github-117] Add getStyleWithName() to XWPFStyles. This closes #117

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java?rev=1836649&r1=1836648&r2=1836649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java Wed Jul 25 17:21:58 2018
@@ -1333,15 +1333,14 @@ public class XWPFParagraph implements IB
     }
 
     /**
-     * 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);
     }
 
     /**

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=1836649&r1=1836648&r2=1836649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Wed Jul 25 17:21:58 2018
@@ -56,33 +56,7 @@ import org.openxmlformats.schemas.drawin
 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 ISDTCont
     public List<XWPFPicture> 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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java?rev=1836649&r1=1836648&r2=1836649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java Wed Jul 25 17:21:58 2018
@@ -323,4 +323,21 @@ public class XWPFStyles extends POIXMLDo
     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;
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java?rev=1836649&r1=1836648&r2=1836649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java Wed Jul 25 17:21:58 2018
@@ -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);
+        
+    }
+
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java?rev=1836649&r1=1836648&r2=1836649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java Wed Jul 25 17:21:58 2018
@@ -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());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org