close cursors in finally blocks

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896470 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-12-28 10:24:04 +00:00
parent 2996f82e98
commit 233fbadf0a
18 changed files with 460 additions and 328 deletions

View File

@ -139,10 +139,13 @@ public final class XSLFChart extends XDDFChart {
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor grCur = gr.newCursor();
grCur.toNextToken();
grCur.beginElement(new QName(CHART_URI, "chart"));
grCur.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, rID);
grCur.dispose();
try {
grCur.toNextToken();
grCur.beginElement(new QName(CHART_URI, "chart"));
grCur.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, rID);
} finally {
grCur.dispose();
}
gr.setUri(CHART_URI);
return frame;

View File

@ -191,9 +191,12 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
XmlObject[] obj = getGraphicalData().selectPath(xpath);
if (obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
id = c.getAttributeText(idQualifiedName);
c.dispose();
try {
QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
id = c.getAttributeText(idQualifiedName);
} finally {
c.dispose();
}
}
if (id == null) {
return null;
@ -251,8 +254,9 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
}
} catch (InvalidFormatException | IOException e) {
throw new POIXMLException(e);
} finally {
c.dispose();
}
c.dispose();
}
}
@ -287,8 +291,9 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
} catch (InvalidFormatException e){
throw new POIXMLException(e);
} finally {
c.dispose();
}
c.dispose();
}
}

View File

@ -251,44 +251,46 @@ public class XSLFObjectShape extends XSLFGraphicFrame implements ObjectShape<XSL
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
gr.setUri(OLE_URI);
XmlCursor grCur = gr.newCursor();
grCur.toEndToken();
grCur.beginElement(new QName(PML_NS, "oleObj"));
grCur.insertElement(new QName(PML_NS, "embed"));
try {
grCur.toEndToken();
grCur.beginElement(new QName(PML_NS, "oleObj"));
grCur.insertElement(new QName(PML_NS, "embed"));
CTGroupShape grpShp = CTGroupShape.Factory.newInstance();
CTPicture pic = grpShp.addNewPic();
CTPictureNonVisual nvPicPr = pic.addNewNvPicPr();
CTNonVisualDrawingProps cNvPr = nvPicPr.addNewCNvPr();
cNvPr.setName("");
cNvPr.setId(0);
nvPicPr.addNewCNvPicPr();
nvPicPr.addNewNvPr();
CTGroupShape grpShp = CTGroupShape.Factory.newInstance();
CTPicture pic = grpShp.addNewPic();
CTPictureNonVisual nvPicPr = pic.addNewNvPicPr();
CTNonVisualDrawingProps cNvPr = nvPicPr.addNewCNvPr();
cNvPr.setName("");
cNvPr.setId(0);
nvPicPr.addNewCNvPicPr();
nvPicPr.addNewNvPr();
CTBlipFillProperties blip = pic.addNewBlipFill();
blip.addNewBlip().setEmbed(picRel);
blip.addNewStretch().addNewFillRect();
CTShapeProperties spPr = pic.addNewSpPr();
CTTransform2D xfrm = spPr.addNewXfrm();
CTPoint2D off = xfrm.addNewOff();
off.setX(1270000);
off.setY(1270000);
CTPositiveSize2D xext = xfrm.addNewExt();
xext.setCx(1270000);
xext.setCy(1270000);
CTBlipFillProperties blip = pic.addNewBlipFill();
blip.addNewBlip().setEmbed(picRel);
blip.addNewStretch().addNewFillRect();
CTShapeProperties spPr = pic.addNewSpPr();
CTTransform2D xfrm = spPr.addNewXfrm();
CTPoint2D off = xfrm.addNewOff();
off.setX(1270000);
off.setY(1270000);
CTPositiveSize2D xext = xfrm.addNewExt();
xext.setCx(1270000);
xext.setCy(1270000);
spPr.addNewPrstGeom().setPrst(STShapeType.RECT);
XmlCursor picCur = grpShp.newCursor();
picCur.toStartDoc();
picCur.moveXmlContents(grCur);
picCur.dispose();
grCur.dispose();
spPr.addNewPrstGeom().setPrst(STShapeType.RECT);
XmlCursor picCur = grpShp.newCursor();
try {
picCur.toStartDoc();
picCur.moveXmlContents(grCur);
} finally {
picCur.dispose();
}
} finally {
grCur.dispose();
}
return frame;
}

