poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject svn commit: r805820 - in /poi/trunk/src/ooxml: java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java
Date Wed, 19 Aug 2009 14:33:27 GMT
Author: paolo
Date: Wed Aug 19 14:33:27 2009
New Revision: 805820

URL: http://svn.apache.org/viewvc?rev=805820&view=rev
Log:
createHeader/Footer with paragraphs and fixed GUMP break (case sensitive/insensitive filename
problem)

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java?rev=805820&r1=805819&r2=805820&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java Wed Aug
19 14:33:27 2009
@@ -30,6 +30,7 @@
 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 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 @@
     	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 @@
 	}
 
 
-	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;
 	}
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java?rev=805820&r1=805819&r2=805820&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java Wed Aug
19 14:33:27 2009
@@ -24,6 +24,9 @@
 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 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 @@
 		// 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());



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message