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;
}
}
+ /**
+ * 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
*
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;
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 {
}
}
+ @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