diff options
author | PJ Fanning <fanningpj@apache.org> | 2024-08-15 19:45:40 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2024-08-15 19:45:40 +0000 |
commit | c2945d86f92f668a632acbf20f834e74e2990753 (patch) | |
tree | fb3b4f828ebe86e6342f5bce516b917dad11facb /poi-ooxml | |
parent | 571845c5b0b97b4f6ad089b09162ef38b86dfe03 (diff) | |
download | poi-c2945d86f92f668a632acbf20f834e74e2990753.tar.gz poi-c2945d86f92f668a632acbf20f834e74e2990753.zip |
[github-672] Support removing XWPF Styles. Thanks to fangd1997. This closes #672
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1919918 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java | 36 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java | 26 |
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 |