View File

@ -241,11 +241,14 @@ public class XSLFPictureShape extends XSLFSimpleShape
extBitmap = extLst.addNewExt();
extBitmap.setUri(BITMAP_URI);
XmlCursor cur = extBitmap.newCursor();
cur.toEndToken();
cur.beginElement(new QName(MS_DML_NS, "useLocalDpi", "a14"));
cur.insertNamespace("a14", MS_DML_NS);
cur.insertAttributeWithValue("val", "0");
cur.dispose();
try {
cur.toEndToken();
cur.beginElement(new QName(MS_DML_NS, "useLocalDpi", "a14"));
cur.insertNamespace("a14", MS_DML_NS);
cur.insertAttributeWithValue("val", "0");
} finally {
cur.dispose();
}
}
final int svgId = getExt(extLst, SVG_URI);
@ -261,11 +264,14 @@ public class XSLFPictureShape extends XSLFSimpleShape
CTOfficeArtExtension svgBitmap = extLst.addNewExt();
svgBitmap.setUri(SVG_URI);
XmlCursor cur = svgBitmap.newCursor();
cur.toEndToken();
cur.beginElement(new QName(MS_SVG_NS, "svgBlip", "asvg"));
cur.insertNamespace("asvg", MS_SVG_NS);
cur.insertAttributeWithValue(EMBED_TAG, svgRelId);
cur.dispose();
try {
cur.toEndToken();
cur.beginElement(new QName(MS_SVG_NS, "svgBlip", "asvg"));
cur.insertNamespace("asvg", MS_SVG_NS);
cur.insertAttributeWithValue(EMBED_TAG, svgRelId);
} finally {
cur.dispose();
}
}
@Override
@ -424,12 +430,15 @@ public class XSLFPictureShape extends XSLFSimpleShape
for(CTOfficeArtExtension ext : extLst.getExtArray()){
String xpath = "declare namespace a14='"+ MS_DML_NS +"' $this//a14:imgProps/a14:imgLayer";
XmlObject[] obj = ext.selectPath(xpath);
if(obj != null && obj.length == 1){
if(obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
String id = c.getAttributeText(EMBED_TAG);
String newId = getSheet().importBlip(id, p.getSheet());
c.setAttributeText(EMBED_TAG, newId);
c.dispose();
try {
String id = c.getAttributeText(EMBED_TAG);
String newId = getSheet().importBlip(id, p.getSheet());
c.setAttributeText(EMBED_TAG, newId);
} finally {
c.dispose();
}
}
}
}

View File

@ -1155,9 +1155,12 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperti
@Override
public boolean isLineStyle() {
XmlCursor cur = props.newCursor();
String name = cur.getName().getLocalPart();
cur.dispose();
return "lnRef".equals(name);
try {
String name = cur.getName().getLocalPart();
return "lnRef".equals(name);
} finally {
cur.dispose();
}
}
}

View File

@ -255,15 +255,18 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
*/
@SuppressWarnings({"unchecked", "WeakerAccess", "unused", "SameParameterValue"})
protected <T extends XmlObject> T getChild(Class<T> childClass, String namespace, String nodename) {
XmlCursor cur = getXmlObject().newCursor();
T child = null;
if (cur.toChild(namespace, nodename)) {
child = (T)cur.getObject();
XmlCursor cur = getXmlObject().newCursor();
try {
if (cur.toChild(namespace, nodename)) {
child = (T)cur.getObject();
}
if (cur.toChild(XSLFRelation.NS_DRAWINGML, nodename)) {
child = (T)cur.getObject();
}
} finally {
cur.dispose();
}
if (cur.toChild(XSLFRelation.NS_DRAWINGML, nodename)) {
child = (T)cur.getObject();
}
cur.dispose();
return child;
}
@ -457,12 +460,15 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
} else {
return null;
}
XmlCursor cur = styleLst.newCursor();
XSLFFillProperties fp = null;
if (cur.toChild(Math.toIntExact(childIdx))) {
fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject());
XmlCursor cur = styleLst.newCursor();
try {
if (cur.toChild(Math.toIntExact(childIdx))) {
fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject());
}
} finally {
cur.dispose();
}
cur.dispose();
CTSchemeColor phClr = fillRef.getSchemeClr();
PaintStyle res = selectPaint(fp, phClr, theme.getPackagePart(), theme, hasPlaceholder);

