]> source.dussan.org Git - poi.git/commitdiff
Start on xwpf header/footer support
authorNick Burch <nick@apache.org>
Sat, 9 Aug 2008 11:27:29 +0000 (11:27 +0000)
committerNick Burch <nick@apache.org>
Sat, 9 Aug 2008 11:27:29 +0000 (11:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@684229 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XMLWordDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java

index 219cacec142a28b6c7f88a246b9c1d0d055677a8..d53cfa57637013aaf91b2fecf69b99f3d4f070fd 100644 (file)
@@ -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<XWPFHyperlink> hyperlinks;
        protected List<XWPFParagraph> paragraphs;
        protected List<XWPFTable> 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<XWPFHyperlink> 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<XWPFComment> 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
index 27c988c6d7512f2f662bccf6decc3457f25dc6a7..5e30c28408916e161a95f926bc46949ec85d6dd5 100644 (file)
@@ -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 (file)
index 0000000..7131cce
--- /dev/null
@@ -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 (file)
index 0000000..971b403
--- /dev/null
@@ -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 (file)
index 0000000..4b1325f
--- /dev/null
@@ -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();
+       }
+}
index f29d09d02fc180ce1ae3057a03b4104eb112371b..b61af2f06c3f01d29e26608a6e88698565e055af 100644 (file)
@@ -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(