]> source.dussan.org Git - poi.git/commitdiff
Bug 57254: XWPF: Correctly build internal list of styles when styles are added
authorDominik Stadler <centic@apache.org>
Fri, 19 Dec 2014 14:42:25 +0000 (14:42 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 19 Dec 2014 14:42:25 +0000 (14:42 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1646742 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java

index 359f4ece249ddf389ce4bc2cb7ab3c82b195606e..824d39d717d064ff19d647beb4611bed207725b3 100644 (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));
+       }
     }
        
         /**
index fc2c3a28e66ade4a4e054a26b1a60950b72903da..c29d5020850200ecf757cd7687c165a05b4b70d4 100644 (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.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 {
 
@@ -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));
+    }
 }