View File

@ -142,7 +142,7 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
List<XSLFShape> shapes = new ArrayList<>();
XmlCursor cur = spTree.newCursor();
try {
for (boolean b=cur.toFirstChild();b;b=cur.toNextSibling()) {
for (boolean b = cur.toFirstChild(); b; b = cur.toNextSibling()) {
XmlObject ch = cur.getObject();
if(ch instanceof CTShape){
// simple shape

View File

@ -261,17 +261,22 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
frame.addNewXfrm();
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor grCur = gr.newCursor();
grCur.toNextToken();
grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl"));
try {
grCur.toNextToken();
grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl"));
CTTable tbl = CTTable.Factory.newInstance();
tbl.addNewTblPr();
tbl.addNewTblGrid();
XmlCursor tblCur = tbl.newCursor();
tblCur.moveXmlContents(grCur);
tblCur.dispose();
grCur.dispose();
CTTable tbl = CTTable.Factory.newInstance();
tbl.addNewTblPr();
tbl.addNewTblGrid();
XmlCursor tblCur = tbl.newCursor();
try {
tblCur.moveXmlContents(grCur);
} finally {
tblCur.dispose();
}
} finally {
grCur.dispose();
}
gr.setUri(TABLE_URI);
return frame;
}

View File

@ -768,11 +768,17 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
}
XmlCursor thisC = thisP.newCursor();
thisC.toEndToken();
XmlCursor otherC = otherP.newCursor();
otherC.copyXmlContents(thisC);
otherC.dispose();
thisC.dispose();
try {
thisC.toEndToken();
XmlCursor otherC = otherP.newCursor();
try {
otherC.copyXmlContents(thisC);
} finally {
otherC.dispose();
}
} finally {
thisC.dispose();
}
for (XSLFTextRun tr : other.getTextRuns()) {
XmlObject xo = tr.getXmlObject();

View File

@ -461,27 +461,33 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
ole1.setShapeId(shapeId);
ole1.setId(olePR.getId());
XmlCursor cur1 = ole1.newCursor();
cur1.toEndToken();
cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
cur1.insertAttributeWithValue("defaultSize", "0");
cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
cur1.insertAttributeWithValue("moveWithCells", "1");
CTTwoCellAnchor ctAnchor;
final XmlCursor cur1 = ole1.newCursor();
try {
cur1.toEndToken();
cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
cur1.insertAttributeWithValue("defaultSize", "0");
cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
cur1.insertAttributeWithValue("moveWithCells", "1");
CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
XmlCursor cur2 = ctAnchor.newCursor();
cur2.copyXmlContents(cur1);
cur2.dispose();
final XmlCursor cur2 = ctAnchor.newCursor();
try {
cur2.copyXmlContents(cur1);
} finally {
cur2.dispose();
}
cur1.toParent();
cur1.toFirstChild();
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
cur1.toNextSibling();
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
cur1.dispose();
cur1.toParent();
cur1.toFirstChild();
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
cur1.toNextSibling();
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
} finally {
cur1.dispose();
}
// add a new shape and link OLE & image part
CTShape ctShape = ctAnchor.addNewSp();
@ -498,9 +504,12 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
cNvPr.setName("Object " + shapeId);
XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
extCur.toFirstChild();
extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
extCur.dispose();
try {
extCur.toFirstChild();
extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
} finally {
extCur.dispose();
}
XSSFObjectData shape = new XSSFObjectData(this, ctShape);
shape.anchor = (XSSFClientAnchor) anchor;
@ -682,8 +691,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
QName uriName = new QName(null, "uri");
String xquery = "declare namespace a='" + XSSFRelation.NS_DRAWINGML + "' .//a:extLst/a:ext";
XmlCursor cur = shape.newCursor();
cur.selectPath(xquery);
try {
cur.selectPath(xquery);
while (cur.toNextSelection()) {
String uri = cur.getAttributeText(uriName);
if ("{63B3BB69-23CF-44E3-9099-C40C66FF867C}".equals(uri)) {
@ -701,10 +710,13 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
XmlObject parentXbean = null;
XmlCursor cursor = obj.newCursor();
if (cursor.toParent()) {
parentXbean = cursor.getObject();
try {
if (cursor.toParent()) {
parentXbean = cursor.getObject();
}
} finally {
cursor.dispose();
}
cursor.dispose();
if (parentXbean != null) {
if (parentXbean instanceof CTTwoCellAnchor) {
CTTwoCellAnchor ct = (CTTwoCellAnchor) parentXbean;

View File

@ -246,16 +246,21 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
*/
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}

View File

@ -255,17 +255,22 @@ public class XWPFComment implements IBody {
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
cursor.dispose();
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
return null;
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}

View File

@ -1651,16 +1651,21 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
*/
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}

View File

@ -56,19 +56,22 @@ public class XWPFHeader extends XWPFHeaderFooter {
public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) {
super(doc, hdrFtr);
XmlCursor cursor = headerFooter.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
paragraphs.add(p);
}
if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
tables.add(t);
try {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
paragraphs.add(p);
}
if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
tables.add(t);
}
}
} finally {
cursor.dispose();
}
cursor.dispose();
}
/**

View File

@ -331,8 +331,11 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
if (paragraphs.contains(paragraph)) {
CTP ctP = paragraph.getCTP();
XmlCursor c = ctP.newCursor();
c.removeXml();
c.dispose();
try {
c.removeXml();
} finally {
c.dispose();
}
paragraphs.remove(paragraph);
bodyElements.remove(paragraph);
}
@ -347,8 +350,11 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
if (tables.contains(table)) {
CTTbl ctTbl = table.getCTTbl();
XmlCursor c = ctTbl.newCursor();
c.removeXml();
c.dispose();
try {
c.removeXml();
} finally {
c.dispose();
}
tables.remove(table);
bodyElements.remove(table);
}
@ -359,8 +365,11 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
*/
public void clearHeaderFooter() {
XmlCursor c = headerFooter.newCursor();
c.removeXmlContents();
c.dispose();
try {
c.removeXmlContents();
} finally {
c.dispose();
}
paragraphs.clear();
tables.clear();
bodyElements.clear();
@ -391,19 +400,25 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
paragraphs.add(pos, newP);
}
int i = 0;
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
final XmlCursor p2 = p.newCursor();
try {
cursor.toCursor(p2);
} finally {
p2.dispose();
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
p2 = p.newCursor();
cursor.toCursor(p2);
cursor.toEndToken();
p2.dispose();
final XmlCursor p3 = p.newCursor();
try {
cursor.toCursor(p3);
cursor.toEndToken();
} finally {
p3.dispose();
}
return newP;
}
return null;
@ -434,19 +449,25 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
tables.add(pos, newT);
}
int i = 0;
XmlCursor cursor2 = t.newCursor();
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
final XmlCursor cursor2 = t.newCursor();
try {
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
}
}
} finally {
cursor2.dispose();
}
cursor2.dispose();
bodyElements.add(i, newT);
cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken();
cursor2.dispose();
final XmlCursor cursor3 = t.newCursor();
try {
cursor.toCursor(cursor3);
cursor.toEndToken();
} finally {
cursor3.dispose();
}
return newT;
}
return null;
@ -457,10 +478,13 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
*/
private boolean isCursorInHdrF(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
boolean result = (verify.getObject() == this.headerFooter);
verify.dispose();
return result;
try {
verify.toParent();
boolean result = (verify.getObject() == this.headerFooter);
return result;
} finally {
verify.dispose();
}
}
@ -503,21 +527,24 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
// parse the document with cursor and add
// the XmlObject to its lists
XmlCursor cursor = headerFooter.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
paragraphs.add(p);
bodyElements.add(p);
}
if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
tables.add(t);
bodyElements.add(t);
try {
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);
}
}
} finally {
cursor.dispose();
}
cursor.dispose();
}
/**
@ -525,17 +552,21 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
*/
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
return null;
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}

