applied patches by Gisella Bronzetti:46299 - support for carriage return and line break in XWPFRun 46300 - support for line spacing in XWPFParagraph 46308 - initial support for creation of XWPFTable

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@727498 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-12-17 20:05:16 +00:00
parent 9a665498f8
commit fabf35b20d
16 changed files with 1317 additions and 296 deletions

View File

@ -37,6 +37,9 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.5-beta5" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">46299 - support for carriage return and line break in XWPFRun</action>
<action dev="POI-DEVELOPERS" type="add">46300 - support for line spacing in XWPFParagraph</action>
<action dev="POI-DEVELOPERS" type="add">46308 - initial support for creation of XWPFTable</action>
<action dev="POI-DEVELOPERS" type="add">Added getters to parent objects: HSSFSheet.getWorkbook(), HSSFRow.getSheet() and HSSFCell.getRow()</action>
<action dev="POI-DEVELOPERS" type="fix">46385 - (also patch 46362) fix serialization of StyleRecord with unicode name</action>
<action dev="POI-DEVELOPERS" type="fix">46368 - Fix HSSFRichTextRun and strings longer than 32768 characters</action>

View File

@ -34,6 +34,9 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.5-beta5" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">46299 - support for carriage return and line break in XWPFRun</action>
<action dev="POI-DEVELOPERS" type="add">46300 - support for line spacing in XWPFParagraph</action>
<action dev="POI-DEVELOPERS" type="add">46308 - initial support for creation of XWPFTable</action>
<action dev="POI-DEVELOPERS" type="add">Added getters to parent objects: HSSFSheet.getWorkbook(), HSSFRow.getSheet() and HSSFCell.getRow()</action>
<action dev="POI-DEVELOPERS" type="fix">46385 - (also patch 46362) fix serialization of StyleRecord with unicode name</action>
<action dev="POI-DEVELOPERS" type="fix">46368 - Fix HSSFRichTextRun and strings longer than 32768 characters</action>

View File

@ -17,7 +17,6 @@
package org.apache.poi.xwpf.usermodel;
import java.io.FileOutputStream;
import java.math.BigInteger;
/**
* A simple WOrdprocessingML document created by POI XWPF API
@ -46,7 +45,7 @@ public class SimpleDocument {
r1.setBold(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(new BigInteger("100"));
r1.setTextPosition(100);
XWPFParagraph p2 = doc.createParagraph();
p2.setAlignment(ParagraphAlignment.RIGHT);
@ -61,50 +60,57 @@ public class SimpleDocument {
XWPFRun r2 = p2.createRun();
r2.setText("jumped over the lazy dog");
r2.setStrike(true);
r2.setFontSize(new BigInteger("20"));
r2.setFontSize(20);
XWPFRun r3 = p2.createRun();
r3.setText("and went away");
r3.setStrike(true);
r3.setFontSize(new BigInteger("20"));
r3.setFontSize(20);
r3.setSubscript(VerticalAlign.SUPERSCRIPT);
XWPFParagraph p3 = doc.createParagraph();
p3.setWordWrap(true);
p3.setPageBreak(true);
p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
p3.setIndentationFirstLine(new BigInteger("600"));
p3.setSpacingAfter(new BigInteger("250"));
p3.setSpacingBefore(new BigInteger("250"));
//p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
p3.setAlignment(ParagraphAlignment.BOTH);
p3.setSpacingLineRule(LineSpacingRule.EXACT);
p3.setIndentationFirstLine(600);
XWPFRun r4 = p3.createRun();
r4.setTextPosition(new BigInteger("20"));
r4.setTextPosition(20);
r4.setText("To be, or not to be: that is the question: "
+ "Whether 'tis nobler in the mind to suffer "
+ "The slings and arrows of outrageous fortune, "
+ "Or to take arms against a sea of troubles, "
+ "And by opposing end them? To die: to sleep; "
+ "No more; and by a sleep to say we end "
+ "And by opposing end them? To die: to sleep; ");
r4.addBreak(BreakType.PAGE);
r4.setText("No more; and by a sleep to say we end "
+ "The heart-ache and the thousand natural shocks "
+ "That flesh is heir to, 'tis a consummation "
+ "Devoutly to be wish'd. To die, to sleep; "
+ "To sleep: perchance to dream: ay, there's the rub; "
+ ".......");
r4.setItalic(true);
//This would imply that this break shall be treated as a simple line break, and break the line after that word:
XWPFRun r5 = p3.createRun();
r5.setTextPosition(new BigInteger("-10"));
r5.setText("For in that sleep of death what dreams may come"
+ "When we have shuffled off this mortal coil,"
r5.setTextPosition(-10);
r5.setText("For in that sleep of death what dreams may come");
r5.addCarriageReturn();
r5.setText("When we have shuffled off this mortal coil,"
+ "Must give us pause: there's the respect"
+ "That makes calamity of so long life;"
+ "For who would bear the whips and scorns of time,"
+ "The oppressor's wrong, the proud man's contumely,"
+ "The pangs of despised love, the law's delay,"
+ "The insolence of office and the spurns" + ".......");
+ "That makes calamity of so long life;");
r5.addBreak();
r5.setText("For who would bear the whips and scorns of time,"
+ "The oppressor's wrong, the proud man's contumely,");
r5.addBreak(BreakClear.ALL);
r5.setText("The pangs of despised love, the law's delay,"
+ "The insolence of office and the spurns" + ".......");
FileOutputStream out = new FileOutputStream("simple.docx");
doc.write(out);

View File

@ -0,0 +1,56 @@
/* ====================================================================
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.io.FileOutputStream;
/**
* A simple WOrdprocessingML table created by POI XWPF API
*
* @author gisella bronzetti
*/
public class SimpleTable {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
XWPFTable table=doc.createTable(3,3);
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setBold(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
table.getRow(0).getCell(0).setParagraph(p1);
table.getRow(2).getCell(2).setText("only text");
FileOutputStream out = new FileOutputStream("simpleTable.docx");
doc.write(out);
out.close();
}
}

View File

@ -0,0 +1,111 @@
/* ====================================================================
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.HashMap;
import java.util.Map;
/**
* Specifies the set of possible restart locations which may be used as to
* determine the next available line when a breakÕs type attribute has a value
* of textWrapping.
*
* @author Gisella Bronzetti
*/
public enum BreakClear {
/**
* Specifies that the text wrapping break shall advance the text to the next
* line in the WordprocessingML document, regardless of its position left to
* right or the presence of any floating objects which intersect with the
* line,
*
* This is the setting for a typical line break in a document.
*/
NONE(1),
/**
* Specifies that the text wrapping break shall behave as follows:
* <ul>
* <li> If this line is broken into multiple regions (a floating object in
* the center of the page has text wrapping on both sides:
* <ul>
* <li> If this is the leftmost region of text flow on this line, advance
* the text to the next position on the line </li>
* <li>Otherwise, treat this as a text wrapping break of type all. </li>
* </ul>
* </li>
* <li> If this line is not broken into multiple regions, then treat this
* break as a text wrapping break of type none. </li>
* </ul>
* <li> If the parent paragraph is right to left, then these behaviors are
* also reversed. </li>
*/
LEFT(2),
/**
* Specifies that the text wrapping break shall behave as follows:
* <ul>
* <li> If this line is broken into multiple regions (a floating object in
* the center of the page has text wrapping on both sides:
* <ul>
* <li> If this is the rightmost region of text flow on this line, advance
* the text to the next position on the next line </li>
* <li> Otherwise, treat this as a text wrapping break of type all. </li>
* </ul>
* <li> If this line is not broken into multiple regions, then treat this
* break as a text wrapping break of type none. </li>
* <li> If the parent paragraph is right to left, then these beha viors are
* also reversed. </li>
* </ul>
*/
RIGHT(3),
/**
* Specifies that the text wrapping break shall advance the text to the next
* line in the WordprocessingML document which spans the full width of the
* line.
*/
ALL(4);
private final int value;
private BreakClear(int val) {
value = val;
}
public int getValue() {
return value;
}
private static Map<Integer, BreakClear> imap = new HashMap<Integer, BreakClear>();
static {
for (BreakClear p : values()) {
imap.put(p.getValue(), p);
}
}
public static BreakClear valueOf(int type) {
BreakClear bType = imap.get(type);
if (bType == null)
throw new IllegalArgumentException("Unknown break clear type: "
+ type);
return bType;
}
}

