diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index 359f4ece2..824d39d71 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -73,22 +73,18 @@ public class XWPFStyles extends POIXMLDocumentPart{ * Read document */ @Override - @SuppressWarnings("deprecation") protected void onDocumentRead() throws IOException{ StylesDocument stylesDoc; try { InputStream is = getPackagePart().getInputStream(); stylesDoc = StylesDocument.Factory.parse(is); - ctStyles = stylesDoc.getStyles(); + setStyles(stylesDoc.getStyles()); latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this); } catch (XmlException e) { throw new POIXMLException("Unable to read styles", e); } - // Build up all the style objects - for(CTStyle style : ctStyles.getStyleArray()) { - listStyle.add(new XWPFStyle(style, this)); - } + } @Override @@ -113,8 +109,14 @@ public class XWPFStyles extends POIXMLDocumentPart{ * Sets the ctStyles * @param styles */ + @SuppressWarnings("deprecation") public void setStyles(CTStyles styles) { ctStyles = styles; + + // Build up all the style objects + for(CTStyle style : ctStyles.getStyleArray()) { + listStyle.add(new XWPFStyle(style, this)); + } } /** diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index fc2c3a28e..c29d50208 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -20,17 +20,16 @@ package org.apache.poi.xwpf.usermodel; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.lang.String; import junit.framework.TestCase; import org.apache.poi.xwpf.XWPFTestDataSamples; - import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType; public class TestXWPFStyles extends TestCase { @@ -59,17 +58,19 @@ public class TestXWPFStyles extends TestCase { XWPFDocument docOut = new XWPFDocument(); XWPFStyles styles = docOut.createStyles(); - String strStyleName = "headline1"; + String strStyleId = "headline1"; CTStyle ctStyle = CTStyle.Factory.newInstance(); - ctStyle.setStyleId(strStyleName); + ctStyle.setStyleId(strStyleId); XWPFStyle s = new XWPFStyle(ctStyle); styles.addStyle(s); + + assertTrue(styles.styleExist(strStyleId)); XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); styles = docIn.getStyles(); - assertTrue(styles.styleExist(strStyleName)); + assertTrue(styles.styleExist(strStyleId)); } /** @@ -118,4 +119,23 @@ public class TestXWPFStyles extends TestCase { assertEquals(true, ls.isLatentStyle("ex1")); assertEquals(false, ls.isLatentStyle("notex1")); } + + public void testSetStyles_Bug57254() throws IOException { + XWPFDocument docOut = new XWPFDocument(); + XWPFStyles styles = docOut.createStyles(); + + CTStyles ctStyles = CTStyles.Factory.newInstance(); + String strStyleId = "headline1"; + CTStyle ctStyle = ctStyles.addNewStyle(); + + ctStyle.setStyleId(strStyleId); + styles.setStyles(ctStyles); + + assertTrue(styles.styleExist(strStyleId)); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut); + + styles = docIn.getStyles(); + assertTrue(styles.styleExist(strStyleId)); + } }