createHeader/Footer with paragraphs and fixed GUMP break (case sensitive/insensitive filename problem)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@805820 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paolo Mottadelli 2009-08-19 14:33:27 +00:00
parent 9926c89006
commit a7e9db045f
2 changed files with 58 additions and 20 deletions

View File

@ -30,6 +30,7 @@ import org.apache.poi.xwpf.usermodel.XWPFFactory;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
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;
@ -127,15 +128,18 @@ public class XWPFHeaderFooterPolicy {
}
}
public XWPFHeader createHeader(Enum type) throws IOException {
return createHeader(type, null);
}
public XWPFHeader createHeader(Enum type, XWPFParagraph[] pars) throws IOException {
XWPFRelation relation = XWPFRelation.HEADER;
String pStyle = "Header";
int i = getRelationIndex(relation);
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
XWPFHeader wrapper = (XWPFHeader)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper);
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
hdrDoc.setHdr(hdr);
@ -148,15 +152,18 @@ public class XWPFHeaderFooterPolicy {
return wrapper;
}
public XWPFFooter createFooter(Enum type) throws IOException {
return createFooter(type, null);
}
public XWPFFooter createFooter(Enum type, XWPFParagraph[] pars) throws IOException {
XWPFRelation relation = XWPFRelation.FOOTER;
String pStyle = "Footer";
int i = getRelationIndex(relation);
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
XWPFFooter wrapper = (XWPFFooter)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper);
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
ftrDoc.setFtr(ftr);
@ -183,29 +190,36 @@ public class XWPFHeaderFooterPolicy {
}
private CTHdrFtr buildFtr(Enum type, String pStyle, XWPFHeaderFooter wrapper) {
CTHdrFtr ftr = buildHdrFtr(pStyle);
private CTHdrFtr buildFtr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
CTHdrFtr ftr = buildHdrFtr(pStyle, pars);
setFooterReference(type, wrapper);
return ftr;
}
private CTHdrFtr buildHdr(Enum type, String pStyle, XWPFHeaderFooter wrapper) {
CTHdrFtr hdr = buildHdrFtr(pStyle);
private CTHdrFtr buildHdr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
CTHdrFtr hdr = buildHdrFtr(pStyle, pars);
setHeaderReference(type, wrapper);
return hdr;
}
private CTHdrFtr buildHdrFtr(String pStyle) {
private CTHdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs) {
CTHdrFtr ftr = CTHdrFtr.Factory.newInstance();
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);
CTPPr pPr = p.addNewPPr();
pPr.addNewPStyle().setVal(pStyle);
if (paragraphs != null) {
for (int i = 0 ; i < paragraphs.length ; i++) {
CTP p = ftr.addNewP();
ftr.setPArray(0, paragraphs[i].getCTP());
}
}
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);
CTPPr pPr = p.addNewPPr();
pPr.addNewPStyle().setVal(pStyle);
}
return ftr;
}

View File

@ -24,6 +24,9 @@ import junit.framework.TestCase;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
public class TestXWPFHeader extends TestCase {
@ -54,7 +57,7 @@ public class TestXWPFHeader extends TestCase {
public void testSetHeader() throws IOException {
File sampleFile = new File(
System.getProperty("HWPF.testdata.path") +
File.separator + "sampleDoc.docx"
File.separator + "SampleDoc.docx"
);
assertTrue(sampleFile.exists());
XWPFDocument sampleDoc;
@ -64,10 +67,31 @@ public class TestXWPFHeader extends TestCase {
// no header is set (yet)
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
assertNull(policy.getDefaultHeader());
assertNull(policy.getFirstPageHeader());
assertNull(policy.getDefaultFooter());
CTP ctP1 = CTP.Factory.newInstance();
CTR ctR1 = ctP1.addNewR();
CTText t = ctR1.addNewT();
t.set("Paragraph in header");
CTP ctP2 = CTP.Factory.newInstance();
CTR ctR2 = ctP2.addNewR();
CTText t2 = ctR2.addNewT();
t2.set("Second paragraph.. for footer");
XWPFParagraph p1 = new XWPFParagraph(ctP1);
XWPFParagraph[] pars = new XWPFParagraph[1];
pars[0] = p1;
XWPFParagraph p2 = new XWPFParagraph(ctP2);
XWPFParagraph[] pars2 = new XWPFParagraph[1];
pars2[0] = p2;
// set a default header and test it is not null
policy.createHeader(policy.DEFAULT);
policy.createHeader(policy.DEFAULT, pars);
policy.createHeader(policy.FIRST);
policy.createFooter(policy.DEFAULT);
policy.createFooter(policy.DEFAULT, pars2);
assertNotNull(policy.getDefaultHeader());
assertNotNull(policy.getFirstPageHeader());