You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2010/06/11 16:37:59 UTC
svn commit: r953704 [1/3] - in /poi/trunk/src: documentation/content/xdocs/
ooxml/java/org/apache/poi/ ooxml/java/org/apache/poi/xwpf/model/
ooxml/java/org/apache/poi/xwpf/usermodel/
ooxml/testcases/org/apache/poi/xwpf/ ooxml/testcases/org/apache/poi/x...
Author: nick
Date: Fri Jun 11 14:37:58 2010
New Revision: 953704
URL: http://svn.apache.org/viewvc?rev=953704&view=rev
Log:
Apply (with slight tweaks) patch from Phillip Epp from bug #48574 - further XWPF support for tables, paragraphs, including enhanced support for adding new ones
Added:
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyElementType.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyType.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/Document.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/PositionInParagraph.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextSegement.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFAbstractNum.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFNum.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyle.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableRow.java
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Jun 11 14:37:58 2010
@@ -34,6 +34,7 @@
<changes>
<release version="3.7-SNAPSHOT" date="2010-??-??">
+ <action dev="POI-DEVELOPERS" type="add">48574 - further XWPF support for tables, paragraphs, including enhanced support for adding new ones</action>
<action dev="POI-DEVELOPERS" type="add">48245 - tweak HWPF table cell detection to work across more files</action>
<action dev="POI-DEVELOPERS" type="add">48996 - initial support for External Name References in HSSF formula evaluation</action>
<action dev="POI-DEVELOPERS" type="fix">46664 - fix up Tab IDs when adding new sheets, so that print areas don't end up invalid</action>
Modified: poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java Fri Jun 11 14:37:58 2010
@@ -281,5 +281,4 @@ public class POIXMLDocumentPart {
protected void onDocumentRead() throws IOException{
}
-
}
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=953704&r1=953703&r2=953704&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 Fri Jun 11 14:37:58 2010
@@ -120,8 +120,7 @@ public class XWPFHeaderFooterPolicy {
CTHdrFtrRef ref = sectPr.getFooterReferenceArray(i);
PackagePart ftrPart = doc.getPartById(ref.getId());
XWPFFooter ftr = new XWPFFooter(
- FtrDocument.Factory.parse(ftrPart.getInputStream()).getFtr()
- );
+ FtrDocument.Factory.parse(ftrPart.getInputStream()).getFtr());
// Assign it
Enum type = ref.getType();
@@ -163,6 +162,7 @@ public class XWPFHeaderFooterPolicy {
XWPFHeader wrapper = (XWPFHeader)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
+ wrapper.setHeaderFooter(hdr);
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
hdrDoc.setHdr(hdr);
@@ -189,6 +189,7 @@ public class XWPFHeaderFooterPolicy {
XWPFFooter wrapper = (XWPFFooter)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
+ wrapper.setHeaderFooter(ftr);
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
ftrDoc.setFtr(ftr);
@@ -459,6 +460,6 @@ public class XWPFHeaderFooterPolicy {
shapeTextPath.setString(text);
pict.set(group);
// end watermark paragraph
- return new XWPFParagraph(p);
+ return new XWPFParagraph(p, null);
}
}
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyElementType.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyElementType.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyElementType.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyElementType.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,34 @@
+/* ====================================================================
+ 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;
+
+/**
+ * <p>
+ * 9 Jan 2010
+ * </p>
+ * <p>
+ * // TODO insert Javadoc here!
+ * </p>
+ * @author epp
+ *
+ */
+public enum BodyElementType {
+ PARAGRAPH,
+ TABLE,
+
+}
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyType.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyType.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyType.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/BodyType.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,37 @@
+/* ====================================================================
+ 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;
+
+/**
+ * <p>
+ * 9 Jan 2010
+ * </p>
+ * <p>
+ * // TODO insert Javadoc here!
+ * </p>
+ * @author epp
+ *
+ */
+public enum BodyType {
+ DOCUMENT,
+ HEADER,
+ FOOTER,
+ FOOTNOTE,
+ TABLECELL
+
+}
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/Document.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/Document.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/Document.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/Document.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,38 @@
+/* ====================================================================
+ 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;
+
+public interface Document {
+ /** Extended windows meta file */
+ public static final int PICTURE_TYPE_EMF = 2;
+
+ /** Windows Meta File */
+ public static final int PICTURE_TYPE_WMF = 3;
+
+ /** Mac PICT format */
+ public static final int PICTURE_TYPE_PICT = 4;
+
+ /** JPEG format */
+ public static final int PICTURE_TYPE_JPEG = 5;
+
+ /** PNG format */
+ public static final int PICTURE_TYPE_PNG = 6;
+
+ /** Device independent bitmap */
+ public static final int PICTURE_TYPE_DIB = 7;
+
+}
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBody.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,131 @@
+/* ====================================================================
+ 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 java.util.List;
+
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.xmlbeans.XmlCursor;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
+
+
+
+/**
+ * <p>
+ * 8 Jan 2010
+ * </p>
+ * <p>
+ * // This Interface represents an object, which is able to have a collection of paragraphs and tables
+ * this can be XWFPDocument, XWPFHeader, XWPFFooter, XWPFTableCell
+ * </p>
+ * @author Philipp Epp
+ *
+ */
+public interface IBody {
+ /**
+ * returns the Part, to which the body belongs, which you need for adding relationship to other parts
+ * Actually it is needed of the class XWPFTableCell. Because you have to know to which part the tableCell
+ * belongs.
+ * @return
+ */
+ IBody getPart();
+
+ /**
+ * get the PartType of the body, for example
+ * DOCUMENT, HEADER, FOOTER, FOOTNOTE,
+ * @return
+ */
+ BodyType getPartType();
+ /**
+ * Returns the paragraph(s) that holds
+ * the text of the header or footer.
+ */
+ public List<XWPFParagraph> getParagraphs();
+
+
+ /**
+ * Return the table(s) that holds the text
+ * of the IBodyPart, for complex cases
+ * where a paragraph isn't used.
+ */
+ public List<XWPFTable> getTables();
+
+ /**
+ * if there is a corresponding {@link XWPFParagraph} of the parameter ctTable in the paragraphList of this header or footer
+ * the method will return this paragraph
+ * if there is no corresponding {@link XWPFParagraph} the method will return null
+ * @param p is instance of CTP and is searching for an XWPFParagraph
+ * @return null if there is no XWPFParagraph with an corresponding CTPparagraph in the paragraphList of this header or footer
+ * XWPFParagraph with the correspondig CTP p
+ */
+ public XWPFParagraph getParagraph(CTP p);
+
+ /**
+ * if there is a corresponding {@link XWPFTable} of the parameter ctTable in the tableList of this header
+ * the method will return this table
+ * if there is no corresponding {@link XWPFTable} the method will return null
+ * @param ctTable
+ * @return
+ */
+ public XWPFTable getTable(CTTbl ctTable);
+
+ /**
+ * Returns the paragraph that of position pos
+ */
+ public XWPFParagraph getParagraphArray(int pos);
+
+ /**
+ * Returns the table at position pos
+ */
+ public XWPFTable getTableArray(int pos);
+
+ /**
+ *inserts a new paragraph at position of the cursor
+ * @param cursor
+ * @return
+ */
+ public XWPFParagraph insertNewParagraph(XmlCursor cursor);
+
+ /**
+ * inserts a new Table at the cursor position.
+ * @param cursor
+ * @return
+ */
+ public XWPFTable insertNewTbl(XmlCursor cursor);
+
+ /**
+ * inserts a new Table at position pos
+ * @param pos
+ * @param table
+ */
+ void insertTable(int pos, XWPFTable table);
+
+ /**
+ * returns the TableCell to which the Table belongs
+ * @param cell
+ * @return
+ */
+ XWPFTableCell getTableCell(CTTc cell);
+
+
+
+
+}
+
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,31 @@
+/* ====================================================================
+ 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.apache.poi.POIXMLDocumentPart;
+
+/**
+ * 9 Jan 2010
+ * @author Philipp Epp
+ *
+ */
+public interface IBodyElement{
+ IBody getPart();
+ BodyType getPartType();
+ BodyElementType getElementType();
+}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java Fri Jun 11 14:37:58 2010
@@ -17,8 +17,8 @@
package org.apache.poi.xwpf.usermodel;
-import java.util.Map;
import java.util.HashMap;
+import java.util.Map;
/**
* Specifies all types of alignment which are available to be applied to objects in a
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/PositionInParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/PositionInParagraph.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/PositionInParagraph.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/PositionInParagraph.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,65 @@
+/* ====================================================================
+ 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;
+
+
+/**
+ * postion of a character in a paragrapho
+* 1st RunPositon
+* 2nd TextPosition
+* 3rd CharacterPosition
+*
+*
+*/
+public class PositionInParagraph {
+ private int posRun = 0, posText = 0, posChar = 0;
+
+ public PositionInParagraph(){
+ }
+
+ public PositionInParagraph(int posRun, int posText, int posChar){
+ this.posRun=posRun;
+ this.posChar=posChar;
+ this.posText= posText;
+ }
+
+ public int getRun() {
+ return posRun;
+ }
+
+ public void setRun(int beginRun) {
+ this.posRun = beginRun;
+ }
+
+ public int getText() {
+ return posText;
+ }
+
+ public void setText(int beginText) {
+ this.posText = beginText;
+ }
+
+ public int getChar() {
+ return posChar;
+ }
+
+ public void setChar(int beginChar) {
+ this.posChar = beginChar;
+ }
+
+}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java Fri Jun 11 14:37:58 2010
@@ -18,8 +18,8 @@ package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger;
-import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space;
import org.apache.poi.util.Internal;
+import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextSegement.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextSegement.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextSegement.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextSegement.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,98 @@
+/* ====================================================================
+ 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;
+
+
+/**
+ * saves the begin and end position of a text in a Paragraph
+*/
+public class TextSegement {
+ private PositionInParagraph beginPos;
+ private PositionInParagraph endPos;
+
+ public TextSegement(){
+ this.beginPos = new PositionInParagraph();
+ this. endPos = new PositionInParagraph();
+ }
+
+ public TextSegement(int beginRun, int endRun, int beginText, int endText, int beginChar, int endChar){
+ PositionInParagraph beginPos = new PositionInParagraph(beginRun, beginText, beginChar);
+ PositionInParagraph endPos = new PositionInParagraph(endRun, endText, endChar);
+ this.beginPos = beginPos;
+ this.endPos = endPos;
+ }
+
+ public TextSegement(PositionInParagraph beginPos, PositionInParagraph endPos){
+ this.beginPos = beginPos;
+ this.endPos = endPos;
+ }
+
+ public PositionInParagraph getBeginPos(){
+ return beginPos;
+ }
+
+ public PositionInParagraph getEndPos(){
+ return endPos;
+ }
+
+ public int getBeginRun(){
+ return beginPos.getRun();
+ }
+
+ public void setBeginRun(int beginRun){
+ beginPos.setRun(beginRun);
+ }
+
+ public int getBeginText(){
+ return beginPos.getText();
+ }
+
+ public void setBeginText(int beginText){
+ beginPos.setText(beginText);
+ }
+
+ public int getBeginChar(){
+ return beginPos.getChar();
+ }
+
+ public void setBeginChar(int beginChar){
+ beginPos.setChar(beginChar);
+ }
+ public int getEndRun(){
+ return endPos.getRun();
+ }
+
+ public void setEndRun(int endRun){
+ endPos.setRun(endRun);
+ }
+
+ public int getEndText(){
+ return endPos.getText();
+ }
+
+ public void setEndText(int endText){
+ endPos.setText(endText);
+ }
+
+ public int getEndChar(){
+ return endPos.getChar();
+ }
+
+ public void setEndChar(int endChar){
+ endPos.setChar(endChar);
+ }
+}
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFAbstractNum.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFAbstractNum.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFAbstractNum.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFAbstractNum.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,59 @@
+/* ====================================================================
+ 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.CTAbstractNum;
+
+/**
+ * @author Philipp Epp
+ *
+ */
+public class XWPFAbstractNum {
+ private CTAbstractNum ctAbstractNum;
+ protected XWPFNumbering numbering;
+
+ protected XWPFAbstractNum() {
+ this.ctAbstractNum = null;
+ this.numbering = null;
+
+ }
+ public XWPFAbstractNum(CTAbstractNum abstractNum){
+ this.ctAbstractNum = abstractNum;
+ }
+
+ public XWPFAbstractNum(CTAbstractNum ctAbstractNum, XWPFNumbering numbering){
+ this.ctAbstractNum = ctAbstractNum;
+ this.numbering = numbering;
+ }
+ public CTAbstractNum getAbstractNum(){
+ return ctAbstractNum;
+ }
+
+ public XWPFNumbering getNumbering(){
+ return numbering;
+ }
+
+ public CTAbstractNum getCTAbstractNum(){
+ return ctAbstractNum;
+ }
+
+ public void setNumbering(XWPFNumbering numbering){
+ this.numbering = numbering;
+ }
+
+}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java Fri Jun 11 14:37:58 2010
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
@@ -43,19 +44,24 @@ import org.apache.poi.openxml4j.opc.Pack
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
-import org.apache.poi.util.PackageHelper;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
+import org.apache.poi.util.PackageHelper;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
+import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument;
@@ -67,23 +73,29 @@ import org.openxmlformats.schemas.wordpr
* Experimental class to do low level processing
* of docx files.
*
- * If you are using these low level classes, then you
+ * If you're using these low level classes, then you
* will almost certainly need to refer to the OOXML
* specifications from
* http://www.ecma-international.org/publications/standards/Ecma-376.htm
*
* WARNING - APIs expected to change rapidly
*/
-public class XWPFDocument extends POIXMLDocument {
+public class XWPFDocument extends POIXMLDocument implements Document, IBody {
private CTDocument1 ctDocument;
private XWPFSettings settings;
+ protected List<XWPFFooter> footers;
+ protected List <XWPFHeader> headers;
protected List<XWPFComment> comments;
protected List<XWPFHyperlink> hyperlinks;
protected List<XWPFParagraph> paragraphs;
protected List<XWPFTable> tables;
+ protected List<IBodyElement> bodyElements;
+ protected List<XWPFPictureData> pictures;
protected Map<Integer, XWPFFootnote> footnotes;
protected Map<Integer, XWPFFootnote> endnotes;
+ protected XWPFNumbering numbering;
+ protected XWPFStyles styles;
/** Handles the joy of different headers/footers for different pages */
private XWPFHeaderFooterPolicy headerFooterPolicy;
@@ -113,6 +125,9 @@ public class XWPFDocument extends POIXML
comments = new ArrayList<XWPFComment>();
paragraphs = new ArrayList<XWPFParagraph>();
tables= new ArrayList<XWPFTable>();
+ bodyElements = new ArrayList<IBodyElement>();
+ footers = new ArrayList<XWPFFooter>();
+ headers = new ArrayList<XWPFHeader>();
footnotes = new HashMap<Integer, XWPFFootnote>();
endnotes = new HashMap<Integer, XWPFFootnote>();
@@ -120,27 +135,49 @@ public class XWPFDocument extends POIXML
DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream());
ctDocument = doc.getDocument();
- CTBody body = ctDocument.getBody();
-
initFootnotes();
-
- // filling paragraph list
- for (CTP p : body.getPArray()) {
- paragraphs.add(new XWPFParagraph(p, this));
- }
-
- // Get any tables
- for(CTTbl table : body.getTblArray()) {
- tables.add(new XWPFTable(this, table));
+
+
+ // parse the document with cursor and add
+ // the XmlObject to its lists
+ XmlCursor cursor = ctDocument.getBody().newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP)o, this);
+ bodyElements.add(p);
+ paragraphs.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl)o, this);
+ bodyElements.add(t);
+ tables.add(t);
+ }
}
-
+
// Sort out headers and footers
if (doc.getDocument().getBody().getSectPr() != null)
headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
-
+
+ // Create for each XML-part in the Package a PartClass
for(POIXMLDocumentPart p : getRelations()){
String relation = p.getPackageRelationship().getRelationshipType();
- if(relation.equals(XWPFRelation.COMMENT.getRelation())){
+ if(relation.equals(XWPFRelation.STYLES.getRelation())){
+ this.styles = (XWPFStyles) p;
+ }
+ else if(relation.equals(XWPFRelation.NUMBERING.getRelation())){
+ this.numbering = (XWPFNumbering) p;
+
+ }
+ else if(relation.equals(XWPFRelation.FOOTER.getRelation())){
+ footers.add((XWPFFooter)p);
+ }
+ else if(relation.equals(XWPFRelation.HEADER.getRelation())){
+ headers.add((XWPFHeader)p);
+ }
+
+ else if(relation.equals(XWPFRelation.COMMENT.getRelation())){
CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream());
for(CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
comments.add(new XWPFComment(ctcomment));
@@ -155,12 +192,14 @@ public class XWPFDocument extends POIXML
} catch (XmlException e) {
throw new POIXMLException(e);
}
+ // create for every Graphic-Part in Package a new XWPFGraphic
+ getAllPictures();
}
private void initHyperlinks(){
// Get the hyperlinks
// TODO: make me optional/separated in private function
- try {
+ try {
Iterator <PackageRelationship> relIter =
getPackagePart().getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation()).iterator();
while(relIter.hasNext()) {
@@ -192,10 +231,10 @@ public class XWPFDocument extends POIXML
}
/**
- * Create a new SpreadsheetML package and setup the default minimal content
+ * Create a new WordProcessingML package and setup the default minimal content
*/
protected static OPCPackage newPackage() {
- try {
+ try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
@@ -237,21 +276,90 @@ public class XWPFDocument extends POIXML
public CTDocument1 getDocument() {
return ctDocument;
}
-
- public Iterator<XWPFParagraph> getParagraphsIterator() {
- return paragraphs.iterator();
+
+ /**
+ * returns an Iterator with paragraphs and tables
+ * @see org.apache.poi.xwpf.usermodel.IBody#getBodyElements()
+ * @return
+ */
+ public List<IBodyElement> getBodyElements(){
+ return Collections.unmodifiableList(bodyElements);
}
- public XWPFParagraph[] getParagraphs() {
- return paragraphs.toArray(
- new XWPFParagraph[paragraphs.size()]
- );
+
+ /**
+ * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphs()
+ */
+ public List<XWPFParagraph> getParagraphs(){
+ return Collections.unmodifiableList(paragraphs);
}
-
- public Iterator<XWPFTable> getTablesIterator()
- {
- return tables.iterator();
+
+ /**
+ * @see org.apache.poi.xwpf.usermodel.IBody#getTables()
+ * @return
+ */
+ public List<XWPFTable> getTables(){
+ return Collections.unmodifiableList(tables);
+ }
+
+ /**
+ * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
+ */
+ @Override
+ public XWPFTable getTableArray(int pos) {
+ if(pos > 0 && pos < tables.size()){
+ return tables.get(pos);
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public List<XWPFFooter> getFooterList(){
+ return Collections.unmodifiableList(footers);
+ }
+
+ /**
+ *
+ * @param pos
+ * @return
+ */
+ public XWPFFooter getFooterArray(int pos){
+ return footers.get(pos);
+ }
+
+ /**
+ *
+ * @return
+ */
+ public List<XWPFHeader> getHeaderList(){
+ return Collections.unmodifiableList(headers);
+ }
+
+ /**
+ *
+ * @param pos
+ * @return
+ */
+ public XWPFHeader getHeaderArray(int pos){
+ return headers.get(pos);
+ }
+
+ /**
+ *
+ * @param table
+ * @return
+ */
+ public String getTblStyle(XWPFTable table){
+ return table.getStyleID();
}
+ /**
+ *
+ * @param id
+ * @return
+ */
public XWPFHyperlink getHyperlinkByID(String id) {
Iterator<XWPFHyperlink> iter = hyperlinks.iterator();
while(iter.hasNext())
@@ -264,18 +372,36 @@ public class XWPFDocument extends POIXML
return null;
}
+ /**
+ *
+ * @param id
+ * @return
+ */
public XWPFFootnote getFootnoteByID(int id) {
return footnotes.get(id);
}
+ /**
+ *
+ * @param id
+ * @return
+ */
public XWPFFootnote getEndnoteByID(int id) {
return endnotes.get(id);
}
+ /**
+ *
+ * @return
+ */
public Collection<XWPFFootnote> getFootnotes() {
- return footnotes == null ? new ArrayList<XWPFFootnote>() : footnotes.values();
+ return Collections.unmodifiableCollection(footnotes == null ? new ArrayList<XWPFFootnote>() : footnotes.values());
}
+ /**
+ *
+ * @return
+ */
public XWPFHyperlink[] getHyperlinks() {
return hyperlinks.toArray(
new XWPFHyperlink[hyperlinks.size()]
@@ -356,7 +482,198 @@ public class XWPFDocument extends POIXML
return embedds;
}
+
+ /**
+ * get with the position of a Paragraph in the bodyelement array list
+ * the position of this paragraph in the paragraph array list
+ * @param pos position of the paragraph in the bodyelement array list
+ * @return if there is a paragraph at the position in the bodyelement array list,
+ * else it will return -1
+ *
+ */
+ public int getParagraphPos(int pos){
+ if(pos >= 0 && pos < bodyElements.size()){
+ if(bodyElements.get(pos).getElementType() == BodyElementType.PARAGRAPH){
+ int startPos;
+ //find the startpoint for searching
+ if(pos < paragraphs.size()){
+ startPos = pos;
+ }
+ else{
+ startPos = (paragraphs.size());
+ }
+ for(int i = startPos; i < 0; i--){
+ if(paragraphs.get(i) == bodyElements.get(pos))
+ return i;
+ }
+ }
+ }
+ if(paragraphs.size() == 0){
+ return 0;
+ }
+ return -1;
+ }
+
+ /**
+ * get with the position of a table in the bodyelement array list
+ * the position of this table in the table array list
+ * @param pos position of the table in the bodyelement array list
+ * @return if there is a table at the position in the bodyelement array list,
+ * else it will return null.
+ */
+ public int getTablePos(int pos){
+ if(pos >= 0 && pos < bodyElements.size()){
+ if(bodyElements.get(pos).getElementType() == BodyElementType.TABLE){
+ int startPos;
+ //find the startpoint for searching
+ if(pos < tables.size()){
+ startPos = pos;
+ }
+ else{
+ startPos = (tables.size());
+ }
+ for(int i = startPos; i > 0; i--){
+ if(tables.get(i) == bodyElements.get(pos))
+ return i;
+ }
+ }
+ }
+ if(tables.size() == 0){
+ return 0;
+ }
+ else
+ return -1;
+ }
+
+ /**
+ * add a new paragraph at position of the cursor
+ * @param cursor
+ * @return
+ */
+ public XWPFParagraph insertNewParagraph(XmlCursor cursor){
+ if(isCursorInBody(cursor)){
+ String uri = CTP.type.getName().getNamespaceURI();
+ String localPart = "p";
+ cursor.beginElement(localPart,uri);
+ cursor.toParent();
+ CTP p = (CTP)cursor.getObject();
+ XWPFParagraph newP = new XWPFParagraph(p, this);
+ XmlObject o = null;
+ while(!(o instanceof CTP)&&(cursor.toPrevSibling())){
+ o = cursor.getObject();
+ }
+ if((!(o instanceof CTP)) || (CTP)o == p){
+ paragraphs.add(0, newP);
+ }
+ else{
+ int pos = paragraphs.indexOf(getParagraph((CTP)o))+1;
+ paragraphs.add(pos,newP);
+ }
+ int i=0;
+ cursor.toCursor(p.newCursor());
+ while(cursor.toPrevSibling()){
+ o =cursor.getObject();
+ if(o instanceof CTP || o instanceof CTTbl)
+ i++;
+ }
+ bodyElements.add(i, newP);
+ cursor.toCursor(p.newCursor());
+ cursor.toEndToken();
+ return newP;
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param cursor
+ * @return
+ */
+ public XWPFTable insertNewTbl(XmlCursor cursor) {
+ if(isCursorInBody(cursor)){
+ String uri = CTTbl.type.getName().getNamespaceURI();
+ String localPart ="tbl";
+ cursor.beginElement(localPart,uri);
+ cursor.toParent();
+ CTTbl t = (CTTbl)cursor.getObject();
+ XWPFTable newT = new XWPFTable(t, this);
+ cursor.removeXmlContents();
+ XmlObject o = null;
+ while(!(o instanceof CTTbl)&&(cursor.toPrevSibling())){
+ o = cursor.getObject();
+ }
+ if(!(o instanceof CTTbl)){
+ tables.add(0, newT);
+ }
+ else{
+ int pos = tables.indexOf(getTable((CTTbl)o))+1;
+ tables.add(pos,newT);
+ }
+ int i=0;
+ cursor = t.newCursor();
+ while(cursor.toPrevSibling()){
+ o =cursor.getObject();
+ if(o instanceof CTP || o instanceof CTTbl)
+ i++;
+ }
+ bodyElements.add(i, newT);
+ cursor = t.newCursor();
+ cursor.toEndToken();
+ return newT;
+ }
+ return null;
+ }
+
+ /**
+ * verifies that cursor is on the right position
+ * @param cursor
+ * @return
+ */
+ private boolean isCursorInBody(XmlCursor cursor) {
+ XmlCursor verify = cursor.newCursor();
+ verify.toParent();
+ if(verify.getObject() == this.ctDocument.getBody()){
+ return true;
+ }
+ XmlObject o = verify.getObject();
+ return false;
+
+ }
+
+ /**
+ * get position of the paragraph
+ * @param p
+ * @return
+ */
+ public Integer getPosOfParagraph(XWPFParagraph p){
+ int i, pos = 0;
+ for (i = 0 ; i < bodyElements.size() ; i++) {
+ if (bodyElements.get(i) instanceof XWPFParagraph){
+ if (bodyElements.get(i).equals(p)){
+ return pos;
+ }
+ pos++;
+ }
+ }
+ return null;
+ }
+
+ public Integer getPosOfTable(XWPFTable t){
+ int i, pos = 0;
+ for(i = 0; i < bodyElements.size(); i++){
+ if(bodyElements.get(i).getElementType() == BodyElementType.TABLE){
+ if (bodyElements.get(i) == t){
+ return pos;
+ }
+ pos++;
+ }
+ }
+ return null;
+ }
+ /**
+ * commit and saves the document
+ */
@Override
protected void commit() throws IOException {
@@ -382,20 +699,62 @@ public class XWPFDocument extends POIXML
/**
* Appends a new paragraph to this document
- *
* @return a new paragraph
*/
public XWPFParagraph createParagraph(){
return new XWPFParagraph(ctDocument.getBody().addNewP(), this);
}
+
+ /**
+ * remove a BodyElement from bodyElements array list
+ * @param pos
+ * @return true if removing was successfully, else return false
+ */
+ public boolean removeBodyElement(int pos){
+ if(pos >= 0 && pos < bodyElements.size()){
+ if(bodyElements.get(pos).getElementType() == BodyElementType.TABLE){
+ bodyElements.remove(pos);
+ Integer tablePos = getTablePos(pos);
+ tables.remove(tablePos);
+ ctDocument.getBody().removeTbl(tablePos);
+ return true;
+ }
+ if(bodyElements.get(pos).getElementType() == BodyElementType.PARAGRAPH){
+ bodyElements.remove(pos);
+ Integer paraPos = getParagraphPos(pos);
+ paragraphs.remove(paraPos);
+ ctDocument.getBody().removeP(paraPos);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * copies content of a paragraph to a existing paragraph in the list paragraphs at position pos
+ * @param paragraph
+ * @param pos
+ */
+ public void setParagraph(XWPFParagraph paragraph, int pos){
+ paragraphs.set(pos, paragraph);
+ ctDocument.getBody().setPArray(pos, paragraph.getCTP());
+ }
+
+ /**
+ * get the LastParagraph of the document
+ * @return
+ */
+ public XWPFParagraph getLastParagraph(){
+ int lastPos = paragraphs.toArray().length - 1;
+ return paragraphs.get(lastPos);
+ }
/**
* Create an empty table with one row and one column as default.
- *
* @return a new table
*/
public XWPFTable createTable(){
- return new XWPFTable(this, ctDocument.getBody().addNewTbl());
+ return new XWPFTable(ctDocument.getBody().addNewTbl(), this);
}
/**
@@ -405,15 +764,17 @@ public class XWPFDocument extends POIXML
* @return table
*/
public XWPFTable createTable(int rows, int cols) {
- return new XWPFTable(this, ctDocument.getBody().addNewTbl(), rows, cols);
+ return new XWPFTable(ctDocument.getBody().addNewTbl(), this, rows, cols);
}
-
+
+
+ /**
+ *
+ */
public void createTOC() {
CTSdtBlock block = this.getDocument().getBody().addNewSdt();
TOC toc = new TOC(block);
- int i = 1;
- for (Iterator<XWPFParagraph> iterator = getParagraphsIterator() ; iterator.hasNext() ; ) {
- XWPFParagraph par = iterator.next();
+ for (XWPFParagraph par: paragraphs ) {
String parStyle = par.getStyle();
if (parStyle != null && parStyle.substring(0, 7).equals("Heading")) {
try {
@@ -426,7 +787,15 @@ public class XWPFDocument extends POIXML
}
}
}
-
+
+ /**Replace content of table in array tables at position pos with a
+ * @param pos, table
+ */
+ public void setTable(int pos, XWPFTable table){
+ tables.set(pos, table);
+ ctDocument.getBody().setTblArray(pos, table.getCTTbl());
+ }
+
/**
* Verifies that the documentProtection tag in settings.xml file <br/>
* specifies that the protection is enforced (w:enforcement="1") <br/>
@@ -568,4 +937,292 @@ public class XWPFDocument extends POIXML
settings.removeEnforcement();
}
-}
+ /**
+ * inserts an existing XWPFTable to the arrays bodyElements and tables
+ * @param i
+ * @param table
+ */
+ @Override
+ public void insertTable(int pos, XWPFTable table) {
+ bodyElements.add(pos, table);
+ int i;
+ for (i = 0; i < ctDocument.getBody().getTblArray().length; i++) {
+ CTTbl tbl = ctDocument.getBody().getTblArray(i);
+ if(tbl == table.getCTTbl()){
+ break;
+ }
+ }
+ tables.add(i, table);
+ }
+
+ /**
+ *
+ * @return
+ */
+ public List<XWPFPictureData> getAllPictures() {
+ if(pictures == null){
+ pictures = new ArrayList<XWPFPictureData>();
+ for (POIXMLDocumentPart poixmlDocumentPart : getRelations()){
+ if(poixmlDocumentPart instanceof XWPFPictureData){
+ pictures.add((XWPFPictureData)poixmlDocumentPart);
+ }
+ }
+ }
+ return pictures;
+ }
+
+ /**
+ * get all Pictures in this package
+ * @return
+ */
+ public List<XWPFPictureData> getAllPackagePictures(){
+ List<XWPFPictureData> pkgpictures = new ArrayList<XWPFPictureData>();
+ pkgpictures.addAll(getAllPictures());
+ for (POIXMLDocumentPart poixmlDocumentPart : getRelations()){
+ if(poixmlDocumentPart instanceof XWPFHeaderFooter){
+ pkgpictures.addAll(((XWPFHeaderFooter)poixmlDocumentPart).getAllPictures());
+ }
+ }
+ return pkgpictures;
+ }
+
+ /**
+ * Adds a picture to the document.
+ *
+ * @param is The stream to read image from
+ * @param format The format of the picture.
+ *
+ * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
+ * @throws InvalidFormatException
+ */
+ public int addPicture(InputStream is, int format) throws IOException, InvalidFormatException {
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, true);
+ OutputStream out = img.getPackagePart().getOutputStream();
+ IOUtils.copy(is, out);
+ out.close();
+ pictures.add(img);
+ return getAllPictures().size()-1;
+
+ }
+
+ /**
+ * Adds a picture to the document.
+ *
+ * @param is The stream to read image from
+ * @param format The format of the picture.
+ *
+ * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
+ * @throws InvalidFormatException
+ */
+ public int addPicture(byte[] pictureData, int format) throws InvalidFormatException {
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ try {
+ OutputStream out = img.getPackagePart().getOutputStream();
+ out.write(pictureData);
+ out.close();
+ } catch (IOException e){
+ throw new POIXMLException(e);
+ }
+
+ pictures.add(img);
+ return getAllPictures().size()-1;
+ }
+
+ /**
+ * get the next free ImageNumber
+ * @param format
+ * @return
+ * @throws InvalidFormatException
+ */
+ public int getNextPicNameNumber(int format) throws InvalidFormatException{
+ int img = getAllPackagePictures().size()+1;
+ String proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+ PackagePartName createPartName = PackagingURIHelper.createPartName(proposal);
+ while (this.getPackage().getPart(createPartName)!= null){
+ img++;
+ proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+ createPartName = PackagingURIHelper.createPartName(proposal);
+ }
+ return img;
+ }
+
+ /**
+ * returns the PictureData by blipID
+ * @param blipID
+ * @return XWPFPictureData of a specificID
+ * @throws Exception
+ */
+ public XWPFPictureData getPictureDataByID(String blipID) {
+ for(POIXMLDocumentPart part: getRelations()){
+ if(part.getPackageRelationship() != null){
+ if(part.getPackageRelationship().getId() != null){
+ if(part.getPackageRelationship().getId().equals(blipID)){
+ return (XWPFPictureData)part;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Add the picture to drawing relations
+ *
+ * @param img the PictureData of the Picture,
+ * @throws InvalidFormatException
+ */
+ public PackageRelationship addPictureReference(byte[] pictureData, int format) throws InvalidFormatException{
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ PackageRelationship rel = null;
+ try {
+ OutputStream out = img.getPackagePart().getOutputStream();
+ out.write(pictureData);
+ out.close();
+ rel = img.getPackageRelationship();
+ pictures.add(img);
+ } catch (IOException e){
+ throw new POIXMLException(e);
+ }
+ return rel;
+ }
+
+ /**
+ * Add the picture to drawing relations
+ *
+ * @param img the PictureData of the Picture,
+ * @throws InvalidFormatException
+ * @throws IOException
+ */
+ public PackageRelationship addPictureReference(InputStream is, int format) throws InvalidFormatException, IOException{
+
+ PackageRelationship rel = null;
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ OutputStream out = img.getPackagePart().getOutputStream();
+ IOUtils.copy(is, out);
+ out.close();
+ rel = img.getPackageRelationship();
+ pictures.add(img);
+ return rel;
+ }
+
+ /**
+ * getNumbering
+ * @return numbering
+ */
+ public XWPFNumbering getNumbering(){
+ return numbering;
+ }
+
+ /**
+ * get Styles
+ * @return
+ */
+ public XWPFStyles getStyles(){
+ return styles;
+ }
+
+ /**
+ * get the paragraph with the CTP class p
+ * @param p
+ * @return
+ */
+ public XWPFParagraph getParagraph(CTP p){
+ for(int i=0; i<getParagraphs().size(); i++){
+ if(getParagraphs().get(i).getCTP() == p) return getParagraphs().get(i);
+ }
+ return null;
+ }
+
+ /**
+ * get a table by its CTTbl-Object
+ * @param ctTbl
+ * @see org.apache.poi.xwpf.usermodel.IBody#getTable(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)
+ * @return
+ */
+ public XWPFTable getTable(CTTbl ctTbl) {
+ for(int i=0; i<tables.size(); i++){
+ if(getTables().get(i).getCTTbl() == ctTbl) return getTables().get(i);
+ }
+ return null;
+ }
+
+
+ public Iterator<XWPFTable> getTablesIterator() {
+ return tables.iterator();
+ }
+
+ public Iterator<XWPFParagraph> getParagraphsIterator() {
+ return paragraphs.iterator();
+ }
+
+ /**
+ * Returns the paragraph that of position pos
+ * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int)
+ */
+ @Override
+ public XWPFParagraph getParagraphArray(int pos) {
+ if(pos > 0 && pos < paragraphs.size()){
+ return paragraphs.get(pos);
+ }
+ return null;
+ }
+
+
+
+ /**
+ * returns the Part, to which the body belongs, which you need for adding relationship to other parts
+ * Actually it is needed of the class XWPFTableCell. Because you have to know to which part the tableCell
+ * belongs.
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+ */
+ @Override
+ public IBody getPart() {
+ return this;
+ }
+
+ /**
+ * get the PartType of the body, for example
+ * DOCUMENT, HEADER, FOOTER, FOOTNOTE,
+ * @return
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
+ */
+ @Override
+ public BodyType getPartType() {
+ return BodyType.DOCUMENT;
+ }
+
+ /**
+ * get the TableCell which belongs to the TableCell
+ * @param o
+ * @return
+ */
+ @Override
+ public XWPFTableCell getTableCell(CTTc cell) {
+ XmlCursor cursor = cell.newCursor();
+ cursor.toParent();
+ XmlObject o = cursor.getObject();
+ if(!(o instanceof CTRow)){
+ return null;
+ }
+ CTRow row = (CTRow)o;
+ cursor.toParent();
+ o = cursor.getObject();
+ if(! (o instanceof CTTbl)){
+ return null;
+ }
+ CTTbl tbl = (CTTbl) o;
+ XWPFTable table = getTable(tbl);
+ if(table == null){
+ return null;
+ }
+ XWPFTableRow tableRow = table.getRow(row);
+ if(row == null){
+ return null;
+ }
+ return tableRow.getTableCell(cell);
+ }
+}//end class
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java Fri Jun 11 14:37:58 2010
@@ -17,10 +17,25 @@
package org.apache.poi.xwpf.usermodel;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
/**
* Sketch of XWPF footer class
@@ -29,12 +44,112 @@ public class XWPFFooter extends XWPFHead
public XWPFFooter() {
super();
}
- public XWPFFooter(CTHdrFtr hdrFtr) {
+
+
+ public XWPFFooter(CTHdrFtr hdrFtr) throws IOException {
super(hdrFtr);
+ bodyElements = new ArrayList<IBodyElement>();
+ paragraphs = new ArrayList<XWPFParagraph>();
+ tables = new ArrayList<XWPFTable>();
+ XmlCursor cursor = headerFooter.newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP)o, this);
+ paragraphs.add(p);
+ bodyElements.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl)o, this);
+ tables.add(t);
+ bodyElements.add(t);
+ }
+ }
+ getAllPictures();
}
public XWPFFooter(PackagePart part, PackageRelationship rel) throws IOException {
super(part, rel);
}
+ /**
+ * save and commit footer
+ */
+ @Override
+ protected void commit() throws IOException {
+ XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+ xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "ftr"));
+ Map map = new HashMap();
+ map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve");
+ map.put("urn:schemas-microsoft-com:office:office", "o");
+ map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
+ map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m");
+ map.put("urn:schemas-microsoft-com:vml", "v");
+ map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp");
+ map.put("urn:schemas-microsoft-com:office:word", "w10");
+ map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
+ map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
+ xmlOptions.setSaveSuggestedPrefixes(map);
+ PackagePart part = getPackagePart();
+ OutputStream out = part.getOutputStream();
+ super._getHdrFtr().save(out, xmlOptions);
+ out.close();
+ }
+
+ @Override
+ protected void onDocumentRead(){
+ bodyElements = new ArrayList<IBodyElement>();
+ paragraphs = new ArrayList<XWPFParagraph>();
+ tables= new ArrayList<XWPFTable>();
+ FtrDocument ftrDocument = null;
+ InputStream is;
+ try {
+ is = getPackagePart().getInputStream();
+ ftrDocument = FtrDocument.Factory.parse(is);
+ headerFooter = ftrDocument.getFtr();
+ // parse the document with cursor and add
+ // the XmlObject to its lists
+ XmlCursor cursor = headerFooter.newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP)o, this);
+ paragraphs.add(p);
+ bodyElements.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl)o, this);
+ tables.add(t);
+ bodyElements.add(t);
+ }
+ }
+ getAllPictures();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (XmlException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ /**
+ * returns the Part, to which the body belongs, which you need for adding relationship to other parts
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+ */
+ @Override
+ public IBody getPart() {
+ return this;
+ }
+
+ /**
+ * get the PartType of the body
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
+ */
+ @Override
+ public BodyType getPartType() {
+ return BodyType.FOOTER;
+ }
+
}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java Fri Jun 11 14:37:58 2010
@@ -16,12 +16,12 @@
==================================================================== */
package org.apache.poi.xwpf.usermodel;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
-
-import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
+
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
public class XWPFFootnote implements Iterable<XWPFParagraph> {
private List<XWPFParagraph> paragraphs = new ArrayList<XWPFParagraph>();
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java Fri Jun 11 14:37:58 2010
@@ -17,24 +17,149 @@
package org.apache.poi.xwpf.usermodel;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import javax.xml.namespace.QName;
+
+import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
/**
* Sketch of XWPF header class
*/
public class XWPFHeader extends XWPFHeaderFooter {
+
public XWPFHeader() {
super();
}
- public XWPFHeader(CTHdrFtr hdrFtr) {
+
+ public XWPFHeader(PackagePart part, PackageRelationship rel) throws IOException {
+ super(part, rel);
+ }
+
+ public XWPFHeader(CTHdrFtr hdrFtr) throws IOException {
super(hdrFtr);
+ paragraphs = new ArrayList<XWPFParagraph>();
+ tables = new ArrayList<XWPFTable>();
+ XmlCursor cursor = headerFooter.newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP) o, this);
+ paragraphs.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl) o, this);
+ tables.add(t);
+ }
+ }
+ getAllPictures();
}
- public XWPFHeader(PackagePart part, PackageRelationship rel) throws IOException {
+ /**
+ public XWPFHeader(PackagePart part, PackageRelationship rel)
+ throws IOException {
super(part, rel);
}
-}
+ /**
+ * save and commit footer
+ */
+ @Override
+ protected void commit() throws IOException {
+ XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+ xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "hdr"));
+ Map map = new HashMap();
+ map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve");
+ map.put("urn:schemas-microsoft-com:office:office", "o");
+ map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
+ map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m");
+ map.put("urn:schemas-microsoft-com:vml", "v");
+ map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp");
+ map.put("urn:schemas-microsoft-com:office:word", "w10");
+ map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
+ map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
+ xmlOptions.setSaveSuggestedPrefixes(map);
+ PackagePart part = getPackagePart();
+ OutputStream out = part.getOutputStream();
+ super._getHdrFtr().save(out, xmlOptions);
+ out.close();
+ }
+
+ /**
+ * reads the document
+ * @overide onDocumentRead of class {@link POIXMLDocumentPart}
+ */
+ @Override
+ protected void onDocumentRead(){
+ bodyElements = new ArrayList<IBodyElement>();
+ paragraphs = new ArrayList<XWPFParagraph>();
+ tables= new ArrayList<XWPFTable>();
+ HdrDocument hdrDocument = null;
+ InputStream is;
+ try {
+ is = getPackagePart().getInputStream();
+ hdrDocument = HdrDocument.Factory.parse(is);
+ headerFooter = hdrDocument.getHdr();
+ // parse the document with cursor and add
+ // the XmlObject to its lists
+ XmlCursor cursor = headerFooter.newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP)o, this);
+ paragraphs.add(p);
+ bodyElements.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl)o, this);
+ tables.add(t);
+ bodyElements.add(t);
+ }
+ }
+ getAllPictures();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (XmlException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * returns the Part, to which the body belongs, which you need for adding relationship to other parts
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+ */
+ @Override
+ public IBody getPart() {
+ return this;
+ }
+
+ /**
+ * get the PartType of the body
+ * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
+ */
+ @Override
+ public BodyType getPartType() {
+ return BodyType.HEADER;
+ }
+
+
+}//end class
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java?rev=953704&r1=953703&r2=953704&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java Fri Jun 11 14:37:58 2010
@@ -17,28 +17,52 @@
package org.apache.poi.xwpf.usermodel;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import org.apache.poi.POIXMLDocumentPart;
-import org.apache.poi.util.Internal;
+import org.apache.poi.POIXMLException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
/**
* Parent of XWPF headers and footers
*/
-public abstract class XWPFHeaderFooter extends POIXMLDocumentPart{
+public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBody{
protected CTHdrFtr headerFooter;
+ protected List<XWPFParagraph> paragraphs;
+ protected List<XWPFTable> tables;
+ protected List<XWPFPictureData> pictures;
+ protected XWPFDocument document;
+ protected List<IBodyElement> bodyElements;
- protected XWPFHeaderFooter(CTHdrFtr hdrFtr) {
+ protected XWPFHeaderFooter(CTHdrFtr hdrFtr){
headerFooter = hdrFtr;
+ readHdrFtr();
}
protected XWPFHeaderFooter() {
- headerFooter = CTHdrFtr.Factory.newInstance();
+ this(CTHdrFtr.Factory.newInstance());
}
public XWPFHeaderFooter(PackagePart part, PackageRelationship rel) throws IOException {
super(part, rel);
+ this.document = (XWPFDocument)getParent();
+ onDocumentRead();
}
@Internal
@@ -53,16 +77,11 @@ public abstract class XWPFHeaderFooter e
* there could be more in certain cases, or
* a table.
*/
- public XWPFParagraph[] getParagraphs() {
- XWPFParagraph[] paras =
- new XWPFParagraph[headerFooter.getPArray().length];
- for(int i=0; i<paras.length; i++) {
- paras[i] = new XWPFParagraph(
- headerFooter.getPArray(i), null
- );
- }
- return paras;
- }
+ public List<XWPFParagraph> getParagraphs() {
+ return Collections.unmodifiableList(paragraphs);
+ }
+
+
/**
* Return the table(s) that holds the text
* of the header or footer, for complex cases
@@ -71,17 +90,11 @@ public abstract class XWPFHeaderFooter e
* complex headers/footers have a table or two
* in addition.
*/
- public XWPFTable[] getTables() {
- XWPFTable[] tables =
- new XWPFTable[headerFooter.getTblArray().length];
- for(int i=0; i<tables.length; i++) {
- tables[i] = new XWPFTable(
- null,
- headerFooter.getTblArray(i)
- );
- }
- return tables;
- }
+ public List<XWPFTable> getTables()throws ArrayIndexOutOfBoundsException {
+ return Collections.unmodifiableList(tables);
+ }
+
+
/**
* Returns the textual content of the header/footer,
@@ -90,10 +103,9 @@ public abstract class XWPFHeaderFooter e
public String getText() {
StringBuffer t = new StringBuffer();
- XWPFParagraph[] paras = getParagraphs();
- for(int i=0; i<paras.length; i++) {
- if(! paras[i].isEmpty()) {
- String text = paras[i].getText();
+ for(int i=0; i<paragraphs.size(); i++) {
+ if(! paragraphs.get(i).isEmpty()) {
+ String text = paragraphs.get(i).getText();
if(text != null && text.length() > 0) {
t.append(text);
t.append('\n');
@@ -101,9 +113,9 @@ public abstract class XWPFHeaderFooter e
}
}
- XWPFTable[] tables = getTables();
- for(int i=0; i<tables.length; i++) {
- String text = tables[i].getText();
+ List<XWPFTable> tables = getTables();
+ for(int i=0; i<tables.size(); i++) {
+ String text = tables.get(i).getText();
if(text != null && text.length() > 0) {
t.append(text);
t.append('\n');
@@ -112,4 +124,404 @@ public abstract class XWPFHeaderFooter e
return t.toString();
}
-}
+
+ /**
+ * set a new headerFooter
+ */
+ public void setHeaderFooter(CTHdrFtr headerFooter){
+ this.headerFooter = headerFooter;
+ readHdrFtr();
+ }
+
+ /**
+ * if there is a corresponding {@link XWPFTable} of the parameter ctTable in the tableList of this header
+ * the method will return this table
+ * if there is no corresponding {@link XWPFTable} the method will return null
+ * @param ctTable
+ * @return
+ */
+ public XWPFTable getTable(CTTbl ctTable){
+ for (XWPFTable table : tables) {
+ if(table==null)
+ return null;
+ if(table.getCTTbl().equals(ctTable))
+ return table;
+ }
+ return null;
+ }
+
+ /**
+ * if there is a corresponding {@link XWPFParagraph} of the parameter ctTable in the paragraphList of this header or footer
+ * the method will return this paragraph
+ * if there is no corresponding {@link XWPFParagraph} the method will return null
+ * @param p is instance of CTP and is searching for an XWPFParagraph
+ * @return null if there is no XWPFParagraph with an corresponding CTPparagraph in the paragraphList of this header or footer
+ * XWPFParagraph with the correspondig CTP p
+ */
+ public XWPFParagraph getParagraph(CTP p){
+ for (XWPFParagraph paragraph : paragraphs) {
+ if(paragraph.getCTP().equals(p))
+ return paragraph;
+ }
+ return null;
+
+ }
+
+ /**
+ * Returns the paragraph that holds
+ * the text of the header or footer.
+ */
+ public XWPFParagraph getParagraphArray(int pos) {
+
+ return paragraphs.get(pos);
+ }
+
+ /**
+ * get a List of all Paragraphs
+ * @return a list of {@link XWPFParagraph}
+ */
+ public List<XWPFParagraph> getListParagraph(){
+ return paragraphs;
+ }
+
+ public List<XWPFPictureData> getAllPictures() {
+ if(pictures == null){
+ pictures = new ArrayList<XWPFPictureData>();
+ for (POIXMLDocumentPart poixmlDocumentPart : getRelations()){
+ if(poixmlDocumentPart instanceof XWPFPictureData){
+ pictures.add((XWPFPictureData)poixmlDocumentPart);
+ }
+ }
+ }
+ return pictures;
+ }
+
+ /**
+ * get all Pictures in this package
+ * @return
+ */
+ public List<XWPFPictureData> getAllPackagePictures(){
+ List<XWPFPictureData> pkgpictures = new ArrayList<XWPFPictureData>();
+ pkgpictures.addAll(getAllPictures());
+ for (POIXMLDocumentPart poixmlDocumentPart : getRelations()){
+ if(poixmlDocumentPart instanceof XWPFHeaderFooter){
+ pkgpictures.addAll(((XWPFHeaderFooter)poixmlDocumentPart).getAllPictures());
+ }
+ }
+ return pkgpictures;
+ }
+
+ /**
+ * Adds a picture to the document.
+ *
+ * @param is The stream to read image from
+ * @param format The format of the picture.
+ *
+ * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
+ */
+ public int addPicture(InputStream is, int format) throws IOException {
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, true);
+ OutputStream out = img.getPackagePart().getOutputStream();
+ IOUtils.copy(is, out);
+ out.close();
+ pictures.add(img);
+ return getAllPictures().size()-1;
+
+ }
+
+ /**
+ * Adds a picture to the document.
+ *
+ * @param is The stream to read image from
+ * @param format The format of the picture.
+ *
+ * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
+ */
+ public int addPicture(byte[] pictureData, int format) {
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ try {
+ OutputStream out = img.getPackagePart().getOutputStream();
+ out.write(pictureData);
+ out.close();
+ } catch (IOException e){
+ throw new POIXMLException(e);
+ }
+
+ pictures.add(img);
+ return getAllPictures().size()-1;
+ }
+
+ /**
+ * get the next free ImageNumber
+ * @param format
+ * @return
+ */
+ public int getNextPicNameNumber(int format){
+ int img = getAllPackagePictures().size()+1;
+ String proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+ try {
+ PackagePartName createPartName = PackagingURIHelper.createPartName(proposal);
+ while (this.getPackagePart().getPackage().getPart(createPartName)!= null){
+ img++;
+ proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+ createPartName = PackagingURIHelper.createPartName(proposal);
+ }
+ } catch (InvalidFormatException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return img;
+ }
+
+ /**
+ * returns the PictureData by blipID
+ * @param blipID
+ * @return XWPFPictureData of a specificID
+ * @throws Exception
+ */
+ public XWPFPictureData getPictureDataByID(String blipID) {
+ for(POIXMLDocumentPart part: getRelations()){
+ if(part.getPackageRelationship() != null){
+ if(part.getPackageRelationship().getId() != null){
+ if(part.getPackageRelationship().getId().equals(blipID)){
+ return (XWPFPictureData)part;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Add the picture to drawing relations
+ *
+ * @param img the PictureData of the Picture,
+ */
+ public PackageRelationship addPictureReference(byte[] pictureData, int format){
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ PackageRelationship rel = null;
+ try {
+ OutputStream out = img.getPackagePart().getOutputStream();
+ out.write(pictureData);
+ out.close();
+ rel = img.getPackageRelationship();
+ pictures.add(img);
+ } catch (IOException e){
+ throw new POIXMLException(e);
+ }
+ return rel;
+ }
+
+ /**
+ * Add the picture to drawing relations
+ *
+ * @param img the PictureData of the Picture,
+ */
+ public PackageRelationship addPictureReference(InputStream is, int format){
+
+ PackageRelationship rel = null;
+ try {
+ int imageNumber = getNextPicNameNumber(format);
+ XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
+ OutputStream out = img.getPackagePart().getOutputStream();
+ IOUtils.copy(is, out);
+ out.close();
+ rel = img.getPackageRelationship();
+ pictures.add(img);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return rel;
+ }
+
+ /**
+ * add a new paragraph at position of the cursor
+ * @param cursor
+ * @return
+ */
+ public XWPFParagraph insertNewParagraph(XmlCursor cursor){
+ if(isCursorInHdrF(cursor)){
+ String uri = CTP.type.getName().getNamespaceURI();
+ String localPart = "p";
+ cursor.beginElement(localPart,uri);
+ cursor.toParent();
+ CTP p = (CTP)cursor.getObject();
+ XWPFParagraph newP = new XWPFParagraph(p, this);
+ XmlObject o = null;
+ while(!(o instanceof CTP)&&(cursor.toPrevSibling())){
+ o = cursor.getObject();
+ }
+ if((!(o instanceof CTP)) || (CTP)o == p){
+ paragraphs.add(0, newP);
+ }
+ else{
+ int pos = paragraphs.indexOf(getParagraph((CTP)o))+1;
+ paragraphs.add(pos,newP);
+ }
+ int i=0;
+ cursor.toCursor(p.newCursor());
+ while(cursor.toPrevSibling()){
+ o =cursor.getObject();
+ if(o instanceof CTP || o instanceof CTTbl)
+ i++;
+ }
+ bodyElements.add(i, newP);
+ cursor.toCursor(p.newCursor());
+ cursor.toEndToken();
+ return newP;
+ }
+ return null;
+ }
+
+
+ /**
+ *
+ * @param cursor
+ * @return
+ */
+ public XWPFTable insertNewTbl(XmlCursor cursor) {
+ if(isCursorInHdrF(cursor)){
+ String uri = CTTbl.type.getName().getNamespaceURI();
+ String localPart = "tbl";
+ cursor.beginElement(localPart,uri);
+ cursor.toParent();
+ CTTbl t = (CTTbl)cursor.getObject();
+ XWPFTable newT = new XWPFTable(t, this);
+ cursor.removeXmlContents();
+ XmlObject o = null;
+ while(!(o instanceof CTTbl)&&(cursor.toPrevSibling())){
+ o = cursor.getObject();
+ }
+ if(!(o instanceof CTTbl)){
+ tables.add(0, newT);
+ }
+ else{
+ int pos = tables.indexOf(getTable((CTTbl)o))+1;
+ tables.add(pos,newT);
+ }
+ int i=0;
+ cursor = t.newCursor();
+ while(cursor.toPrevSibling()){
+ o =cursor.getObject();
+ if(o instanceof CTP || o instanceof CTTbl)
+ i++;
+ }
+ bodyElements.add(i, newT);
+ cursor = t.newCursor();
+ cursor.toEndToken();
+ return newT;
+ }
+ return null;
+ }
+
+ /**
+ * verifies that cursor is on the right position
+ * @param cursor
+ * @return
+ */
+ private boolean isCursorInHdrF(XmlCursor cursor) {
+ XmlCursor verify = cursor.newCursor();
+ verify.toParent();
+ if(verify.getObject() == this.headerFooter){
+ return true;
+ }
+ return false;
+ }
+
+
+ public POIXMLDocumentPart getOwner(){
+ return this;
+ }
+
+ /**
+ * Returns the table at position pos
+ * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
+ */
+ @Override
+ public XWPFTable getTableArray(int pos) {
+
+ if(pos > 0 && pos < tables.size()){
+ return tables.get(pos);
+ }
+ return null;
+ }
+
+ /**
+ * inserts an existing XWPFTable to the arrays bodyElements and tables
+ * @param i
+ * @param table
+ */
+ @Override
+ public void insertTable(int pos, XWPFTable table) {
+ bodyElements.add(pos, table);
+ int i;
+ for (i = 0; i < headerFooter.getTblArray().length; i++) {
+ CTTbl tbl = headerFooter.getTblArray(i);
+ if(tbl == table.getCTTbl()){
+ break;
+ }
+ }
+ tables.add(i, table);
+
+ }
+
+ public void readHdrFtr(){
+ bodyElements = new ArrayList<IBodyElement>();
+ paragraphs = new ArrayList<XWPFParagraph>();
+ tables= new ArrayList<XWPFTable>();
+ // parse the document with cursor and add
+ // the XmlObject to its lists
+ XmlCursor cursor = headerFooter.newCursor();
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP)o, this);
+ paragraphs.add(p);
+ bodyElements.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl)o, this);
+ tables.add(t);
+ bodyElements.add(t);
+ }
+ }
+ getAllPictures();
+ }
+
+ /**
+ * get the TableCell which belongs to the TableCell
+ * @param o
+ * @return
+ */
+ public XWPFTableCell getTableCell(CTTc cell) {
+ XmlCursor cursor = cell.newCursor();
+ cursor.toParent();
+ XmlObject o = cursor.getObject();
+ if(!(o instanceof CTRow)){
+ return null;
+ }
+ CTRow row = (CTRow)o;
+ cursor.toParent();
+ o = cursor.getObject();
+ if(! (o instanceof CTTbl)){
+ return null;
+ }
+ CTTbl tbl = (CTTbl) o;
+ XWPFTable table = getTable(tbl);
+ if(table == null){
+ return null;
+ }
+ XWPFTableRow tableRow = table.getRow(row);
+ if(row == null){
+ return null;
+ }
+ return tableRow.getTableCell(cell);
+ }
+
+
+}//end class
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java?rev=953704&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFLatentStyles.java Fri Jun 11 14:37:58 2010
@@ -0,0 +1,48 @@
+/* ====================================================================
+ 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.CTLatentStyles;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
+
+public class XWPFLatentStyles {
+ private CTLatentStyles latentStyles;
+ protected XWPFStyles styles; //LatentStyle shall know styles
+
+ protected XWPFLatentStyles(){
+ }
+
+ protected XWPFLatentStyles(CTLatentStyles latentStyles){
+ this(latentStyles,null);
+ }
+
+ protected XWPFLatentStyles(CTLatentStyles latentStyles, XWPFStyles styles) {
+ this.latentStyles=latentStyles;
+ this.styles=styles;
+ }
+
+ /**
+ * checks wheter specific LatentStyleID is a latentStyle
+ */
+ protected boolean isLatentStyle(String latentStyleID){
+ for ( CTLsdException lsd: latentStyles.getLsdExceptionArray()) {
+ if(lsd.getName().equals(latentStyleID));
+ return true;
+ }
+ return false;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org