Bug 57254: XWPF: Correctly build internal list of styles when styles are added

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1646742 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-12-19 14:42:25 +00:00
parent 66de3ced8b
commit 68bb54e06a
2 changed files with 35 additions and 13 deletions

View File

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

View File

@ -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));
}
}