#58237 Add some more XWPF header and footer tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695769 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d9697de075
commit
cfbc261e65
@ -31,8 +31,8 @@ import org.apache.poi.xwpf.usermodel.XWPFHeader;
|
|||||||
import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
|
import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFRelation;
|
import org.apache.poi.xwpf.usermodel.XWPFRelation;
|
||||||
import org.apache.xmlbeans.XmlException;
|
|
||||||
import org.apache.xmlbeans.XmlOptions;
|
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.CTHdrFtr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
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.HdrDocument;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum;
|
||||||
|
|
||||||
import schemasMicrosoftComOfficeOffice.CTLock;
|
import schemasMicrosoftComOfficeOffice.CTLock;
|
||||||
import schemasMicrosoftComOfficeOffice.STConnectType;
|
import schemasMicrosoftComOfficeOffice.STConnectType;
|
||||||
import schemasMicrosoftComVml.CTFormulas;
|
import schemasMicrosoftComVml.CTFormulas;
|
||||||
@ -86,7 +87,7 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
* and creates any header and footer objects
|
* and creates any header and footer objects
|
||||||
* as required.
|
* as required.
|
||||||
*/
|
*/
|
||||||
public XWPFHeaderFooterPolicy(XWPFDocument doc) throws IOException, XmlException {
|
public XWPFHeaderFooterPolicy(XWPFDocument doc) {
|
||||||
this(doc, doc.getDocument().getBody().getSectPr());
|
this(doc, doc.getDocument().getBody().getSectPr());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
* and creates any header and footer objects
|
* and creates any header and footer objects
|
||||||
* as required.
|
* 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
|
// Grab what headers and footers have been defined
|
||||||
// For now, we don't care about different ranges, as it
|
// For now, we don't care about different ranges, as it
|
||||||
// doesn't seem that .docx properly supports that
|
// doesn't seem that .docx properly supports that
|
||||||
@ -178,6 +179,7 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
assignHeader(wrapper, type);
|
assignHeader(wrapper, type);
|
||||||
hdrDoc.save(outputStream, xmlOptions);
|
hdrDoc.save(outputStream, xmlOptions);
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,10 +284,16 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CTP p = ftr.addNewP();
|
CTP p = ftr.addNewP();
|
||||||
byte[] rsidr = doc.getDocument().getBody().getPArray(0).getRsidR();
|
CTBody body = doc.getDocument().getBody();
|
||||||
byte[] rsidrdefault = doc.getDocument().getBody().getPArray(0).getRsidRDefault();
|
if (body.sizeOfPArray() > 0) {
|
||||||
p.setRsidP(rsidr);
|
CTP p0 = body.getPArray(0);
|
||||||
p.setRsidRDefault(rsidrdefault);
|
if (p0.isSetRsidR()) {
|
||||||
|
byte[] rsidr = p0.getRsidR();
|
||||||
|
byte[] rsidrdefault = p0.getRsidRDefault();
|
||||||
|
p.setRsidP(rsidr);
|
||||||
|
p.setRsidRDefault(rsidrdefault);
|
||||||
|
}
|
||||||
|
}
|
||||||
CTPPr pPr = p.addNewPPr();
|
CTPPr pPr = p.addNewPPr();
|
||||||
pPr.addNewPStyle().setVal(pStyle);
|
pPr.addNewPStyle().setVal(pStyle);
|
||||||
}
|
}
|
||||||
|
@ -427,6 +427,15 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
|
|||||||
public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
|
public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
|
||||||
return headerFooterPolicy;
|
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
|
* Returns the styles object used
|
||||||
|
@ -317,6 +317,15 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
|
|||||||
return null;
|
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
|
* add a new paragraph at position of the cursor
|
||||||
*
|
*
|
||||||
|
@ -20,8 +20,11 @@ package org.apache.poi.xwpf.model;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
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
|
* Tests for XWPF Header Footer Stuff
|
||||||
@ -35,7 +38,6 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
|
|||||||
private XWPFDocument diffFirst;
|
private XWPFDocument diffFirst;
|
||||||
|
|
||||||
protected void setUp() throws IOException {
|
protected void setUp() throws IOException {
|
||||||
|
|
||||||
noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
|
noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
|
||||||
header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
|
header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
|
||||||
headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.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(2));
|
||||||
assertEquals(policy.getDefaultFooter(), policy.getFooter(3));
|
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() {
|
public void testContents() {
|
||||||
XWPFHeaderFooterPolicy policy;
|
XWPFHeaderFooterPolicy policy;
|
||||||
|
@ -19,15 +19,18 @@ package org.apache.poi.xwpf.usermodel;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFRelation;
|
import org.apache.poi.xssf.usermodel.XSSFRelation;
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||||
|
|
||||||
public class TestXWPFPictureData extends TestCase {
|
public class TestXWPFPictureData extends TestCase {
|
||||||
|
|
||||||
@ -60,6 +63,37 @@ public class TestXWPFPictureData extends TestCase {
|
|||||||
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||||
verifyOneHeaderPicture(readBack);
|
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) {
|
private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
|
||||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||||
|
Loading…
Reference in New Issue
Block a user