View File

@ -0,0 +1,84 @@
/* ====================================================================
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.HashMap;
import java.util.Map;
/**
* Specifies the possible types of break characters in a WordprocessingML
* document.
* The break type determines the next location where text shall be
* placed after this manual break is applied to the text contents
*
* @author Gisella Bronzetti
*/
public enum BreakType {
/**
* Specifies that the current break shall restart itself on the next page of
* the document when the document is displayed in page view.
*/
PAGE(1),
/**
* Specifies that the current break shall restart itself on the next column
* available on the current page when the document is displayed in page
* view.
* <p>
* If the current section is not divided into columns, or the column break
* occurs in the last column on the current page when displayed, then the
* restart location for text shall be the next page in the document.
* </p>
*/
COLUMN(2),
/**
* Specifies that the current break shall restart itself on the next line in
* the document when the document is displayed in page view.
* The determine of the next line shall be done subject to the value of the clear
* attribute on the specified break character.
*/
TEXT_WRAPPING(3);
private final int value;
private BreakType(int val) {
value = val;
}
public int getValue() {
return value;
}
private static Map<Integer, BreakType> imap = new HashMap<Integer, BreakType>();
static {
for (BreakType p : values()) {
imap.put(p.getValue(), p);
}
}
public static BreakType valueOf(int type) {
BreakType bType = imap.get(type);
if (bType == null)
throw new IllegalArgumentException("Unknown break type: "
+ type);
return bType;
}
}

View File

@ -0,0 +1,76 @@
/* ====================================================================
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.HashMap;
import java.util.Map;
/**
* Specifies the logic which shall be used to calculate the line spacing of the
* parent object when it is displayed in the document.
*
* @author Gisella Bronzetti
*/
public enum LineSpacingRule {
/**
* Specifies that the line spacing of the parent object shall be
* automatically determined by the size of its contents, with no
* predetermined minimum or maximum size.
*/
AUTO(1),
/**
* Specifies that the height of the line shall be exactly the value
* specified, regardless of the size of the contents If the contents are too
* large for the specified height, then they shall be clipped as necessary.
*/
EXACT(2),
/**
* Specifies that the height of the line shall be at least the value
* specified, but may be expanded to fit its content as needed.
*/
AT_LEAST(3);
private final int value;
private LineSpacingRule(int val) {
value = val;
}
public int getValue() {
return value;
}
private static Map<Integer, LineSpacingRule> imap = new HashMap<Integer, LineSpacingRule>();
static {
for (LineSpacingRule p : values()) {
imap.put(p.getValue(), p);
}
}
public static LineSpacingRule valueOf(int type) {
LineSpacingRule lineType = imap.get(type);
if (lineType == null)
throw new IllegalArgumentException("Unknown line type: " + type);
return lineType;
}
}

View File

@ -318,5 +318,23 @@ public class XWPFDocument extends POIXMLDocument {
return new XWPFParagraph(ctDocument.getBody().addNewP(), this);
}
/**
* Create an empty table with one row and one column as default.
*
* @return a new table
*/
public XWPFTable createTable(){
return new XWPFTable(ctDocument.getBody().addNewTbl());
}
/**
* Create an empty table with a number of rows and cols specified
* @param rows
* @param cols
* @return table
*/
public XWPFTable createTable(int rows, int cols) {
return new XWPFTable(ctDocument.getBody().addNewTbl(), rows, cols);
}
}

View File

