* 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
* 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));
+ }
}
/**
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.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;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
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));
}
/**
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));
+ }
}