From ab19d2e09c4c234520fa8d0795718dec4feb1ee1 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 13 Jun 2013 18:34:54 +0000 Subject: [PATCH] Fix and update JavaDoc entries, and correct areas with wildy inconsistent whitespace / style to the surrounding code git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1492804 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFFootnotes.java | 201 ++++--- .../apache/poi/xwpf/usermodel/XWPFTable.java | 498 +++++++++--------- .../poi/xwpf/usermodel/XWPFTableCell.java | 437 ++++++++------- .../poi/xwpf/usermodel/XWPFTableRow.java | 207 ++++---- 4 files changed, 661 insertions(+), 682 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnotes.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnotes.java index 93bc8bfc7..2cb8bd2e6 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnotes.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnotes.java @@ -41,121 +41,118 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument; /** * Looks after the collection of Footnotes for a document - * - * @author Mike McEuen (mceuen@hp.com) */ public class XWPFFootnotes extends POIXMLDocumentPart { - private List listFootnote = new ArrayList(); - private CTFootnotes ctFootnotes; + private List listFootnote = new ArrayList(); + private CTFootnotes ctFootnotes; - protected XWPFDocument document; + protected XWPFDocument document; - /** - * Construct XWPFFootnotes from a package part - * - * @param part the package part holding the data of the footnotes, - * @param rel the package relationship of type "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" - */ - public XWPFFootnotes(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{ - super(part, rel); + /** + * Construct XWPFFootnotes from a package part + * + * @param part the package part holding the data of the footnotes, + * @param rel the package relationship of type "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" + */ + public XWPFFootnotes(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{ + super(part, rel); + } + + /** + * Construct XWPFFootnotes from scratch for a new document. + */ + public XWPFFootnotes() { + } + + /** + * Read document + */ + @Override + protected void onDocumentRead () throws IOException { + FootnotesDocument notesDoc; + try { + InputStream is = getPackagePart().getInputStream(); + notesDoc = FootnotesDocument.Factory.parse(is); + ctFootnotes = notesDoc.getFootnotes(); + } catch (XmlException e) { + throw new POIXMLException(); } - /** - * Construct XWPFFootnotes from scratch for a new document. - */ - public XWPFFootnotes() { + //get any Footnote + for(CTFtnEdn note : ctFootnotes.getFootnoteList()) { + listFootnote.add(new XWPFFootnote(note, this)); } + } - /** - * Read document - */ - @Override - protected void onDocumentRead () throws IOException { - FootnotesDocument notesDoc; - try { - InputStream is = getPackagePart().getInputStream(); - notesDoc = FootnotesDocument.Factory.parse(is); - ctFootnotes = notesDoc.getFootnotes(); - } catch (XmlException e) { - throw new POIXMLException(); - } - - //get any Footnote - for(CTFtnEdn note : ctFootnotes.getFootnoteList()) { - listFootnote.add(new XWPFFootnote(note, this)); - } - } - - @Override - protected void commit() throws IOException { - XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); - xmlOptions.setSaveSyntheticDocumentElement(new QName(CTFootnotes.type.getName().getNamespaceURI(), "footnotes")); - Map map = new HashMap(); - map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); - map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); - xmlOptions.setSaveSuggestedPrefixes(map); - PackagePart part = getPackagePart(); - OutputStream out = part.getOutputStream(); - ctFootnotes.save(out, xmlOptions); - out.close(); - } + @Override + protected void commit() throws IOException { + XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); + xmlOptions.setSaveSyntheticDocumentElement(new QName(CTFootnotes.type.getName().getNamespaceURI(), "footnotes")); + Map map = new HashMap(); + map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); + map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); + xmlOptions.setSaveSuggestedPrefixes(map); + PackagePart part = getPackagePart(); + OutputStream out = part.getOutputStream(); + ctFootnotes.save(out, xmlOptions); + out.close(); + } - public List getFootnotesList() { - return listFootnote; - } + public List getFootnotesList() { + return listFootnote; + } - public XWPFFootnote getFootnoteById(int id) { - for(XWPFFootnote note : listFootnote) { - if(note.getCTFtnEdn().getId().intValue() == id) - return note; - } - return null; - } - - /** - * Sets the ctFootnotes - * @param footnotes - */ - public void setFootnotes(CTFootnotes footnotes) { - ctFootnotes = footnotes; + public XWPFFootnote getFootnoteById(int id) { + for(XWPFFootnote note : listFootnote) { + if(note.getCTFtnEdn().getId().intValue() == id) + return note; } + return null; + } - /** - * add an XWPFFootnote to the document - * @param footnote - * @throws IOException - */ - public void addFootnote(XWPFFootnote footnote){ - listFootnote.add(footnote); - ctFootnotes.addNewFootnote().set(footnote.getCTFtnEdn()); - } + /** + * Sets the ctFootnotes + * @param footnotes + */ + public void setFootnotes(CTFootnotes footnotes) { + ctFootnotes = footnotes; + } - /** - * add a footnote to the document - * @param note - * @throws IOException - */ - public XWPFFootnote addFootnote(CTFtnEdn note){ - CTFtnEdn newNote = ctFootnotes.addNewFootnote(); - newNote.set(note); - XWPFFootnote xNote = new XWPFFootnote(newNote, this); - listFootnote.add(xNote); - return xNote; - } + /** + * add an XWPFFootnote to the document + * @param footnote + * @throws IOException + */ + public void addFootnote(XWPFFootnote footnote){ + listFootnote.add(footnote); + ctFootnotes.addNewFootnote().set(footnote.getCTFtnEdn()); + } - public void setXWPFDocument(XWPFDocument doc) { - document = doc; - } + /** + * add a footnote to the document + * @param note + * @throws IOException + */ + public XWPFFootnote addFootnote(CTFtnEdn note){ + CTFtnEdn newNote = ctFootnotes.addNewFootnote(); + newNote.set(note); + XWPFFootnote xNote = new XWPFFootnote(newNote, this); + listFootnote.add(xNote); + return xNote; + } - /** - * @see org.apache.poi.xwpf.usermodel.IBody#getPart() - */ - public XWPFDocument getXWPFDocument() { - if ( document != null) { - return document; - } else { - return (XWPFDocument)getParent(); - } - } -}//end class + public void setXWPFDocument(XWPFDocument doc) { + document = doc; + } + /** + * @see org.apache.poi.xwpf.usermodel.IBody#getPart() + */ + public XWPFDocument getXWPFDocument() { + if ( document != null) { + return document; + } else { + return (XWPFDocument)getParent(); + } + } +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java index 22e798df2..b62f86b34 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java @@ -39,24 +39,11 @@ 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. - *

- * 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) - * @author Gregg Morris (gregg dot morris at gmail dot com) - added - * setStyleID() - * getRowBandSize(), setRowBandSize() - * getColBandSize(), setColBandSize() - * getInsideHBorderType(), getInsideHBorderSize(), getInsideHBorderSpace(), getInsideHBorderColor() - * getInsideVBorderType(), getInsideVBorderSize(), getInsideVBorderSpace(), getInsideVBorderColor() - * setInsideHBorder(), setInsideVBorder() - * getCellMarginTop(), getCellMarginLeft(), getCellMarginBottom(), getCellMarginRight() - * setCellMargins() + *

Sketch of XWPFTable class. Only table's text is being hold.

+ *

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.

*/ -public class XWPFTable implements IBodyElement{ - +public class XWPFTable implements IBodyElement { protected StringBuffer text = new StringBuffer(); private CTTbl ctTbl; protected List tableRows; @@ -157,10 +144,10 @@ public class XWPFTable implements IBodyElement{ borders.addNewTop().setVal(STBorder.SINGLE); /* - * CTTblGrid tblgrid=table.addNewTblGrid(); - * tblgrid.addNewGridCol().setW(new BigInteger("2000")); - */ - getRows(); + * CTTblGrid tblgrid=table.addNewTblGrid(); + * tblgrid.addNewGridCol().setW(new BigInteger("2000")); + */ + getRows(); } /** @@ -227,8 +214,7 @@ public class XWPFTable implements IBodyElement{ */ public void setWidth(int width) { CTTblPr tblPr = getTrPr(); - CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr - .addNewTblW(); + CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); tblWidth.setW(new BigInteger("" + width)); } @@ -291,167 +277,167 @@ public class XWPFTable implements IBodyElement{ } public XWPFBorderType getInsideHBorderType() { - XWPFBorderType bt = null; + XWPFBorderType bt = null; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideH()) { - CTBorder border = ctb.getInsideH(); - bt = stBorderTypeMap.get(border.getVal().intValue()); - } - } - return bt; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideH()) { + CTBorder border = ctb.getInsideH(); + bt = stBorderTypeMap.get(border.getVal().intValue()); + } + } + return bt; } public int getInsideHBorderSize() { - int size = -1; + int size = -1; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideH()) { - CTBorder border = ctb.getInsideH(); - size = border.getSz().intValue(); - } - } - return size; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideH()) { + CTBorder border = ctb.getInsideH(); + size = border.getSz().intValue(); + } + } + return size; } public int getInsideHBorderSpace() { - int space = -1; + int space = -1; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideH()) { - CTBorder border = ctb.getInsideH(); - space = border.getSpace().intValue(); - } - } - return space; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideH()) { + CTBorder border = ctb.getInsideH(); + space = border.getSpace().intValue(); + } + } + return space; } public String getInsideHBorderColor() { - String color = null; + String color = null; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideH()) { - CTBorder border = ctb.getInsideH(); - color = border.xgetColor().getStringValue(); - } - } - return color; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideH()) { + CTBorder border = ctb.getInsideH(); + color = border.xgetColor().getStringValue(); + } + } + return color; } public XWPFBorderType getInsideVBorderType() { - XWPFBorderType bt = null; + XWPFBorderType bt = null; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideV()) { - CTBorder border = ctb.getInsideV(); - bt = stBorderTypeMap.get(border.getVal().intValue()); - } - } - return bt; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideV()) { + CTBorder border = ctb.getInsideV(); + bt = stBorderTypeMap.get(border.getVal().intValue()); + } + } + return bt; } public int getInsideVBorderSize() { - int size = -1; + int size = -1; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideV()) { - CTBorder border = ctb.getInsideV(); - size = border.getSz().intValue(); - } - } - return size; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideV()) { + CTBorder border = ctb.getInsideV(); + size = border.getSz().intValue(); + } + } + return size; } public int getInsideVBorderSpace() { - int space = -1; + int space = -1; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideV()) { - CTBorder border = ctb.getInsideV(); - space = border.getSpace().intValue(); - } - } - return space; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideV()) { + CTBorder border = ctb.getInsideV(); + space = border.getSpace().intValue(); + } + } + return space; } public String getInsideVBorderColor() { - String color = null; + String color = null; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblBorders()) { - CTTblBorders ctb = tblPr.getTblBorders(); - if (ctb.isSetInsideV()) { - CTBorder border = ctb.getInsideV(); - color = border.xgetColor().getStringValue(); - } - } - return color; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblBorders()) { + CTTblBorders ctb = tblPr.getTblBorders(); + if (ctb.isSetInsideV()) { + CTBorder border = ctb.getInsideV(); + color = border.xgetColor().getStringValue(); + } + } + return color; } public int getRowBandSize() { - int size = 0; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblStyleRowBandSize()) { - CTDecimalNumber rowSize = tblPr.getTblStyleRowBandSize(); - size = rowSize.getVal().intValue(); - } - return size; + int size = 0; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblStyleRowBandSize()) { + CTDecimalNumber rowSize = tblPr.getTblStyleRowBandSize(); + size = rowSize.getVal().intValue(); + } + return size; } public void setRowBandSize(int size) { - CTTblPr tblPr = getTrPr(); - CTDecimalNumber rowSize = tblPr.isSetTblStyleRowBandSize() ? tblPr.getTblStyleRowBandSize() : tblPr.addNewTblStyleRowBandSize(); - rowSize.setVal(BigInteger.valueOf(size)); + CTTblPr tblPr = getTrPr(); + CTDecimalNumber rowSize = tblPr.isSetTblStyleRowBandSize() ? tblPr.getTblStyleRowBandSize() : tblPr.addNewTblStyleRowBandSize(); + rowSize.setVal(BigInteger.valueOf(size)); } public int getColBandSize() { - int size = 0; - CTTblPr tblPr = getTrPr(); - if (tblPr.isSetTblStyleColBandSize()) { - CTDecimalNumber colSize = tblPr.getTblStyleColBandSize(); - size = colSize.getVal().intValue(); - } - return size; + int size = 0; + CTTblPr tblPr = getTrPr(); + if (tblPr.isSetTblStyleColBandSize()) { + CTDecimalNumber colSize = tblPr.getTblStyleColBandSize(); + size = colSize.getVal().intValue(); + } + return size; } public void setColBandSize(int size) { - CTTblPr tblPr = getTrPr(); - CTDecimalNumber colSize = tblPr.isSetTblStyleColBandSize() ? tblPr.getTblStyleColBandSize() : tblPr.addNewTblStyleColBandSize(); - colSize.setVal(BigInteger.valueOf(size)); + CTTblPr tblPr = getTrPr(); + CTDecimalNumber colSize = tblPr.isSetTblStyleColBandSize() ? tblPr.getTblStyleColBandSize() : tblPr.addNewTblStyleColBandSize(); + colSize.setVal(BigInteger.valueOf(size)); } public void setInsideHBorder(XWPFBorderType type, int size, int space, String rgbColor) { - CTTblPr tblPr = getTrPr(); - CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders(); - CTBorder b = ctb.isSetInsideH() ? ctb.getInsideH() : ctb.addNewInsideH(); - b.setVal(xwpfBorderTypeMap.get(type)); - b.setSz(BigInteger.valueOf(size)); - b.setSpace(BigInteger.valueOf(space)); - b.setColor(rgbColor); + CTTblPr tblPr = getTrPr(); + CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders(); + CTBorder b = ctb.isSetInsideH() ? ctb.getInsideH() : ctb.addNewInsideH(); + b.setVal(xwpfBorderTypeMap.get(type)); + b.setSz(BigInteger.valueOf(size)); + b.setSpace(BigInteger.valueOf(space)); + b.setColor(rgbColor); } public void setInsideVBorder(XWPFBorderType type, int size, int space, String rgbColor) { - CTTblPr tblPr = getTrPr(); - CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders(); - CTBorder b = ctb.isSetInsideV() ? ctb.getInsideV() : ctb.addNewInsideV(); - b.setVal(xwpfBorderTypeMap.get(type)); - b.setSz(BigInteger.valueOf(size)); - b.setSpace(BigInteger.valueOf(space)); - b.setColor(rgbColor); + CTTblPr tblPr = getTrPr(); + CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders(); + CTBorder b = ctb.isSetInsideV() ? ctb.getInsideV() : ctb.addNewInsideV(); + b.setVal(xwpfBorderTypeMap.get(type)); + b.setSz(BigInteger.valueOf(size)); + b.setSpace(BigInteger.valueOf(space)); + b.setColor(rgbColor); } public int getCellMarginTop() { @@ -494,141 +480,141 @@ public class XWPFTable implements IBodyElement{ } public int getCellMarginRight() { - int margin = 0; - CTTblPr tblPr = getTrPr(); - CTTblCellMar tcm = tblPr.getTblCellMar(); - if (tcm != null) { - CTTblWidth tw = tcm.getRight(); - if (tw != null) { - margin = tw.getW().intValue(); - } - } - return margin; + int margin = 0; + CTTblPr tblPr = getTrPr(); + CTTblCellMar tcm = tblPr.getTblCellMar(); + if (tcm != null) { + CTTblWidth tw = tcm.getRight(); + if (tw != null) { + margin = tw.getW().intValue(); + } + } + return margin; } public void setCellMargins(int top, int left, int bottom, int right) { - CTTblPr tblPr = getTrPr(); - CTTblCellMar tcm = tblPr.isSetTblCellMar() ? tblPr.getTblCellMar() : tblPr.addNewTblCellMar(); + CTTblPr tblPr = getTrPr(); + CTTblCellMar tcm = tblPr.isSetTblCellMar() ? tblPr.getTblCellMar() : tblPr.addNewTblCellMar(); - CTTblWidth tw = tcm.isSetLeft() ? tcm.getLeft() : tcm.addNewLeft(); - tw.setType(STTblWidth.DXA); - tw.setW(BigInteger.valueOf(left)); + CTTblWidth tw = tcm.isSetLeft() ? tcm.getLeft() : tcm.addNewLeft(); + tw.setType(STTblWidth.DXA); + tw.setW(BigInteger.valueOf(left)); - tw = tcm.isSetTop() ? tcm.getTop() : tcm.addNewTop(); - tw.setType(STTblWidth.DXA); - tw.setW(BigInteger.valueOf(top)); + tw = tcm.isSetTop() ? tcm.getTop() : tcm.addNewTop(); + tw.setType(STTblWidth.DXA); + tw.setW(BigInteger.valueOf(top)); - tw = tcm.isSetBottom() ? tcm.getBottom() : tcm.addNewBottom(); - tw.setType(STTblWidth.DXA); - tw.setW(BigInteger.valueOf(bottom)); + tw = tcm.isSetBottom() ? tcm.getBottom() : tcm.addNewBottom(); + tw.setType(STTblWidth.DXA); + tw.setW(BigInteger.valueOf(bottom)); - tw = tcm.isSetRight() ? tcm.getRight() : tcm.addNewRight(); - tw.setType(STTblWidth.DXA); - tw.setW(BigInteger.valueOf(right)); + tw = tcm.isSetRight() ? tcm.getRight() : tcm.addNewRight(); + tw.setType(STTblWidth.DXA); + tw.setW(BigInteger.valueOf(right)); } - + /** * add a new Row to the table * * @param row the row which should be added */ - public void addRow(XWPFTableRow row){ - ctTbl.addNewTr(); - ctTbl.setTrArray(getNumberOfRows()-1, row.getCtRow()); - tableRows.add(row); - } - - /** - * add a new Row to the table - * at position pos - * @param row the row which should be added - */ - public boolean addRow(XWPFTableRow row, int pos){ - if(pos >= 0 && pos <= tableRows.size()){ - ctTbl.insertNewTr(pos); - ctTbl.setTrArray(pos,row.getCtRow()); - tableRows.add(pos, row); - return true; - } - return false; - } - - /** - * inserts a new tablerow - * @param pos - * @return the inserted row - */ - public XWPFTableRow insertNewTableRow(int pos){ - if(pos >= 0 && pos <= tableRows.size()){ - CTRow row = ctTbl.insertNewTr(pos); - XWPFTableRow tableRow = new XWPFTableRow(row, this); - tableRows.add(pos, tableRow); - return tableRow; - } - return null; - } - - - /** - * Remove a row at position pos from the table - * @param pos position the Row in the Table - */ - public boolean removeRow(int pos) throws IndexOutOfBoundsException { - if (pos >= 0 && pos < tableRows.size()) { - if (ctTbl.sizeOfTrArray() > 0) { - ctTbl.removeTr(pos); - } - tableRows.remove(pos); - return true; - } - return false; - } - - public List getRows() { - return tableRows; - } + public void addRow(XWPFTableRow row){ + ctTbl.addNewTr(); + ctTbl.setTrArray(getNumberOfRows()-1, row.getCtRow()); + tableRows.add(row); + } + + /** + * add a new Row to the table + * at position pos + * @param row the row which should be added + */ + public boolean addRow(XWPFTableRow row, int pos){ + if(pos >= 0 && pos <= tableRows.size()){ + ctTbl.insertNewTr(pos); + ctTbl.setTrArray(pos,row.getCtRow()); + tableRows.add(pos, row); + return true; + } + return false; + } + + /** + * inserts a new tablerow + * @param pos + * @return the inserted row + */ + public XWPFTableRow insertNewTableRow(int pos){ + if(pos >= 0 && pos <= tableRows.size()){ + CTRow row = ctTbl.insertNewTr(pos); + XWPFTableRow tableRow = new XWPFTableRow(row, this); + tableRows.add(pos, tableRow); + return tableRow; + } + return null; + } - /** - * returns the type of the BodyElement Table - * @see org.apache.poi.xwpf.usermodel.IBodyElement#getElementType() - */ - public BodyElementType getElementType() { - return BodyElementType.TABLE; - } + /** + * Remove a row at position pos from the table + * @param pos position the Row in the Table + */ + public boolean removeRow(int pos) throws IndexOutOfBoundsException { + if (pos >= 0 && pos < tableRows.size()) { + if (ctTbl.sizeOfTrArray() > 0) { + ctTbl.removeTr(pos); + } + tableRows.remove(pos); + return true; + } + return false; + } - public IBody getBody() - { - return part; - } + public List getRows() { + return tableRows; + } - /** - * returns the part of the bodyElement - * @see org.apache.poi.xwpf.usermodel.IBody#getPart() - */ - public POIXMLDocumentPart getPart() { - if(part != null){ - return part.getPart(); - } - return null; - } - /** - * returns the partType of the bodyPart which owns the bodyElement - * @see org.apache.poi.xwpf.usermodel.IBody#getPartType() - */ - public BodyType getPartType() { - return part.getPartType(); - } + /** + * returns the type of the BodyElement Table + * @see org.apache.poi.xwpf.usermodel.IBodyElement#getElementType() + */ + public BodyElementType getElementType() { + return BodyElementType.TABLE; + } - /** - * returns the XWPFRow which belongs to the CTRow row - * if this row is not existing in the table null will be returned - */ - public XWPFTableRow getRow(CTRow row) { - for(int i=0; i(); - paragraphs = new ArrayList(); - tables = new ArrayList(); - - XmlCursor cursor = ctTc.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); - } - } - cursor.dispose(); + this.ctTc = cell; + this.part = part; + this.tableRow = tableRow; + // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt. + if(cell.getPList().size()<1) + cell.addNewP(); + bodyElements = new ArrayList(); + paragraphs = new ArrayList(); + tables = new ArrayList(); + + XmlCursor cursor = ctTc.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); + } + } + cursor.dispose(); } @Internal public CTTc getCTTc() { - return ctTc; + return ctTc; } /** @@ -168,12 +165,12 @@ public class XWPFTableCell implements IBody { * XWPFParagraph with the correspondig CTP p */ public XWPFParagraph getParagraph(CTP p){ - for (XWPFParagraph paragraph : paragraphs) { - if(p.equals(paragraph.getCTP())){ - return paragraph; - } - } - return null; + for (XWPFParagraph paragraph : paragraphs) { + if(p.equals(paragraph.getCTP())){ + return paragraph; + } + } + return null; } public void setText(String text) { @@ -204,15 +201,15 @@ public class XWPFTableCell implements IBody { * @return RGB string of cell color */ public String getColor() { - String color = null; - CTTcPr tcpr = ctTc.getTcPr(); - if (tcpr != null) { - CTShd ctshd = tcpr.getShd(); - if (ctshd != null) { - color = ctshd.xgetFill().getStringValue(); - } - } - return color; + String color = null; + CTTcPr tcpr = ctTc.getTcPr(); + if (tcpr != null) { + CTShd ctshd = tcpr.getShd(); + if (ctshd != null) { + color = ctshd.xgetFill().getStringValue(); + } + } + return color; } /** @@ -230,13 +227,13 @@ public class XWPFTableCell implements IBody { * @return the cell alignment enum value */ public XWPFVertAlign getVerticalAlignment() { - XWPFVertAlign vAlign = null; - CTTcPr tcpr = ctTc.getTcPr(); - if (ctTc != null) { - CTVerticalJc va = tcpr.getVAlign(); - vAlign = stVertAlignTypeMap.get(va.getVal().intValue()); - } - return vAlign; + XWPFVertAlign vAlign = null; + CTTcPr tcpr = ctTc.getTcPr(); + if (ctTc != null) { + CTVerticalJc va = tcpr.getVAlign(); + vAlign = stVertAlignTypeMap.get(va.getVal().intValue()); + } + return vAlign; } /** @@ -245,97 +242,98 @@ public class XWPFTableCell implements IBody { * @return the inserted paragraph */ public XWPFParagraph insertNewParagraph(XmlCursor cursor){ - if(!isCursorInTableCell(cursor)) - return null; - - 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; + if(!isCursorInTableCell(cursor)) { + return null; + } + + 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; } - public XWPFTable insertNewTbl(XmlCursor cursor) { - if(isCursorInTableCell(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 - */ - private boolean isCursorInTableCell(XmlCursor cursor) { - XmlCursor verify = cursor.newCursor(); - verify.toParent(); - if(verify.getObject() == this.ctTc){ - return true; - } - return false; + public XWPFTable insertNewTbl(XmlCursor cursor) { + if(isCursorInTableCell(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; + } - - - /** - * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int) - */ - public XWPFParagraph getParagraphArray(int pos) { - if(pos > 0 && pos < paragraphs.size()){ - return paragraphs.get(pos); - } - return null; + /** + * verifies that cursor is on the right position + */ + private boolean isCursorInTableCell(XmlCursor cursor) { + XmlCursor verify = cursor.newCursor(); + verify.toParent(); + if(verify.getObject() == this.ctTc){ + return true; } + return false; + } + + + + /** + * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int) + */ + public XWPFParagraph getParagraphArray(int pos) { + if(pos > 0 && pos < paragraphs.size()){ + return paragraphs.get(pos); + } + return null; + } /** * get the to which the TableCell belongs @@ -346,101 +344,100 @@ public class XWPFTableCell implements IBody { return tableRow.getTable().getPart(); } + /** + * @see org.apache.poi.xwpf.usermodel.IBody#getPartType() + */ + public BodyType getPartType() { + return BodyType.TABLECELL; + } - /** - * @see org.apache.poi.xwpf.usermodel.IBody#getPartType() - */ - public BodyType getPartType() { - return BodyType.TABLECELL; + + /** + * get a table by its CTTbl-Object + * @see org.apache.poi.xwpf.usermodel.IBody#getTable(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) + */ + public XWPFTable getTable(CTTbl ctTable) { + for(int i=0; i 0 && pos < tables.size()){ + return tables.get(pos); } + return null; + } - /** - * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) - */ - public XWPFTable getTableArray(int pos) { - if(pos > 0 && pos < tables.size()){ - return tables.get(pos); - } - return null; + /** + * @see org.apache.poi.xwpf.usermodel.IBody#getTables() + */ + public List getTables() { + return Collections.unmodifiableList(tables); + } + + + /** + * inserts an existing XWPFTable to the arrays bodyElements and tables + * @see org.apache.poi.xwpf.usermodel.IBody#insertTable(int, org.apache.poi.xwpf.usermodel.XWPFTable) + */ + public void insertTable(int pos, XWPFTable table) { + bodyElements.add(pos, table); + int i; + for (i = 0; i < ctTc.getTblList().size(); i++) { + CTTbl tbl = ctTc.getTblArray(i); + if(tbl == table.getCTTbl()){ + break; + } } + tables.add(i, table); + } - - /** - * @see org.apache.poi.xwpf.usermodel.IBody#getTables() - */ - public List getTables() { - return Collections.unmodifiableList(tables); + public String getText(){ + StringBuffer text = new StringBuffer(); + for (XWPFParagraph p : paragraphs) { + text.append(p.getText()); } + return text.toString(); + } - /** - * inserts an existing XWPFTable to the arrays bodyElements and tables - * @see org.apache.poi.xwpf.usermodel.IBody#insertTable(int, org.apache.poi.xwpf.usermodel.XWPFTable) - */ - public void insertTable(int pos, XWPFTable table) { - bodyElements.add(pos, table); - int i; - for (i = 0; i < ctTc.getTblList().size(); i++) { - CTTbl tbl = ctTc.getTblArray(i); - if(tbl == table.getCTTbl()){ - break; - } - } - tables.add(i, table); + /** + * get the TableCell which belongs to the TableCell + */ + public XWPFTableCell getTableCell(CTTc cell) { + XmlCursor cursor = cell.newCursor(); + cursor.toParent(); + XmlObject o = cursor.getObject(); + if(!(o instanceof CTRow)){ + return null; } - - public String getText(){ - StringBuffer text = new StringBuffer(); - for (XWPFParagraph p : paragraphs) { - text.append(p.getText()); - } - return text.toString(); + CTRow row = (CTRow)o; + cursor.toParent(); + o = cursor.getObject(); + cursor.dispose(); + if(! (o instanceof CTTbl)){ + return null; } - - - /** - * get the TableCell which belongs to the TableCell - */ - 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(); - cursor.dispose(); - 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 (tableRow == null) { - return null; - } - return tableRow.getTableCell(cell); + CTTbl tbl = (CTTbl) o; + XWPFTable table = getTable(tbl); + if(table == null){ + return null; } + XWPFTableRow tableRow = table.getRow(row); + if (tableRow == null) { + return null; + } + return tableRow.getTableCell(cell); + } public XWPFDocument getXWPFDocument() { - return part.getXWPFDocument(); + return part.getXWPFDocument(); } -}// end class +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index 9da230238..312de0540 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -30,24 +30,24 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; /** - * @author gisellabronzetti - * @author gregg morris - added removeCell(), setCantSplitRow(), setRepeatHeader() + * A row within an {@link XWPFTable}. Rows mostly just have + * sizings and stylings, the interesting content lives inside + * the child {@link XWPFTableCell}s */ public class XWPFTableRow { - private CTRow ctRow; private XWPFTable table; private List tableCells; public XWPFTableRow(CTRow row, XWPFTable table) { - this.table = table; - this.ctRow = row; - getTableCells(); + this.table = table; + this.ctRow = row; + getTableCells(); } @Internal public CTRow getCtRow() { - return ctRow; + return ctRow; } /** @@ -55,31 +55,31 @@ public class XWPFTableRow { * @return the newly created XWPFTableCell */ public XWPFTableCell createCell() { - XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody()); - tableCells.add(tableCell); - return tableCell; + XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody()); + tableCells.add(tableCell); + return tableCell; } public XWPFTableCell getCell(int pos) { - if (pos >= 0 && pos < ctRow.sizeOfTcArray()) { - return getTableCells().get(pos); - } - return null; + if (pos >= 0 && pos < ctRow.sizeOfTcArray()) { + return getTableCells().get(pos); + } + return null; } - + public void removeCell(int pos) { - if (pos >= 0 && pos < ctRow.sizeOfTcArray()) { - tableCells.remove(pos); - } + if (pos >= 0 && pos < ctRow.sizeOfTcArray()) { + tableCells.remove(pos); + } } /** * adds a new TableCell at the end of this tableRow */ public XWPFTableCell addNewTableCell(){ - CTTc cell = ctRow.addNewTc(); - XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody()); - tableCells.add(tableCell); - return tableCell; + CTTc cell = ctRow.addNewTc(); + XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody()); + tableCells.add(tableCell); + return tableCell; } /** @@ -93,9 +93,9 @@ public class XWPFTableRow { * @param height */ public void setHeight(int height) { - CTTrPr properties = getTrPr(); - CTHeight h = properties.sizeOfTrHeightArray() == 0 ? properties.addNewTrHeight() : properties.getTrHeightArray(0); - h.setVal(new BigInteger("" + height)); + CTTrPr properties = getTrPr(); + CTHeight h = properties.sizeOfTrHeightArray() == 0 ? properties.addNewTrHeight() : properties.getTrHeightArray(0); + h.setVal(new BigInteger("" + height)); } /** @@ -109,101 +109,100 @@ public class XWPFTableRow { * @return height */ public int getHeight() { - CTTrPr properties = getTrPr(); - return properties.sizeOfTrHeightArray() == 0 ? 0 : properties.getTrHeightArray(0).getVal().intValue(); + CTTrPr properties = getTrPr(); + return properties.sizeOfTrHeightArray() == 0 ? 0 : properties.getTrHeightArray(0).getVal().intValue(); } private CTTrPr getTrPr() { - return (ctRow.isSetTrPr()) ? ctRow.getTrPr() : ctRow.addNewTrPr(); + return (ctRow.isSetTrPr()) ? ctRow.getTrPr() : ctRow.addNewTrPr(); } - + public XWPFTable getTable(){ - return table; + return table; } - + /** * create and return a list of all XWPFTableCell * who belongs to this row * @return a list of {@link XWPFTableCell} */ public List getTableCells(){ - if(tableCells == null){ - List cells = new ArrayList(); - for (CTTc tableCell : ctRow.getTcList()) { - cells.add(new XWPFTableCell(tableCell, this, table.getBody())); - } - this.tableCells = cells; - } - return tableCells; + if(tableCells == null){ + List cells = new ArrayList(); + for (CTTc tableCell : ctRow.getTcList()) { + cells.add(new XWPFTableCell(tableCell, this, table.getBody())); + } + this.tableCells = cells; + } + return tableCells; } - /** - * returns the XWPFTableCell which belongs to the CTTC cell - * if there is no XWPFTableCell which belongs to the parameter CTTc cell null will be returned - */ - public XWPFTableCell getTableCell(CTTc cell) { - for(int i=0; i 0) { + CTOnOff onoff = trpr.getCantSplitList().get(0); + isCant = onoff.getVal().equals(STOnOff.ON); } + return isCant; + } - /** - * Return true if the "can't split row" value is true. The logic for this - * attribute is a little unusual: a TRUE value means DON'T allow rows to - * split, FALSE means allow rows to split. - * @return true if rows can't be split, false otherwise. - */ - public boolean isCantSplitRow() { - boolean isCant = false; - CTTrPr trpr = getTrPr(); - if (trpr.sizeOfCantSplitArray() > 0) { - CTOnOff onoff = trpr.getCantSplitList().get(0); - isCant = onoff.getVal().equals(STOnOff.ON); - } - return isCant; + /** + * This attribute controls whether to repeat a table's header row at the top + * of a table split across pages. + * @param repeat - if TRUE, repeat header row at the top of each page of table; + * if FALSE, don't repeat header row. + */ + public void setRepeatHeader(boolean repeat) { + CTTrPr trpr = getTrPr(); + CTOnOff onoff = trpr.addNewTblHeader(); + onoff.setVal(repeat ? STOnOff.ON : STOnOff.OFF); + } + + /** + * Return true if a table's header row should be repeated at the top of a + * table split across pages. + * @return true if table's header row should be repeated at the top of each + * page of table, false otherwise. + */ + public boolean isRepeatHeader() { + boolean repeat = false; + CTTrPr trpr = getTrPr(); + if (trpr.sizeOfTblHeaderArray() > 0) { + CTOnOff rpt = trpr.getTblHeaderList().get(0); + repeat = rpt.getVal().equals(STOnOff.ON); } - - /** - * This attribute controls whether to repeat a table's header row at the top - * of a table split across pages. - * @param repeat - if TRUE, repeat header row at the top of each page of table; - * if FALSE, don't repeat header row. - */ - public void setRepeatHeader(boolean repeat) { - CTTrPr trpr = getTrPr(); - CTOnOff onoff = trpr.addNewTblHeader(); - onoff.setVal(repeat ? STOnOff.ON : STOnOff.OFF); - } - - /** - * Return true if a table's header row should be repeated at the top of a - * table split across pages. - * @return true if table's header row should be repeated at the top of each - * page of table, false otherwise. - */ - public boolean isRepeatHeader() { - boolean repeat = false; - CTTrPr trpr = getTrPr(); - if (trpr.sizeOfTblHeaderArray() > 0) { - CTOnOff rpt = trpr.getTblHeaderList().get(0); - repeat = rpt.getVal().equals(STOnOff.ON); - } - return repeat; - } - -}// end class + return repeat; + } +}