From: Nick Burch Date: Sat, 9 Aug 2008 11:27:29 +0000 (+0000) Subject: Start on xwpf header/footer support X-Git-Tag: REL_3_5_BETA2~18 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b9d51638ea08a6d919ae71e76a545238f75de832;p=poi.git Start on xwpf header/footer support git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@684229 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java index 219cacec14..d53cfa5763 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java @@ -35,10 +35,14 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; +import org.apache.poi.xwpf.usermodel.XWPFFooter; +import org.apache.poi.xwpf.usermodel.XWPFHeader; import org.apache.poi.xwpf.usermodel.XWPFHyperlink; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFComment; @@ -57,8 +61,10 @@ import org.apache.poi.xwpf.usermodel.XWPFTable; */ public class XWPFDocument extends POIXMLDocument { public static final String MAIN_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"; - public static final String FOOTER_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"; public static final String HEADER_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"; + public static final String HEADER_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"; + public static final String FOOTER_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"; + public static final String FOOTER_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"; public static final String STYLES_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"; public static final String STYLES_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"; public static final String HYPERLINK_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"; @@ -69,6 +75,10 @@ public class XWPFDocument extends POIXMLDocument { protected List hyperlinks; protected List paragraphs; protected List tables; + /** Should only ever be zero or one of these, we think */ + protected XWPFHeader header; + /** Should only ever be zero or one of these, we think */ + protected XWPFFooter footer; public XWPFDocument(Package container) throws OpenXML4JException, IOException, XmlException { super(container); @@ -122,6 +132,24 @@ public class XWPFDocument extends POIXMLDocument { for(PackageRelationship rel : getCorePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) { embedds.add(getTargetPart(rel)); } + + // Fetch the header, if there's one + PackageRelationshipCollection headerRel = getCorePart().getRelationshipsByType(HEADER_RELATION_TYPE); + if(headerRel != null && headerRel.size() > 0) { + PackagePart headerPart = getTargetPart(headerRel.getRelationship(0)); + header = new XWPFHeader( + HdrDocument.Factory.parse(headerPart.getInputStream()).getHdr() + ); + } + + // Fetch the footer, if there's one + PackageRelationshipCollection footerRel = getCorePart().getRelationshipsByType(FOOTER_RELATION_TYPE); + if(footerRel != null && footerRel.size() > 0) { + PackagePart footerPart = getTargetPart(footerRel.getRelationship(0)); + footer = new XWPFFooter( + FtrDocument.Factory.parse(footerPart.getInputStream()).getFtr() + ); + } } /** @@ -141,8 +169,7 @@ public class XWPFDocument extends POIXMLDocument { return tables.iterator(); } - public XWPFHyperlink getHyperlinkByID(String id) - { + public XWPFHyperlink getHyperlinkByID(String id) { Iterator iter = hyperlinks.iterator(); while(iter.hasNext()) { @@ -153,9 +180,13 @@ public class XWPFDocument extends POIXMLDocument { return null; } + public XWPFHyperlink[] getHyperlinks() { + return hyperlinks.toArray( + new XWPFHyperlink[hyperlinks.size()] + ); + } - public XWPFComment getCommentByID(String id) - { + public XWPFComment getCommentByID(String id) { Iterator iter = comments.iterator(); while(iter.hasNext()) { @@ -166,6 +197,18 @@ public class XWPFDocument extends POIXMLDocument { return null; } + public XWPFComment[] getComments() { + return comments.toArray( + new XWPFComment[comments.size()] + ); + } + + public XWPFHeader getDocumentHeader() { + return header; + } + public XWPFFooter getDocumentFooter() { + return footer; + } /** * Returns the low level body of the document diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XMLWordDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XMLWordDocument.java index 27c988c6d7..5e30c28408 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XMLWordDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XMLWordDocument.java @@ -31,4 +31,25 @@ public class XMLWordDocument { public XWPFDocument _getXWPFXML() { return xwpfXML; } + + public XWPFHyperlink getHyperlinkByID(String id) { + return xwpfXML.getHyperlinkByID(id); + } + public XWPFHyperlink[] getHyperlinks() { + return xwpfXML.getHyperlinks(); + } + + public XWPFComment getCommentByID(String id) { + return xwpfXML.getCommentByID(id); + } + public XWPFComment[] getComments() { + return xwpfXML.getComments(); + } + + public XWPFHeader getHeader() { + return xwpfXML.getDocumentHeader(); + } + public XWPFFooter getFooter() { + return xwpfXML.getDocumentFooter(); + } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java new file mode 100644 index 0000000000..7131cceb8a --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java @@ -0,0 +1,32 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr; + +/** + * Sketch of XWPF footer class + */ +public class XWPFFooter extends XWPFHeaderFooter { + public XWPFFooter() { + super(); + } + public XWPFFooter(CTHdrFtr hdrFtr) { + super(hdrFtr); + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java new file mode 100644 index 0000000000..971b403cb5 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java @@ -0,0 +1,32 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr; + +/** + * Sketch of XWPF header class + */ +public class XWPFHeader extends XWPFHeaderFooter { + public XWPFHeader() { + super(); + } + public XWPFHeader(CTHdrFtr hdrFtr) { + super(hdrFtr); + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java new file mode 100644 index 0000000000..4b1325f183 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java @@ -0,0 +1,33 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr; + +/** + * Parent of XWPF headers and footers + */ +public abstract class XWPFHeaderFooter { + protected CTHdrFtr headerFooter; + + protected XWPFHeaderFooter(CTHdrFtr hdrFtr) { + headerFooter = hdrFtr; + } + protected XWPFHeaderFooter() { + headerFooter = CTHdrFtr.Factory.newInstance(); + } +} diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java index f29d09d02f..b61af2f06c 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java @@ -111,7 +111,7 @@ public class TestXWPFWordExtractor extends TestCase { assertTrue(text.length() > 0); char euro = '\u20ac'; - System.err.println("'"+text.substring(text.length() - 20) + "'"); +// System.err.println("'"+text.substring(text.length() - 20) + "'"); // Check contents assertTrue(text.startsWith(