]> source.dussan.org Git - poi.git/commitdiff
#58237 Add some more XWPF header and footer tests
authorNick Burch <nick@apache.org>
Thu, 13 Aug 2015 20:01:04 +0000 (20:01 +0000)
committerNick Burch <nick@apache.org>
Thu, 13 Aug 2015 20:01:04 +0000 (20:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695769 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java

index 700645df7548cd49b0f99926d02fa53b082d45d6..4a381de9eb9012e2ef9ec42b8d3a79bfcf7351fc 100644 (file)
@@ -31,8 +31,8 @@ import org.apache.poi.xwpf.usermodel.XWPFHeader;
 import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 import org.apache.poi.xwpf.usermodel.XWPFRelation;
-import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
@@ -45,6 +45,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum;
+
 import schemasMicrosoftComOfficeOffice.CTLock;
 import schemasMicrosoftComOfficeOffice.STConnectType;
 import schemasMicrosoftComVml.CTFormulas;
@@ -86,7 +87,7 @@ public class XWPFHeaderFooterPolicy {
      * and creates any header and footer objects
      * as required.
      */
-    public XWPFHeaderFooterPolicy(XWPFDocument doc) throws IOException, XmlException {
+    public XWPFHeaderFooterPolicy(XWPFDocument doc) {
         this(doc, doc.getDocument().getBody().getSectPr());
     }
 
@@ -95,7 +96,7 @@ public class XWPFHeaderFooterPolicy {
      * and creates any header and footer objects
      * as required.
      */
-    public XWPFHeaderFooterPolicy(XWPFDocument doc, CTSectPr sectPr) throws IOException, XmlException {
+    public XWPFHeaderFooterPolicy(XWPFDocument doc, CTSectPr sectPr) {
         // Grab what headers and footers have been defined
         // For now, we don't care about different ranges, as it
         //  doesn't seem that .docx properly supports that
@@ -178,6 +179,7 @@ public class XWPFHeaderFooterPolicy {
         assignHeader(wrapper, type);
         hdrDoc.save(outputStream, xmlOptions);
         outputStream.close();
+        
         return wrapper;
     }
 
@@ -282,10 +284,16 @@ public class XWPFHeaderFooterPolicy {
             }
         } else {
             CTP p = ftr.addNewP();
-            byte[] rsidr = doc.getDocument().getBody().getPArray(0).getRsidR();
-            byte[] rsidrdefault = doc.getDocument().getBody().getPArray(0).getRsidRDefault();
-            p.setRsidP(rsidr);
-            p.setRsidRDefault(rsidrdefault);
+            CTBody body = doc.getDocument().getBody();
+            if (body.sizeOfPArray() > 0) {
+                CTP p0 = body.getPArray(0);
+                if (p0.isSetRsidR()) {
+                    byte[] rsidr = p0.getRsidR();
+                    byte[] rsidrdefault = p0.getRsidRDefault();
+                    p.setRsidP(rsidr);
+                    p.setRsidRDefault(rsidrdefault);
+                }
+            }
             CTPPr pPr = p.addNewPPr();
             pPr.addNewPStyle().setVal(pStyle);
         }
index d0e70bb62f08fe316a274414b578966619ab8182..707f142bf3b62da7a7c373a8e712bd2d24e0daec 100644 (file)
@@ -427,6 +427,15 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
     public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
         return headerFooterPolicy;
     }
+    public XWPFHeaderFooterPolicy createHeaderFooterPolicy() {
+        if (headerFooterPolicy == null) {
+            if (! ctDocument.getBody().isSetSectPr()) {
+                ctDocument.getBody().addNewSectPr();
+            }
+            headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
+        }
+        return headerFooterPolicy;
+    }
 
     /**
      * Returns the styles object used
index 2f96a681c2854ecc1a395ae13f29cca67cfdd785..e4e1ba177f8fca961b86d1aded16d4cc463d02f6 100644 (file)
@@ -317,6 +317,15 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
         return null;
     }
 
+    /**
+     * Adds a new paragraph at the end of the header or footer
+     */
+    public XWPFParagraph createParagraph() {
+        XWPFParagraph paragraph = new XWPFParagraph(headerFooter.addNewP(), this);
+        paragraphs.add(paragraph);
+        return paragraph;
+    }
+    
     /**
      * add a new paragraph at position of the cursor
      *
index d461265d48be876f737f587be94348f6558c81f6..bb7a7049e1d2ce50c142e14cf20e5e37fac3e1e2 100644 (file)
@@ -20,8 +20,11 @@ package org.apache.poi.xwpf.model;
 import java.io.IOException;
 
 import junit.framework.TestCase;
+
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFFooter;
+import org.apache.poi.xwpf.usermodel.XWPFHeader;
 
 /**
  * Tests for XWPF Header Footer Stuff
@@ -35,7 +38,6 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
     private XWPFDocument diffFirst;
 
     protected void setUp() throws IOException {
-
         noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
         header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
         headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx");
@@ -124,6 +126,35 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
         assertEquals(policy.getDefaultFooter(), policy.getFooter(2));
         assertEquals(policy.getDefaultFooter(), policy.getFooter(3));
     }
+    
+    @SuppressWarnings("resource")
+    public void testCreate() throws Exception {
+        XWPFDocument doc = new XWPFDocument();
+        assertEquals(null, doc.getHeaderFooterPolicy());
+        assertEquals(0, doc.getHeaderList().size());
+        assertEquals(0, doc.getFooterList().size());
+        
+        XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
+        assertNotNull(doc.getHeaderFooterPolicy());
+        assertEquals(0, doc.getHeaderList().size());
+        assertEquals(0, doc.getFooterList().size());
+        
+        // Create a header and a footer
+        XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
+        XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
+        header.createParagraph().createRun().setText("Header Hello");
+        footer.createParagraph().createRun().setText("Footer Bye");
+        
+        
+        // Save, re-load, and check
+        doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
+        assertNotNull(doc.getHeaderFooterPolicy());
+        assertEquals(1, doc.getHeaderList().size());
+        assertEquals(1, doc.getFooterList().size());
+        
+        assertEquals("Header Hello\n", doc.getHeaderList().get(0).getText());
+        assertEquals("Footer Bye\n", doc.getFooterList().get(0).getText());
+    }
 
     public void testContents() {
         XWPFHeaderFooterPolicy policy;
index 0d3cea64cb5b28d32e3b674d9216a03af3e03178..0b29956c67ab9f472a354b0de670c1f8dc88381b 100644 (file)
@@ -19,15 +19,18 @@ package org.apache.poi.xwpf.usermodel;
 
 import static org.junit.Assert.assertArrayEquals;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.List;
 
 import junit.framework.TestCase;
+
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.xssf.usermodel.XSSFRelation;
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 
 public class TestXWPFPictureData extends TestCase {
 
@@ -60,6 +63,37 @@ public class TestXWPFPictureData extends TestCase {
         XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
         verifyOneHeaderPicture(readBack);
     }
+    
+    public void FIXMEtestCreateHeaderPicture() throws Exception { // TODO Fix
+        XWPFDocument doc = new XWPFDocument();
+        
+        // Starts with no header
+        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
+        assertNull(policy);
+        
+        // Add a default header
+        policy = doc.createHeaderFooterPolicy();
+        
+        XWPFParagraph[] hparas = new XWPFParagraph[] {
+                new XWPFParagraph(CTP.Factory.newInstance(), doc)
+        };
+        hparas[0].createRun().setText("Header Hello World!");
+        XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, hparas);
+        assertEquals(0, header.getAllPictures().size());
+        assertEquals(1, header.getParagraphs().size());
+        
+        // Add a picture to it
+        header.getParagraphs().get(0).getRuns().get(0).addPicture(
+                new ByteArrayInputStream(new byte[] {1,2,3,4}), 
+                Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
+        
+        // Check
+        verifyOneHeaderPicture(doc);
+        
+        // Save, re-load, re-check
+        XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
+        verifyOneHeaderPicture(readBack);
+    }
 
     private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
         XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();