aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java36
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java26
2 files changed, 56 insertions, 6 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
index eb75a3b4d2..0bcd7b6dd4 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
@@ -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;
@@ -148,6 +149,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
*
* @param styleID styleID of the Style in the style-Document
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
index 6c517f4c95..5487cb429b 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
@@ -17,12 +17,6 @@
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