[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
This commit is contained in:
PJ Fanning 2024-08-15 19:45:40 +00:00
parent 571845c5b0
commit c2945d86f9
2 changed files with 56 additions and 6 deletions

View 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
*

View File

@ -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