View File

@ -73,33 +73,36 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
// Check for bits that only apply when attached to a core document
// TODO Make this nicer by tracking the XWPFFootnotes directly
XmlCursor c = r.newCursor();
c.selectPath("child::*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTFtnEdnRef) {
CTFtnEdnRef ftn = (CTFtnEdnRef) o;
footnoteText.append(" [").append(ftn.getId()).append(": ");
XWPFAbstractFootnoteEndnote footnote =
ftn.getDomNode().getLocalName().equals("footnoteReference") ?
document.getFootnoteByID(ftn.getId().intValue()) :
document.getEndnoteByID(ftn.getId().intValue());
if (null != footnote) {
boolean first = true;
for (XWPFParagraph p : footnote.getParagraphs()) {
if (!first) {
footnoteText.append("\n");
try {
c.selectPath("child::*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTFtnEdnRef) {
CTFtnEdnRef ftn = (CTFtnEdnRef) o;
footnoteText.append(" [").append(ftn.getId()).append(": ");
XWPFAbstractFootnoteEndnote footnote =
ftn.getDomNode().getLocalName().equals("footnoteReference") ?
document.getFootnoteByID(ftn.getId().intValue()) :
document.getEndnoteByID(ftn.getId().intValue());
if (null != footnote) {
boolean first = true;
for (XWPFParagraph p : footnote.getParagraphs()) {
if (!first) {
footnoteText.append("\n");
}
first = false;
footnoteText.append(p.getText());
}
first = false;
footnoteText.append(p.getText());
} else {
footnoteText.append("!!! End note with ID \"").append(ftn.getId()).append("\" not found in document.");
}
} else {
footnoteText.append("!!! End note with ID \"").append(ftn.getId()).append("\" not found in document.");
}
footnoteText.append("] ");
footnoteText.append("] ");
}
}
} finally {
c.dispose();
}
c.dispose();
}
}
@ -111,58 +114,61 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
@SuppressWarnings("deprecation")
private void buildRunsInOrderFromXml(XmlObject object) {
XmlCursor c = object.newCursor();
c.selectPath("child::*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTR) {
XWPFRun r = new XWPFRun((CTR) o, this);
runs.add(r);
iruns.add(r);
}
if (o instanceof CTHyperlink) {
CTHyperlink link = (CTHyperlink)o;
for (CTR r : link.getRArray()) {
XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
runs.add(hr);
iruns.add(hr);
}
}
if (o instanceof CTSimpleField) {
CTSimpleField field = (CTSimpleField)o;
for (CTR r : field.getRArray()) {
XWPFFieldRun fr = new XWPFFieldRun(field, r, this);
runs.add(fr);
iruns.add(fr);
}
}
if (o instanceof CTSdtBlock) {
XWPFSDT cc = new XWPFSDT((CTSdtBlock) o, part);
iruns.add(cc);
}
if (o instanceof CTSdtRun) {
XWPFSDT cc = new XWPFSDT((CTSdtRun) o, part);
iruns.add(cc);
}
if (o instanceof CTRunTrackChange) {
for (CTR r : ((CTRunTrackChange) o).getRArray()) {
XWPFRun cr = new XWPFRun(r, this);
runs.add(cr);
iruns.add(cr);
}
}
if (o instanceof CTSmartTagRun) {
// Smart Tags can be nested many times.
// This implementation does not preserve the tagging information
buildRunsInOrderFromXml(o);
}
if (o instanceof CTRunTrackChange) {
// add all the insertions as text
for (CTRunTrackChange change : ((CTRunTrackChange) o).getInsArray()) {
buildRunsInOrderFromXml(change);
try {
c.selectPath("child::*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTR) {
XWPFRun r = new XWPFRun((CTR) o, this);
runs.add(r);
iruns.add(r);
}
if (o instanceof CTHyperlink) {
CTHyperlink link = (CTHyperlink)o;
for (CTR r : link.getRArray()) {
XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
runs.add(hr);
iruns.add(hr);
}
}
if (o instanceof CTSimpleField) {
CTSimpleField field = (CTSimpleField)o;
for (CTR r : field.getRArray()) {
XWPFFieldRun fr = new XWPFFieldRun(field, r, this);
runs.add(fr);
iruns.add(fr);
}
}
if (o instanceof CTSdtBlock) {
XWPFSDT cc = new XWPFSDT((CTSdtBlock) o, part);
iruns.add(cc);
}
if (o instanceof CTSdtRun) {
XWPFSDT cc = new XWPFSDT((CTSdtRun) o, part);
iruns.add(cc);
}
if (o instanceof CTRunTrackChange) {
for (CTR r : ((CTRunTrackChange) o).getRArray()) {
XWPFRun cr = new XWPFRun(r, this);
runs.add(cr);
iruns.add(cr);
}
}
if (o instanceof CTSmartTagRun) {
// Smart Tags can be nested many times.
// This implementation does not preserve the tagging information
buildRunsInOrderFromXml(o);
}
if (o instanceof CTRunTrackChange) {
// add all the insertions as text
for (CTRunTrackChange change : ((CTRunTrackChange) o).getInsArray()) {
buildRunsInOrderFromXml(change);
}
}
}
} finally {
c.dispose();
}
c.dispose();
}
@Internal

View File

@ -87,29 +87,32 @@ public class XWPFTableCell implements IBody, ICell {
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);
}
if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
bodyElements.add(c);
}
if (o instanceof CTSdtRun) {
XWPFSDT c = new XWPFSDT((CTSdtRun) o, this);
bodyElements.add(c);
try {
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);
}
if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
bodyElements.add(c);
}
if (o instanceof CTSdtRun) {
XWPFSDT c = new XWPFSDT((CTSdtRun) o, this);
bodyElements.add(c);
}
}
} finally {
cursor.dispose();
}
cursor.dispose();
}
@Internal
@ -276,18 +279,24 @@ public class XWPFTableCell implements IBody, ICell {
paragraphs.add(pos, newP);
}
int i = 0;
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
final XmlCursor p2 = p.newCursor();
try {
cursor.toCursor(p2);
} finally {
p2.dispose();
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
final XmlCursor p3 = p.newCursor();
try {
cursor.toCursor(p3);
} finally {
p3.dispose();
}
cursor.toEndToken();
return newP;
}
@ -313,18 +322,24 @@ public class XWPFTableCell implements IBody, ICell {
tables.add(pos, newT);
}
int i = 0;
XmlCursor cursor2 = t.newCursor();
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
final XmlCursor cursor2 = t.newCursor();
try {
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
} finally {
cursor2.dispose();
}
cursor2.dispose();
bodyElements.add(i, newT);
cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken();
cursor2.dispose();
final XmlCursor cursor3 = t.newCursor();
try {
cursor.toCursor(cursor3);
cursor.toEndToken();
} finally {
cursor3.dispose();
}
return newT;
}
return null;
@ -335,10 +350,13 @@ public class XWPFTableCell implements IBody, ICell {
*/
private boolean isCursorInTableCell(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
boolean result = (verify.getObject() == this.ctTc);
verify.dispose();
return result;
try {
verify.toParent();
boolean result = (verify.getObject() == this.ctTc);
return result;
} finally {
verify.dispose();
}
}
@Override
@ -475,16 +493,21 @@ public class XWPFTableCell implements IBody, ICell {
*/
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}

View File

@ -181,16 +181,19 @@ public class XWPFTableRow {
//Can't use ctRow.getTcList because that only gets table cells
//Can't use ctRow.getSdtList because that only gets sdts that are at cell level
XmlCursor cursor = ctRow.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTTc) {
cells.add(new XWPFTableCell((CTTc) o, this, table.getBody()));
} else if (o instanceof CTSdtCell) {
cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody()));
try {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTTc) {
cells.add(new XWPFTableCell((CTTc) o, this, table.getBody()));
} else if (o instanceof CTSdtCell) {
cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody()));
}
}
} finally {
cursor.dispose();
}
cursor.dispose();
return cells;
}