]> source.dussan.org Git - poi.git/commitdiff
[github-672] Support removing XWPF Styles. Thanks to fangd1997. This closes #672
authorPJ Fanning <fanningpj@apache.org>
Thu, 15 Aug 2024 19:45:40 +0000 (19:45 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 15 Aug 2024 19:45:40 +0000 (19:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1919918 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java

index eb75a3b4d288e69cf7f48484227cfcef6a684deb..0bcd7b6dd4e185466222a11196e3f9d0e86466f7 100644 (file)
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import javax.xml.namespace.QName;
@@ -147,6 +148,41 @@ public class XWPFStyles extends POIXMLDocumentPart {
         }
     }
 
+    /**
+     * Gets the underlying CTStyles object for the Styles.
+     *
+     * @return CTStyles object
+     * @since POI 5.3.1
+     */
+    public CTStyles getCtStyles() {
+        return ctStyles;
+    }
+
+    /**
+     * Get the list of {@link XWPFStyle} in the Styles part.
+     *
+     * @since POI 5.3.1
+     */
+    public List<XWPFStyle> getStyles() {
+        return Collections.unmodifiableList(listStyle);
+    }
+
+    /**
+     * Remove the specified style if present.
+     *
+     * @param pos Array position of the style to be removed
+     * @return True if the style was removed.
+     * @since POI 5.3.1
+     */
+    public boolean removeStyle(int pos) {
+        if (pos >= 0 && pos < getNumberOfStyles()) {
+            listStyle.remove(pos);
+            ctStyles.removeStyle(pos);
+            return true;
+        }
+        return false;
+    }
+
     /**
      * checks whether style with styleID exist
      *
index 6c517f4c9563cc1328009fc2661df629700cd672..5487cb429bdeab06653d09f84b224c950c756c02 100644 (file)
 
 package org.apache.poi.xwpf.usermodel;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -36,6 +30,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
 
+import static org.junit.jupiter.api.Assertions.*;
+
 public final class TestXWPFStyles {
     @Test
     void testGetUsedStyles() throws IOException {
@@ -75,6 +71,24 @@ public final class TestXWPFStyles {
         }
     }
 
+    @Test
+    void testRemoveStyle() throws IOException {
+        try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx")) {
+            XWPFStyles styles = sampleDoc.getStyles();
+            assertEquals(12, styles.getStyles().size());
+            // styles.getStyles() returns an unmodifiable list
+            assertThrows(UnsupportedOperationException.class, () -> styles.getStyles().remove(0));
+
+            XWPFStyle styleToRemove = styles.getStyle("Standard");
+            assertTrue(styles.removeStyle(styles.getStyles().indexOf(styleToRemove)));
+            assertEquals(11, styles.getStyles().size());
+
+            XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
+            assertEquals(11, docIn.getStyles().getStyles().size());
+            assertNull(docIn.getStyles().getStyle("Standard"));
+        }
+    }
+
     /**
      * Bug #52449 - We should be able to write a file containing
      * both regular and glossary styles without error