@ -38,6 +38,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextAlignment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
import org.w3c.dom.NodeList;
@ -58,80 +59,86 @@ public class XWPFParagraph {
private StringBuffer text = new StringBuffer();
private StringBuffer pictureText = new StringBuffer();
protected XWPFParagraph(CTP prgrph) {
this(prgrph, null);
}
protected XWPFParagraph(CTP prgrph, XWPFDocument docRef) {
this.paragraph = prgrph;
this.document = docRef;
this.paragraph = prgrph;
this.document = docRef;
if (!isEmpty()) {
// All the runs to loop over
// TODO - replace this with some sort of XPath expression
// to directly find all the CTRs, in the right order
ArrayList<CTR> rs = new ArrayList<CTR>();
CTR[] tmp;
if (!isEmpty()) {
// All the runs to loop over
// TODO - replace this with some sort of XPath expression
// to directly find all the CTRs, in the right order
ArrayList<CTR> rs = new ArrayList<CTR>();
CTR[] tmp;
// Get the main text runs
tmp = paragraph.getRArray();
for (int i = 0; i < tmp.length; i++) {
rs.add(tmp[i]);
}
// Get the main text runs
tmp = paragraph.getRArray();
for (int i = 0; i < tmp.length; i++) {
rs.add(tmp[i]);
}
// Not sure quite what these are, but they hold
// more text runs
CTSdtRun[] sdts = paragraph.getSdtArray();
for (int i = 0; i < sdts.length; i++) {
CTSdtContentRun run = sdts[i].getSdtContent();
tmp = run.getRArray();
for (int j = 0; j < tmp.length; j++) {
rs.add(tmp[j]);
}
}
// Not sure quite what these are, but they hold
// more text runs
CTSdtRun[] sdts = paragraph.getSdtArray();
for (int i = 0; i < sdts.length; i++) {
CTSdtContentRun run = sdts[i].getSdtContent();
tmp = run.getRArray();
for (int j = 0; j < tmp.length; j++) {
rs.add(tmp[j]);
}
}
// Get text of the paragraph
for (int j = 0; j < rs.size(); j++) {
// Grab the text and tabs of the paragraph
// Do so in a way that preserves the ordering
XmlCursor c = rs.get(j).newCursor();
c.selectPath("./*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTText) {
text.append(((CTText) o).getStringValue());
}
if (o instanceof CTPTab) {
text.append("\t");
}
}
// Get text of the paragraph
for (int j = 0; j < rs.size(); j++) {
// Grab the text and tabs of the paragraph
// Do so in a way that preserves the ordering
XmlCursor c = rs.get(j).newCursor();
c.selectPath("./*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTText) {
text.append(((CTText) o).getStringValue());
}
if (o instanceof CTPTab) {
text.append("\t");
}
}
// Loop over pictures inside our
// paragraph, looking for text in them
CTPicture[] picts = rs.get(j).getPictArray();
for (int k = 0; k < picts.length; k++) {
XmlObject[] t = picts[k]
.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
for (int m = 0; m < t.length; m++) {
NodeList kids = t[m].getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) {
pictureText.append("\n");
pictureText.append(kids.item(n).getNodeValue());
}
}
}
}
}
}
// Loop over pictures inside our
// paragraph, looking for text in them
CTPicture[] picts = rs.get(j).getPictArray();
for (int k = 0; k < picts.length; k++) {
XmlObject[] t = picts[k]
.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
for (int m = 0; m < t.length; m++) {
NodeList kids = t[m].getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) {
pictureText.append("\n");
pictureText.append(kids.item(n).getNodeValue());
}
}
}
}
}
}
}
public CTP getCTP() {
return paragraph;
return paragraph;
}
public boolean isEmpty() {
return !paragraph.getDomNode().hasChildNodes();
return !paragraph.getDomNode().hasChildNodes();
}
public XWPFDocument getDocument() {
return document;
return document;
}
/**
@ -139,7 +146,7 @@ public class XWPFParagraph {
* in it.
*/
public String getText() {
return getParagraphText() + getPictureText();
return getParagraphText() + getPictureText();
}
/**
@ -147,14 +154,14 @@ public class XWPFParagraph {
* paragraph
*/
public String getParagraphText() {
return text.toString();
return text.toString();
}
/**
* Returns any text from any suitable pictures in the paragraph
*/
public String getPictureText() {
return pictureText.toString();
return pictureText.toString();
}
/**
@ -163,7 +170,7 @@ public class XWPFParagraph {
* @return a new text run
*/
public XWPFRun createRun() {
return new XWPFRun(paragraph.addNewR(), this);
return new XWPFRun(paragraph.addNewR(), this);
}
/**
@ -181,7 +188,7 @@ public class XWPFParagraph {
* @return the paragraph alignment of this paragraph.
*/
public ParagraphAlignment getAlignment() {
CTPPr pr = paragraph.getPPr();
CTPPr pr = getCTPPr();
return pr == null || !pr.isSetJc() ? ParagraphAlignment.LEFT
: ParagraphAlignment.valueOf(pr.getJc().getVal().intValue());
}
@ -201,8 +208,7 @@ public class XWPFParagraph {
* @param align the paragraph alignment to apply to this paragraph.
*/
public void setAlignment(ParagraphAlignment align) {
CTPPr pr = paragraph.isSetPPr() ? paragraph.getPPr() : paragraph
.addNewPPr();
CTPPr pr = getCTPPr();
CTJc jc = pr.isSetJc() ? pr.getJc() : pr.addNewJc();
STJc.Enum en = STJc.Enum.forInt(align.getValue());
jc.setVal(en);
@ -227,8 +233,8 @@ public class XWPFParagraph {
* @return the vertical alignment of this paragraph.
*/
public TextAlignment getVerticalAlignment() {
CTPPr pr = paragraph.getPPr();
return pr == null || !pr.isSetTextAlignment() ? TextAlignment.AUTO
CTPPr pr = getCTPPr();
return (pr == null || !pr.isSetTextAlignment()) ? TextAlignment.AUTO
: TextAlignment.valueOf(pr.getTextAlignment().getVal()
.intValue());
}
@ -253,8 +259,7 @@ public class XWPFParagraph {
* paragraph.
*/
public void setVerticalAlignment(TextAlignment valign) {
CTPPr pr = paragraph.isSetPPr() ? paragraph.getPPr() : paragraph
.addNewPPr();
CTPPr pr = getCTPPr();
CTTextAlignment textAlignment = pr.isSetTextAlignment() ? pr
.getTextAlignment() : pr.addNewTextAlignment();
STTextAlignment.Enum en = STTextAlignment.Enum
@ -291,7 +296,8 @@ public class XWPFParagraph {
*/
public void setBorderTop(Borders border) {
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
CTBorder pr = (ct != null && ct.isSetTop()) ? ct.getTop() : ct.addNewTop();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetTop();
else
@ -308,13 +314,12 @@ public class XWPFParagraph {
*/
public Borders getBorderTop() {
CTPBdr border = getCTPBrd(false);
CTBorder ct;
CTBorder ct = null;
if (border != null) {
ct = border.getTop();
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
return Borders.NONE;
STBorder.Enum ptrn = (ct != null) ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
/**
@ -345,12 +350,12 @@ public class XWPFParagraph {
* @see Borders a list of all types of borders
*/
public void setBorderBottom(Borders border) {
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetBottom();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetBottom() ? ct.getBottom() : ct.addNewBottom();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetBottom();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
}
/**
@ -362,13 +367,13 @@ public class XWPFParagraph {
* @see Borders a list of all types of borders
*/
public Borders getBorderBottom() {
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getBottom();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getBottom();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
/**
@ -394,12 +399,12 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public void setBorderLeft(Borders border) {
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetLeft();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetLeft() ? ct.getLeft() : ct.addNewLeft();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetLeft();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
}
/**
@ -411,13 +416,13 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public Borders getBorderLeft() {
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getLeft();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getLeft();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
/**
@ -443,12 +448,12 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public void setBorderRight(Borders border) {
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetRight();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetRight() ? ct.getRight() : ct.addNewRight();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetRight();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
}
/**
@ -460,13 +465,13 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public Borders getBorderRight() {
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getRight();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getRight();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
/**
@ -496,12 +501,12 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public void setBorderBetween(Borders border) {
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetBetween() ? ct.getBetween() : ct.addNewBetween();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetBetween();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
CTPBdr ct = getCTPBrd(true);
CTBorder pr = ct.isSetBetween() ? ct.getBetween() : ct.addNewBetween();
if (border.getValue() == Borders.NONE.getValue())
ct.unsetBetween();
else
pr.setVal(STBorder.Enum.forInt(border.getValue()));
}
/**
@ -513,13 +518,13 @@ public class XWPFParagraph {
* @see Borders for a list of all possible borders
*/
public Borders getBorderBetween() {
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getBetween();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
CTPBdr border = getCTPBrd(false);
CTBorder ct = null;
if (border != null) {
ct = border.getBetween();
}
STBorder.Enum ptrn = ct != null ? ct.getVal() : STBorder.NONE;
return Borders.valueOf(ptrn.intValue());
}
/**
@ -539,13 +544,13 @@ public class XWPFParagraph {
* boolean value
*/
public void setPageBreak(boolean pageBreak) {
CTPPr ppr = getCTPPr();
CTOnOff ct_pageBreak = ppr.isSetPageBreakBefore() ? ppr
.getPageBreakBefore() : ppr.addNewPageBreakBefore();
if (pageBreak)
ct_pageBreak.setVal(STOnOff.TRUE);
else
ct_pageBreak.setVal(STOnOff.FALSE);
CTPPr ppr = getCTPPr();
CTOnOff ct_pageBreak = ppr.isSetPageBreakBefore() ? ppr
.getPageBreakBefore() : ppr.addNewPageBreakBefore();
if (pageBreak)
ct_pageBreak.setVal(STOnOff.TRUE);
else
ct_pageBreak.setVal(STOnOff.FALSE);
}
/**
@ -564,14 +569,14 @@ public class XWPFParagraph {
* @return boolean - if page break is set
*/
public boolean isPageBreak() {
CTPPr ppr = getCTPPr();
CTOnOff ct_pageBreak = ppr.isSetPageBreakBefore() ? ppr
.getPageBreakBefore() : null;
if (ct_pageBreak != null
&& ct_pageBreak.getVal().intValue() == STOnOff.INT_TRUE)
return true;
else
return false;
CTPPr ppr = getCTPPr();
CTOnOff ct_pageBreak = ppr.isSetPageBreakBefore() ? ppr
.getPageBreakBefore() : null;
if (ct_pageBreak != null
&& ct_pageBreak.getVal().intValue() == STOnOff.INT_TRUE)
return true;
else
return false;
}
/**
@ -586,21 +591,24 @@ public class XWPFParagraph {
* a positive whole number, whose contents consist of a
* measurement in twentieths of a point.
*/
public void setSpacingAfter(BigInteger spaces) {
public void setSpacingAfter(int spaces) {
CTSpacing spacing = getCTSpacing(true);
if (spacing != null)
spacing.setAfter(spaces);
if (spacing != null) {
BigInteger bi = new BigInteger("" + spaces);
spacing.setAfter(bi);
}
}
/**
* Specifies the spacing that should be added after the last line in this
* paragraph in the document in absolute units.
*
* @return bigInteger - value representing the spacing after the paragraph
* @return int - value representing the spacing after the paragraph
*/
public BigInteger getSpacingAfter() {
public int getSpacingAfter() {
CTSpacing spacing = getCTSpacing(false);
return spacing.isSetAfter() ? spacing.getAfter() : null;
return (spacing != null && spacing.isSetAfter()) ? spacing.getAfter().intValue() : -1;
}
/**
@ -620,9 +628,10 @@ public class XWPFParagraph {
* a positive whole number, whose contents consist of a
* measurement in twentieths of a
*/
public void setSpacingAfterLines(BigInteger spaces) {
public void setSpacingAfterLines(int spaces) {
CTSpacing spacing = getCTSpacing(true);
spacing.setAfterLines(spaces);
BigInteger bi = new BigInteger("" + spaces);
spacing.setAfterLines(bi);
}
@ -633,11 +642,12 @@ public class XWPFParagraph {
* @return bigInteger - value representing the spacing after the paragraph
* @see #setSpacingAfterLines(BigInteger)
*/
public BigInteger getSpacingAfterLines() {
public int getSpacingAfterLines() {
CTSpacing spacing = getCTSpacing(false);
return spacing.isSetAfterLines() ? spacing.getAfterLines() : null;
return (spacing != null && spacing.isSetAfterLines()) ? spacing.getAfterLines().intValue() : -1;
}
/**
* Specifies the spacing that should be added above the first line in this
* paragraph in the document in absolute units.
@ -648,9 +658,10 @@ public class XWPFParagraph {
*
* @param spaces
*/
public void setSpacingBefore(BigInteger spaces) {
public void setSpacingBefore(int spaces) {
CTSpacing spacing = getCTSpacing(true);
spacing.setBefore(spaces);
BigInteger bi = new BigInteger("" + spaces);
spacing.setBefore(bi);
}
/**
@ -660,9 +671,9 @@ public class XWPFParagraph {
* @return the spacing that should be added above the first line
* @see #setSpacingBefore(BigInteger)
*/
public BigInteger getSpacingBefore() {
public int getSpacingBefore() {
CTSpacing spacing = getCTSpacing(false);
return spacing.isSetBefore() ? spacing.getBefore() : null;
return (spacing != null && spacing.isSetBefore()) ? spacing.getBefore().intValue() : -1;
}
/**
@ -677,9 +688,10 @@ public class XWPFParagraph {
*
* @param spaces
*/
public void setSpacingBeforeLines(BigInteger spaces) {
public void setSpacingBeforeLines(int spaces) {
CTSpacing spacing = getCTSpacing(true);
spacing.setBeforeLines(spaces);
BigInteger bi = new BigInteger("" + spaces);
spacing.setBeforeLines(bi);
}
/**
@ -690,11 +702,41 @@ public class XWPFParagraph {
* @return the spacing that should be added before the first line in this paragraph
* @see #setSpacingBeforeLines(BigInteger)
*/
public BigInteger getSpacingBeforeLines() {
public int getSpacingBeforeLines() {
CTSpacing spacing = getCTSpacing(false);
return spacing.isSetBeforeLines() ? spacing.getBeforeLines() : null;
return (spacing != null && spacing.isSetBeforeLines()) ? spacing.getBeforeLines().intValue() : -1;
}
/**
* Specifies how the spacing between lines is calculated as stored in the
* line attribute. If this attribute is omitted, then it shall be assumed to
* be of a value auto if a line attribute value is present.
*
* @param rule
* @see LineSpacingRule
*/
public void setSpacingLineRule(LineSpacingRule rule) {
CTSpacing spacing = getCTSpacing(true);
spacing.setLineRule(STLineSpacingRule.Enum.forInt(rule.getValue()));
}
/**
* Specifies how the spacing between lines is calculated as stored in the
* line attribute. If this attribute is omitted, then it shall be assumed to
* be of a value auto if a line attribute value is present.
*
* @return rule
* @see LineSpacingRule
* @see #setSpacingLineRule(LineSpacingRule)
*/
public LineSpacingRule getSpacingLineRule() {
CTSpacing spacing = getCTSpacing(false);
return (spacing != null && spacing.isSetLineRule()) ? LineSpacingRule.valueOf(spacing
.getLineRule().intValue()) : LineSpacingRule.AUTO;
}
/**
* Specifies the indentation which shall be placed between the left text
* margin for this paragraph and the left edge of that paragraph's content
@ -708,9 +750,10 @@ public class XWPFParagraph {
*
* @param indentation
*/
public void setIndentationLeft(BigInteger indentation) {
public void setIndentationLeft(int indentation) {
CTInd indent = getCTInd(true);
indent.setLeft(indentation);
BigInteger bi = new BigInteger("" + indentation);
indent.setLeft(bi);
}
/**
@ -726,10 +769,10 @@ public class XWPFParagraph {
*
* @return indentation or null if indentation is not set
*/
public BigInteger getIndentationLeft() {
public int getIndentationLeft() {
CTInd indentation = getCTInd(false);
return indentation.isSetLeft() ? indentation.getLeft()
: new BigInteger("0");
return (indentation != null && indentation.isSetLeft()) ? indentation.getLeft().intValue()
: -1;
}
/**
@ -745,9 +788,10 @@ public class XWPFParagraph {
*
* @param indentation
*/
public void setIndentationRight(BigInteger indentation) {
public void setIndentationRight(int indentation) {
CTInd indent = getCTInd(true);
indent.setRight(indentation);
BigInteger bi = new BigInteger("" + indentation);
indent.setRight(bi);
}
/**
@ -764,10 +808,10 @@ public class XWPFParagraph {
* @return indentation or null if indentation is not set
*/
public BigInteger getIndentationRight() {
public int getIndentationRight() {
CTInd indentation = getCTInd(false);
return indentation.isSetRight() ? indentation.getRight()
: new BigInteger("0");
return (indentation != null && indentation.isSetRight()) ? indentation.getRight().intValue()
: -1;
}
/**
@ -784,9 +828,10 @@ public class XWPFParagraph {
* @param indentation
*/
public void setIndentationHanging(BigInteger indentation) {
public void setIndentationHanging(int indentation) {
CTInd indent = getCTInd(true);
indent.setHanging(indentation);
BigInteger bi = new BigInteger("" + indentation);
indent.setHanging(bi);
}
/**
@ -802,9 +847,9 @@ public class XWPFParagraph {
*
* @return indentation or null if indentation is not set
*/
public BigInteger getIndentationHanging() {
public int getIndentationHanging() {
CTInd indentation = getCTInd(false);
return indentation.isSetHanging() ? indentation.getHanging() : null;
return (indentation != null && indentation.isSetHanging()) ? indentation.getHanging().intValue() : -1;
}
/**
@ -821,9 +866,10 @@ public class XWPFParagraph {
*
* @param indentation
*/
public void setIndentationFirstLine(BigInteger indentation) {
public void setIndentationFirstLine(int indentation) {
CTInd indent = getCTInd(true);
indent.setFirstLine(indentation);
BigInteger bi = new BigInteger("" + indentation);
indent.setFirstLine(bi);
}
/**
@ -841,10 +887,10 @@ public class XWPFParagraph {
*
* @return indentation or null if indentation is not set
*/
public BigInteger getIndentationFirstLine() {
public int getIndentationFirstLine() {
CTInd indentation = getCTInd(false);
return indentation.isSetFirstLine() ? indentation.getFirstLine()
: new BigInteger("0");
return (indentation != null && indentation.isSetFirstLine()) ? indentation.getFirstLine().intValue()
: -1;
}
/**
@ -856,12 +902,12 @@ public class XWPFParagraph {
* @param wrap - boolean
*/
public void setWordWrap(boolean wrap) {
CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr()
.getWordWrap() : getCTPPr().addNewWordWrap();
if (wrap)
wordWrap.setVal(STOnOff.TRUE);
else
wordWrap.unsetVal();
CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr()
.getWordWrap() : getCTPPr().addNewWordWrap();
if (wrap)
wordWrap.setVal(STOnOff.TRUE);
else
wordWrap.unsetVal();
}
/**
@ -873,14 +919,14 @@ public class XWPFParagraph {
* @return boolean
*/
public boolean isWordWrap() {
CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr()
.getWordWrap() : null;
if (wordWrap != null) {
return (wordWrap.getVal() == STOnOff.ON
|| wordWrap.getVal() == STOnOff.TRUE || wordWrap.getVal() == STOnOff.X_1) ? true
: false;
} else
return false;
CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr()
.getWordWrap() : null;
if (wordWrap != null) {
return (wordWrap.getVal() == STOnOff.ON
|| wordWrap.getVal() == STOnOff.TRUE || wordWrap.getVal() == STOnOff.X_1) ? true
: false;
} else
return false;
}
/**
@ -889,13 +935,10 @@ public class XWPFParagraph {
*/
private CTPBdr getCTPBrd(boolean create) {
CTPPr pr = getCTPPr();
if (pr != null) {
CTPBdr ct = pr.isSetPBdr() ? pr.getPBdr() : null;
if (create && ct == null)
ct = pr.addNewPBdr();
return ct;
}
return null;
CTPBdr ct = pr.isSetPBdr() ? pr.getPBdr() : null;
if (create && ct == null)
ct = pr.addNewPBdr();
return ct;
}
/**
@ -904,13 +947,10 @@ public class XWPFParagraph {
*/
private CTSpacing getCTSpacing(boolean create) {
CTPPr pr = getCTPPr();
if (pr != null) {
CTSpacing ct = pr.isSetSpacing() ? pr.getSpacing() : null;
if (create && ct == null)
ct = pr.addNewSpacing();
return ct;
}
return null;
CTSpacing ct = pr.getSpacing() == null ? null : pr.getSpacing();
if (create && ct == null)
ct = pr.addNewSpacing();
return ct;
}
/**
@ -919,15 +959,16 @@ public class XWPFParagraph {
*/
private CTInd getCTInd(boolean create) {
CTPPr pr = getCTPPr();
if (pr != null) {
CTInd ct = pr.isSetInd() ? pr.getInd() : null;
if (create && ct == null)
ct = pr.addNewInd();
return ct;
}
return null;
CTInd ct = pr.getInd() == null ? null : pr.getInd();
if (create && ct == null)
ct = pr.addNewInd();
return ct;
}
/**
* Get a <b>copy</b> of the currently used CTPPr, if none is used, return
* a new instance.
*/
private CTPPr getCTPPr() {
CTPPr pr = paragraph.getPPr() == null ? paragraph.addNewPPr()
: paragraph.getPPr();

View File

@ -18,6 +18,7 @@ package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
@ -27,6 +28,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSignedHpsMeasure
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
@ -49,10 +52,18 @@ public class XWPFRun {
this.paragraph = p;
}
/**
* Get the currently used CTR object
* @return ctr object
*/
public CTR getCTR() {
return run;
}
/**
* Get the currenty referenced paragraph object
* @return current paragraph
*/
public XWPFParagraph getParagraph() {
return paragraph;
}
@ -70,9 +81,8 @@ public class XWPFRun {
/**
* Whether the bold property shall be applied to all non-complex script
* characters in the contents of this run when displayed in a document
* <p/>
* <p/>
* characters in the contents of this run when displayed in a document.
* <p>
* This formatting property is a toggle property, which specifies that its
* behavior differs between its use within a style definition and its use as
* direct formatting. When used as part of a style definition, setting this
@ -83,7 +93,7 @@ public class XWPFRun {
* direct formatting, setting this property to true or false shall set the
* absolute state of the resulting property.
* </p>
* <p/>
* <p>
* If this element is not present, the default value is to leave the
* formatting applied at previous level in the style hierarchy. If this
* element is never applied in the style hierarchy, then bold shall not be
@ -104,8 +114,8 @@ public class XWPFRun {
*
* @return the text of this text run or <code>null</code> if not set
*/
public String getText() {
return run.sizeOfTArray() == 0 ? null : run.getTArray(0)
public String getText(int pos) {
return run.sizeOfTArray() == 0 ? null : run.getTArray(pos)
.getStringValue();
}
@ -115,10 +125,22 @@ public class XWPFRun {
* @param value the literal text which shall be displayed in the document
*/
public void setText(String value) {
CTText t = run.sizeOfTArray() == 0 ? run.addNewT() : run.getTArray(0);
setText(value,run.getTArray().length);
}
/**
* Sets the text of this text run in the
*
* @param value the literal text which shall be displayed in the document
* @param pos - position in the text array (NB: 0 based)
*/
public void setText(String value, int pos) {
if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
t.setStringValue(value);
}
/**
* Whether the italic property should be applied to all non-complex script
* characters in the contents of this run when displayed in a document.
@ -171,7 +193,7 @@ public class XWPFRun {
public UnderlinePatterns getUnderline() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetU()) ? UnderlinePatterns.valueOf(pr
.getU().getVal().intValue()) : null;
.getU().getVal().intValue()) : UnderlinePatterns.NONE;
}
/**
@ -191,7 +213,7 @@ public class XWPFRun {
*/
public void setUnderline(UnderlinePatterns value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTUnderline underline = pr.isSetU() ? pr.getU() : pr.addNewU();
CTUnderline underline = (pr.getU() == null) ? pr.addNewU() : pr.getU();
underline.setVal(STUnderline.Enum.forInt(value.getValue()));
}
@ -248,7 +270,7 @@ public class XWPFRun {
public VerticalAlign getSubscript() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetVertAlign()) ? VerticalAlign.valueOf(pr
.getVertAlign().getVal().intValue()) : null;
.getVertAlign().getVal().intValue()) : VerticalAlign.BASELINE;
}
/**
@ -305,9 +327,9 @@ public class XWPFRun {
*
* @return value representing the font size
*/
public BigInteger getFontSize() {
public int getFontSize() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")) : null;
return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")).intValue() : -1;
}
/**
@ -322,10 +344,11 @@ public class XWPFRun {
*
* @param size
*/
public void setFontSize(BigInteger size) {
public void setFontSize(int size) {
BigInteger bint=new BigInteger(""+size);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();
ctSize.setVal(size.multiply(new BigInteger("2")));
ctSize.setVal(bint.multiply(new BigInteger("2")));
}
/**
@ -336,10 +359,10 @@ public class XWPFRun {
*
* @return a big integer representing the amount of text shall be "moved"
*/
public BigInteger getTextPosition() {
public int getTextPosition() {
CTRPr pr = run.getRPr();
return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal()
: null;
return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal().intValue()
: -1;
}
/**
@ -364,10 +387,87 @@ public class XWPFRun {
*
* @param val
*/
public void setTextPosition(BigInteger val) {
public void setTextPosition(int val) {
BigInteger bint=new BigInteger(""+val);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition();
position.setVal(val);
position.setVal(bint);
}
/**
*
*/
public void removeBreak() {
// TODO
}
/**
* Specifies that a break shall be placed at the current location in the run
* content.
* A break is a special character which is used to override the
* normal line breaking that would be performed based on the normal layout
* of the documentÕs contents.
* @see addCarriageReturn()
*/
public void addBreak() {
run.addNewBr();
}
/**
* Specifies that a break shall be placed at the current location in the run
* content.
* A break is a special character which is used to override the
* normal line breaking that would be performed based on the normal layout
* of the documentÕs contents.
* <p>
* The behavior of this break character (the
* location where text shall be restarted after this break) shall be
* determined by its type values.
* </p>
* @see BreakType
*/
public void addBreak(BreakType type){
CTBr br=run.addNewBr();
br.setType(STBrType.Enum.forInt(type.getValue()));
}
/**
* Specifies that a break shall be placed at the current location in the run
* content. A break is a special character which is used to override the
* normal line breaking that would be performed based on the normal layout
* of the documentÕs contents.
* <p>
* The behavior of this break character (the
* location where text shall be restarted after this break) shall be
* determined by its type (in this case is BreakType.TEXT_WRAPPING as default) and clear attribute values.
* </p>
* @see BreakClear
*/
public void addBreak(BreakClear clear){
CTBr br=run.addNewBr();
br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue()));
br.setClear(STBrClear.Enum.forInt(clear.getValue()));
}
/**
* Specifies that a carriage return shall be placed at the
* current location in the run content.
* A carriage return is used to end the current line of text in
* Wordprocess.
* The behavior of a carriage return in run content shall be
* identical to a break character with null type and clear attributes, which
* shall end the current line and find the next available line on which to
* continue.
* The carriage return character forced the following text to be
* restarted on the next available line in the document.
*/
public void addCarriageReturn() {
run.addNewCr();
}
public void removeCarriageReturn() {
//TODO
}
}

View File

@ -16,42 +16,188 @@
==================================================================== */
package org.apache.poi.xwpf.usermodel;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import java.math.BigInteger;
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.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
/**
* Sketch of XWPFTable class. Only table's text is being hold.
* <p/>
* Specifies the contents of a table present in the document. A table is a set
* of paragraphs (and other block-level content) arranged in rows and columns.
*
* @author Yury Batrakov (batrakov at gmail.com)
*
*/
public class XWPFTable
{
protected StringBuffer text=new StringBuffer();
public class XWPFTable {
public XWPFTable(CTTbl table) {
for(CTRow row : table.getTrArray()) {
StringBuffer rowText = new StringBuffer();
for(CTTc cell : row.getTcArray()) {
for(CTP ctp : cell.getPArray()) {
XWPFParagraph p = new XWPFParagraph(ctp, null);
if(rowText.length() > 0) {
rowText.append('\t');
}
rowText.append(p.getText());
}
}
if(rowText.length() > 0) {
this.text.append(rowText);
this.text.append('\n');
protected StringBuffer text = new StringBuffer();
private CTTbl ctTbl;
public XWPFTable(CTTbl table, int row, int col) {
this(table);
for (int i = 0; i < row; i++) {
XWPFTableRow tabRow = (getRow(i) == null) ? createRow() : getRow(i);
for (int k = 0; k < col; k++) {
XWPFTableCell tabCell = (tabRow.getCell(k) == null) ? tabRow
.createCell() : null;
}
}
}
public XWPFTable(CTTbl table) {
this.ctTbl = table;
// is an empty table: I add one row and one column as default
if (table.sizeOfTrArray() == 0)
createEmptyTable(table);
for (CTRow row : table.getTrArray()) {
StringBuffer rowText = new StringBuffer();
for (CTTc cell : row.getTcArray()) {
for (CTP ctp : cell.getPArray()) {
XWPFParagraph p = new XWPFParagraph(ctp, null);
if (rowText.length() > 0) {
rowText.append('\t');
}
rowText.append(p.getText());
}
}
if (rowText.length() > 0) {
this.text.append(rowText);
this.text.append('\n');
}
}
}
private void createEmptyTable(CTTbl table) {
// MINIMUM ELEMENTS FOR A TABLE
table.addNewTr().addNewTc().addNewP();
CTTblPr tblpro = table.addNewTblPr();
tblpro.addNewTblW().setW(new BigInteger("0"));
tblpro.getTblW().setType(STTblWidth.AUTO);
// layout
// tblpro.addNewTblLayout().setType(STTblLayoutType.AUTOFIT);
// borders
CTTblBorders borders = tblpro.addNewTblBorders();
borders.addNewBottom().setVal(STBorder.SINGLE);
borders.addNewInsideH().setVal(STBorder.SINGLE);
borders.addNewInsideV().setVal(STBorder.SINGLE);
borders.addNewLeft().setVal(STBorder.SINGLE);
borders.addNewRight().setVal(STBorder.SINGLE);
borders.addNewTop().setVal(STBorder.SINGLE);
/*
* CTTblGrid tblgrid=table.addNewTblGrid();
* tblgrid.addNewGridCol().setW(new BigInteger("2000"));
*/
}
/**
* @return ctTbl object
*/
public CTTbl getCTTbl() {
return ctTbl;
}
/**
* @return text
*/
public String getText() {
return text.toString();
}
}
public void addNewRowBetween(int start, int end) {
// TODO
}
/**
* add a new column for each row in this table
*/
public void addNewCol() {
if (ctTbl.sizeOfTrArray() == 0) createRow();
for (int i = 0; i < ctTbl.sizeOfTrArray(); i++) {
XWPFTableRow tabRow = new XWPFTableRow(ctTbl.getTrArray(i));
tabRow.createCell();
}
}
/**
* create a new XWPFTableRow object with as many cells as the number of columns defined in that moment
*
* @return tableRow
*/
public XWPFTableRow createRow() {
int sizeCol = ctTbl.sizeOfTrArray() > 0 ? ctTbl.getTrArray(0)
.sizeOfTcArray() : 0;
XWPFTableRow tabRow = new XWPFTableRow(ctTbl.addNewTr());
addColumn(tabRow, sizeCol);
return tabRow;
}
/**
* @param pos - index of the row
* @return the row at the position specified or null if no rows is defined or if the position is greather than the max size of rows array
*/
public XWPFTableRow getRow(int pos) {
if (pos >= 0 && pos < ctTbl.sizeOfTrArray())
return new XWPFTableRow(ctTbl.getTrArray(pos));
else
return null;
}
/**
* @param width
*/
public void setWidth(int width) {
CTTblPr tblPr = getTrPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr
.addNewTblW();
tblWidth.setW(new BigInteger("" + width));
}
/**
* @return width value
*/
public int getWidth() {
CTTblPr tblPr = getTrPr();
return tblPr.isSetTblW() ? tblPr.getTblW().getW().intValue() : -1;
}
/**
* @return number of rows in table
*/
public int getNumberOfRows() {
return ctTbl.sizeOfTrArray();
}
private CTTblPr getTrPr() {
return (ctTbl.getTblPr() != null) ? ctTbl.getTblPr() : ctTbl
.addNewTblPr();
}
private void addColumn(XWPFTableRow tabRow, int sizeCol) {
if (sizeCol > 0) {
for (int i = 0; i < sizeCol; i++) {
tabRow.createCell();
}
}
}
}// end class

View File

@ -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;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
public class XWPFTableCell {
private CTTc ctTc;
/**
* If a table cell does not include at least one block-level element, then this document shall be considered corrupt
*/
public XWPFTableCell(CTTc cell) {
this.ctTc = cell;
// NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt.
cell.addNewP();
}
public CTTc getCTTc() {
return ctTc;
}
public void setParagraph(XWPFParagraph p) {
if (ctTc.sizeOfPArray() == 0) {
ctTc.addNewP();
}
ctTc.setPArray(0, p.getCTP());
}
public XWPFParagraph getParagraph() {
return ctTc.sizeOfPArray() == 0 ? null : new XWPFParagraph(ctTc.getPArray(0));
}
public void setText(String text) {
CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
XWPFParagraph par = new XWPFParagraph(ctP);
par.createRun().setText(text);
}
public String getText() {
//TODO
return null;
}
}// end class

View File

@ -0,0 +1,91 @@
/* ====================================================================
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.math.BigInteger;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
/**
* @author gisellabronzetti
*/
public class XWPFTableRow {
private CTRow ctRow;
public XWPFTableRow(CTRow row) {
this.ctRow = row;
}
public CTRow getCtRow() {
return ctRow;
}
public XWPFTableCell createCell() {
return new XWPFTableCell(ctRow.addNewTc());
}
public XWPFTableCell getCell(int pos) {
if (pos >= 0 && pos < ctRow.sizeOfTcArray())
return new XWPFTableCell(ctRow.getTcArray(pos));
else
return null;
}
/**
* This element specifies the height of the current table row within the
* current table. This height shall be used to determine the resulting
* height of the table row, which may be absolute or relative (depending on
* its attribute values). If omitted, then the table row shall automatically
* resize its height to the height required by its contents (the equivalent
* of an hRule value of auto).
*
* @param height
*/
public void setHeight(int height) {
CTTrPr properties = getTrPr();
CTHeight h = properties.getTrHeightArray(0) == null ? properties.addNewTrHeight() : properties.getTrHeightArray(0);
h.setVal(new BigInteger("" + height));
//properties.setTrHeightArray(0,height);
}
/**
* This element specifies the height of the current table row within the
* current table. This height shall be used to determine the resulting
* height of the table row, which may be absolute or relative (depending on
* its attribute values). If omitted, then the table row shall automatically
* resize its height to the height required by its contents (the equivalent
* of an hRule value of auto).
*
* @return height
*/
public int getHeight() {
CTTrPr properties = getTrPr();
return properties.getTrHeightArray(0) == null ? 0 : properties.getTrHeightArray(0).getVal().intValue();
}
private CTTrPr getTrPr() {
return (ctRow.isSetTrPr()) ? ctRow.getTrPr() : ctRow.addNewTrPr();
}
}// end class

View File

@ -21,7 +21,9 @@ import java.math.BigInteger;
import junit.framework.TestCase;
import org.openxmlformats.schemas.officeDocument.x2006.math.STSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
@ -31,9 +33,9 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextAlignment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
import org.apache.poi.POIXMLDocument;
/**
* Tests for XWPF Paragraphs
@ -44,7 +46,7 @@ public class TestXWPFParagraph extends TestCase {
*/
private XWPFDocument xml;
private File file;
/*
protected void setUp() throws Exception {
super.setUp();
@ -55,10 +57,11 @@ public class TestXWPFParagraph extends TestCase {
assertTrue(file.exists());
xml = new XWPFDocument(POIXMLDocument.openPackage(file.toString()));
}
*/
/**
* Check that we get the right paragraph from the header
*/
/*
public void testHeaderParagraph() throws Exception {
XWPFHeader hdr = xml.getHeaderFooterPolicy().getDefaultHeader();
assertNotNull(hdr);
@ -71,10 +74,11 @@ public class TestXWPFParagraph extends TestCase {
assertEquals("First header column!\tMid header\tRight header!", p
.getText());
}
*/
/**
* Check that we get the right paragraphs from the document
*/
/*
public void testDocumentParagraph() throws Exception {
XWPFParagraph[] ps = xml.getParagraphs();
assertEquals(10, ps.length);
@ -96,20 +100,24 @@ public class TestXWPFParagraph extends TestCase {
assertFalse(ps[4].isEmpty());
assertEquals("More on page one", ps[4].getText());
}
*/
public void testSetGetBorderTop() {
//new clean instance of paragraph
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
assertEquals(STBorder.NONE.intValue(), p.getBorderTop().getValue());
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
//bordi
CTPBdr bdr = ppr.addNewPBdr();
CTBorder borderTop = bdr.addNewTop();
borderTop.setVal(STBorder.APPLES);
borderTop.setVal(STBorder.DOUBLE);
bdr.setTop(borderTop);
assertEquals(Borders.APPLES, p.getBorderTop());
assertEquals(Borders.DOUBLE, p.getBorderTop());
p.setBorderTop(Borders.SINGLE);
assertEquals(STBorder.SINGLE, borderTop.getVal());
}
@ -119,8 +127,10 @@ public class TestXWPFParagraph extends TestCase {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
assertEquals(STJc.LEFT.intValue(), p.getAlignment().getValue());
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
CTJc align = ppr.addNewJc();
align.setVal(STJc.CENTER);
@ -130,20 +140,58 @@ public class TestXWPFParagraph extends TestCase {
assertEquals(STJc.BOTH, ppr.getJc().getVal());
}
public void testSetGetSpacing() {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
assertEquals(-1, p.getSpacingAfter());
//TEST ALL OTHERS POSSIBLE COMBINATIONS
CTSpacing spacing = ppr.addNewSpacing();
spacing.setAfter(new BigInteger("10"));
assertEquals(10, p.getSpacingAfter().longValue());
assertEquals(10, p.getSpacingAfter());
p.setSpacingAfter(new BigInteger("100"));
assertEquals(100, spacing.getAfter().longValue());
p.setSpacingAfter(100);
assertEquals(100, spacing.getAfter().intValue());
}
public void testSetGetSpacingLineRule() {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
CTP ctp = p.getCTP();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
assertEquals(STLineSpacingRule.INT_AUTO, p.getSpacingLineRule().getValue());
CTSpacing spacing = ppr.addNewSpacing();
spacing.setLineRule(STLineSpacingRule.AT_LEAST);
assertEquals(LineSpacingRule.AT_LEAST, p.getSpacingLineRule());
p.setSpacingAfter(100);
assertEquals(100, spacing.getAfter().intValue());
}
public void testSetGetIndentation() {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
assertEquals(-1, p.getIndentationLeft());
CTP ctp = p.getCTP();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
assertEquals(-1, p.getIndentationLeft());
CTInd ind = ppr.addNewInd();
ind.setLeft(new BigInteger("10"));
assertEquals(10, p.getIndentationLeft());
p.setIndentationLeft(100);
assertEquals(100, ind.getLeft().intValue());
}
public void testSetGetVerticalAlignment() {
@ -152,7 +200,7 @@ public class TestXWPFParagraph extends TestCase {
XWPFParagraph p = doc.createParagraph();
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
CTTextAlignment txtAlign = ppr.addNewTextAlignment();
txtAlign.setVal(STTextAlignment.CENTER);
@ -167,7 +215,7 @@ public class TestXWPFParagraph extends TestCase {
XWPFParagraph p = doc.createParagraph();
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
CTOnOff wordWrap = ppr.addNewWordWrap();
wordWrap.setVal(STOnOff.FALSE);
@ -183,7 +231,7 @@ public class TestXWPFParagraph extends TestCase {
XWPFParagraph p = doc.createParagraph();
CTP ctp = p.getCTP();
CTPPr ppr = ctp.addNewPPr();
CTPPr ppr = ctp.getPPr()== null? ctp.addNewPPr() : ctp.getPPr();
CTOnOff pageBreak = ppr.addNewPageBreakBefore();
pageBreak.setVal(STOnOff.FALSE);

View File

@ -20,8 +20,10 @@ import java.math.BigInteger;
import junit.framework.TestCase;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
@ -42,6 +44,23 @@ public class TestXWPFRun extends TestCase {
}
public void testSetGetText() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(3,ctRun.sizeOfTArray());
XWPFRun run = new XWPFRun(ctRun, p);
assertEquals("TEST2 STRING",run.getText(1));
run.setText("NEW STRING",0);
assertEquals("NEW STRING",run.getText(0));
//run.setText("xxx",14);
//fail("Position wrong");
}
public void testSetGetBold() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewB().setVal(STOnOff.TRUE);
@ -118,9 +137,9 @@ public class TestXWPFRun extends TestCase {
rpr.addNewSz().setVal(new BigInteger("14"));
XWPFRun run = new XWPFRun(ctRun, p);
assertEquals(7, run.getFontSize().longValue());
assertEquals(7, run.getFontSize());
run.setFontSize(new BigInteger("24"));
run.setFontSize(24);
assertEquals(48, rpr.getSz().getVal().longValue());
}
@ -130,11 +149,48 @@ public class TestXWPFRun extends TestCase {
rpr.addNewPosition().setVal(new BigInteger("4000"));
XWPFRun run = new XWPFRun(ctRun, p);
assertEquals(4000, run.getTextPosition().longValue());
assertEquals(4000, run.getTextPosition());
run.setTextPosition(new BigInteger("2400"));
run.setTextPosition(2400);
assertEquals(2400, rpr.getPosition().getVal().longValue());
}
public void testAddCarriageReturn() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfCrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
run.setText("T1");
run.addCarriageReturn();
run.addCarriageReturn();
run.setText("T2");
run.addCarriageReturn();
assertEquals(3, run.getCTR().getCrArray().length);
}
public void testAddPageBreak() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewBr();
ctRun.addNewT().setStringValue("TEST2 STRING");
CTBr breac=ctRun.addNewBr();
breac.setClear(STBrClear.LEFT);
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfBrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), p);
run.setText("TEXT1");
run.addBreak();
run.setText("TEXT2");
run.addBreak();
assertEquals(2, run.getCTR().sizeOfBrArray());
}
}

View File

@ -0,0 +1,117 @@
/* ====================================================================
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.math.BigInteger;
import junit.framework.TestCase;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
/**
* Tests for XWPF Run
*/
public class TestXWPFTable extends TestCase {
protected void setUp() {
/*
XWPFDocument doc = new XWPFDocument();
p = doc.createParagraph();
this.ctRun = CTR.Factory.newInstance();
*/
}
public void testConstructor() {
CTTbl ctTable=CTTbl.Factory.newInstance();
XWPFTable xtab=new XWPFTable(ctTable);
assertNotNull(xtab);
assertEquals(1,ctTable.sizeOfTrArray());
assertEquals(1,ctTable.getTrArray(0).sizeOfTcArray());
assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
ctTable=CTTbl.Factory.newInstance();
xtab=new XWPFTable(ctTable, 3,2);
assertNotNull(xtab);
assertEquals(3,ctTable.sizeOfTrArray());
assertEquals(2,ctTable.getTrArray(0).sizeOfTcArray());
assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
}
public void testGetText(){
CTTbl table = CTTbl.Factory.newInstance();
CTRow row=table.addNewTr();
CTTc cell=row.addNewTc();
CTP paragraph=cell.addNewP();
CTR run=paragraph.addNewR();
CTText text=run.addNewT();
text.setStringValue("finally I can write!");
XWPFTable xtab=new XWPFTable(table);
assertEquals("finally I can write!\n",xtab.getText());
}
public void testCreateRow(){
CTTbl table = CTTbl.Factory.newInstance();
CTRow r1=table.addNewTr();
r1.addNewTc().addNewP();
r1.addNewTc().addNewP();
CTRow r2=table.addNewTr();
r2.addNewTc().addNewP();
r2.addNewTc().addNewP();
CTRow r3=table.addNewTr();
r3.addNewTc().addNewP();
r3.addNewTc().addNewP();
XWPFTable xtab=new XWPFTable(table);
assertEquals(3,xtab.getNumberOfRows());
assertNotNull(xtab.getRow(2));
//add a new row
xtab.createRow();
//check number of cols
assertEquals(2,table.getTrArray(0).sizeOfTcArray());
//check creation of first row
xtab=new XWPFTable(CTTbl.Factory.newInstance());
assertEquals(1,xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
}
public void testSetGetWidth(){
CTTbl table = CTTbl.Factory.newInstance();
table.addNewTblPr().addNewTblW().setW(new BigInteger("1000"));
XWPFTable xtab=new XWPFTable(table);
assertEquals(1000,xtab.getWidth());
xtab.setWidth(100);
assertEquals(100,table.getTblPr().getTblW().getW().intValue());
}
}