From: PJ Fanning Date: Sun, 19 Jun 2022 11:02:01 +0000 (+0000) Subject: remove use of some calls to deprecated cursor dispose() X-Git-Tag: REL_5_2_3~223 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b7261c16210c12239ffbec9e87828ea1dac4388f;p=poi.git remove use of some calls to deprecated cursor dispose() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902057 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java index c374c12798..e5595f6883 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java @@ -109,9 +109,8 @@ public final class XPathHelper { public static T selectProperty(XmlObject startObject, Class resultClass, XSLFShape.ReparseFactory factory, QName[]... path) throws XmlException { XmlObject xo = startObject; - XmlCursor cur = xo.newCursor(); XmlCursor innerCur = null; - try { + try (XmlCursor cur = startObject.newCursor()) { innerCur = selectProperty(cur, path, 0, factory != null, false); if (innerCur == null) { return null; @@ -133,9 +132,8 @@ public final class XPathHelper { return (T)xo; } finally { - cur.dispose(); if (innerCur != null) { - innerCur.dispose(); + innerCur.close(); } } } @@ -187,22 +185,17 @@ public final class XPathHelper { for (int i=0; i { @SuppressWarnings({"unchecked", "WeakerAccess", "unused", "SameParameterValue"}) protected T getChild(Class childClass, String namespace, String nodename) { T child = null; - XmlCursor cur = getXmlObject().newCursor(); - try { + try (XmlCursor cur = getXmlObject().newCursor()) { if (cur.toChild(namespace, nodename)) { child = (T)cur.getObject(); } if (cur.toChild(XSLFRelation.NS_DRAWINGML, nodename)) { child = (T)cur.getObject(); } - } finally { - cur.dispose(); } return child; } @@ -461,13 +458,10 @@ public abstract class XSLFShape implements Shape { return null; } XSLFFillProperties fp = null; - XmlCursor cur = styleLst.newCursor(); - try { + try (XmlCursor cur = styleLst.newCursor()) { if (cur.toChild(Math.toIntExact(childIdx))) { fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject()); } - } finally { - cur.dispose(); } CTSchemeColor phClr = fillRef.getSchemeClr(); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java index 20570c789b..b56dc79062 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java @@ -140,8 +140,7 @@ implements XSLFShapeContainer, Sheet { final XSLFSheet sheet = (parent instanceof XSLFSheet) ? (XSLFSheet)parent : ((XSLFShape)parent).getSheet(); List shapes = new ArrayList<>(); - XmlCursor cur = spTree.newCursor(); - try { + try (XmlCursor cur = spTree.newCursor()) { for (boolean b = cur.toFirstChild(); b; b = cur.toNextSibling()) { XmlObject ch = cur.getObject(); if(ch instanceof CTShape){ @@ -174,8 +173,6 @@ implements XSLFShapeContainer, Sheet { cur.pop(); } } - } finally { - cur.dispose(); } for (final XSLFShape s : shapes) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java index b6f2b3350a..863a7527d4 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java @@ -63,8 +63,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable(_table.sizeOfTrArray()); @@ -260,22 +257,16 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable(); _shape = shape; - XmlCursor c = _p.newCursor(); - try { + try (XmlCursor c = _p.newCursor()) { if (c.toFirstChild()) { do { XmlObject r = c.getObject(); @@ -78,8 +77,6 @@ public class XSLFTextParagraph implements TextParagraph getShapes() { List lst = new ArrayList<>(); - XmlCursor cur = drawing.newCursor(); - try { + try (XmlCursor cur = drawing.newCursor()) { if (cur.toFirstChild()) { addShapes(cur, lst); } - } finally { - cur.dispose(); } return lst; } @@ -611,87 +599,76 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing getShapes(XSSFShapeGroup groupshape) { List lst = new ArrayList<>(); - XmlCursor cur = groupshape.getCTGroupShape().newCursor(); - try { + try (XmlCursor cur = groupshape.getCTGroupShape().newCursor()) { addShapes(cur, lst); - } finally { - cur.dispose(); } return lst; } private void addShapes(XmlCursor cur, List lst) { - try { - do { - cur.push(); - if (cur.toFirstChild()) { - do { - XmlObject obj = cur.getObject(); - - XSSFShape shape; - if (obj instanceof CTMarker) { - // ignore anchor elements - continue; - } else if (obj instanceof CTPicture) { - shape = new XSSFPicture(this, (CTPicture) obj); - } else if (obj instanceof CTConnector) { - shape = new XSSFConnector(this, (CTConnector) obj); - } else if (obj instanceof CTShape) { - shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj) - : new XSSFSimpleShape(this, (CTShape) obj); - } else if (obj instanceof CTGraphicalObjectFrame) { - shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj); - } else if (obj instanceof CTGroupShape) { - shape = new XSSFShapeGroup(this, (CTGroupShape) obj); - } else if (obj instanceof XmlAnyTypeImpl) { - LOG.atWarn().log("trying to parse AlternateContent, this unlinks the returned Shapes from the underlying xml content, so those shapes can't be used to modify the drawing, i.e. modifications will be ignored!"); - - // XmlAnyTypeImpl is returned for AlternateContent - // parts, which might contain a CTDrawing - cur.push(); - cur.toFirstChild(); - XmlCursor cur2 = null; - try { - // need to parse AlternateContent again, - // otherwise the child elements aren't typed, - // but also XmlAnyTypes - CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader()); - cur2 = alterWS.newCursor(); + do { + cur.push(); + if (cur.toFirstChild()) { + do { + XmlObject obj = cur.getObject(); + + XSSFShape shape; + if (obj instanceof CTMarker) { + // ignore anchor elements + continue; + } else if (obj instanceof CTPicture) { + shape = new XSSFPicture(this, (CTPicture) obj); + } else if (obj instanceof CTConnector) { + shape = new XSSFConnector(this, (CTConnector) obj); + } else if (obj instanceof CTShape) { + shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj) + : new XSSFSimpleShape(this, (CTShape) obj); + } else if (obj instanceof CTGraphicalObjectFrame) { + shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj); + } else if (obj instanceof CTGroupShape) { + shape = new XSSFShapeGroup(this, (CTGroupShape) obj); + } else if (obj instanceof XmlAnyTypeImpl) { + LOG.atWarn().log("trying to parse AlternateContent, this unlinks the returned Shapes from the underlying xml content, so those shapes can't be used to modify the drawing, i.e. modifications will be ignored!"); + + // XmlAnyTypeImpl is returned for AlternateContent + // parts, which might contain a CTDrawing + cur.push(); + cur.toFirstChild(); + try { + // need to parse AlternateContent again, + // otherwise the child elements aren't typed, + // but also XmlAnyTypes + CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader()); + try (XmlCursor cur2 = alterWS.newCursor()) { if (cur2.toFirstChild()) { addShapes(cur2, lst); } - } catch (XmlException e) { - LOG.atWarn().withThrowable(e).log("unable to parse CTDrawing in alternate content."); - } finally { - if (cur2 != null) { - cur2.dispose(); - } - cur.pop(); } - continue; - } else { - // ignore anything else - continue; + } catch (XmlException e) { + LOG.atWarn().withThrowable(e).log("unable to parse CTDrawing in alternate content."); + } finally { + cur.pop(); } + continue; + } else { + // ignore anything else + continue; + } - assert (shape != null); - shape.anchor = getAnchorFromParent(obj); - lst.add(shape); + assert (shape != null); + shape.anchor = getAnchorFromParent(obj); + lst.add(shape); - } while (cur.toNextSibling()); - } - cur.pop(); - } while (cur.toNextSibling()); - } finally { - cur.dispose(); - } + } while (cur.toNextSibling()); + } + cur.pop(); + } while (cur.toNextSibling()); } private boolean hasOleLink(XmlObject shape) { QName uriName = new QName(null, "uri"); String xquery = "declare namespace a='" + XSSFRelation.NS_DRAWINGML + "' .//a:extLst/a:ext"; - XmlCursor cur = shape.newCursor(); - try { + try (XmlCursor cur = shape.newCursor()) { cur.selectPath(xquery); while (cur.toNextSelection()) { String uri = cur.getAttributeText(uriName); @@ -699,8 +676,6 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing dxfList = new ArrayList<>(); // CT* classes don't handle "mc:AlternateContent" elements, so get the Dxf instances manually - final XmlCursor cur = dxfs.newCursor(); - try { + try (XmlCursor cur = dxfs.newCursor()) { // sometimes there are namespaces sometimes not. String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf | .//dxf"; cur.selectPath(xquery); @@ -82,8 +81,6 @@ public class XSSFTableStyle implements TableStyle { } } } - } finally { - cur.dispose(); } for (CTTableStyleElement element : tableStyle.getTableStyleElementList()) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java index 2ed99ad900..9f7fe0801c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java @@ -150,9 +150,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { " xmlns=\""+NS_SPREADSHEETML+"\"", "") , xopt); - XmlCursor cur = root.getXml().newCursor(); - - try { + try (XmlCursor cur = root.getXml().newCursor()) { for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { XmlObject xo = cur.getObject(); if (xo instanceof CTShapetype) { @@ -168,21 +166,16 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { } } } - } finally { - cur.dispose(); } } protected List getItems(){ List items = new ArrayList<>(); - XmlCursor cur = root.getXml().newCursor(); - try { + try (XmlCursor cur = root.getXml().newCursor()) { for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { items.add(cur.getObject()); } - } finally { - cur.dispose(); } return items; @@ -207,8 +200,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { */ private void newDrawing(){ root = XmlDocument.Factory.newInstance(); - final XmlCursor xml = root.addNewXml().newCursor(); - try { + try (final XmlCursor xml = root.addNewXml().newCursor()) { ShapelayoutDocument layDoc = ShapelayoutDocument.Factory.newInstance(); CTShapeLayout layout = layDoc.addNewShapelayout(); layout.setExt(STExt.EDIT); @@ -217,11 +209,8 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { idmap.setData("1"); xml.toEndToken(); - final XmlCursor layCur = layDoc.newCursor(); - try { + try (XmlCursor layCur = layDoc.newCursor()) { layCur.copyXmlContents(xml); - } finally { - layCur.dispose(); } CTGroup grp = CTGroup.Factory.newInstance(); @@ -237,16 +226,10 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { path.setConnecttype(STConnectType.RECT); xml.toEndToken(); - final XmlCursor grpCur = grp.newCursor(); - try { + try (XmlCursor grpCur = grp.newCursor()) { grpCur.copyXmlContents(xml); - } finally { - grpCur.dispose(); } - } finally { - xml.dispose(); } - } /** @@ -278,19 +261,13 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { cldata.addNewRow().setBigIntegerValue(BigInteger.valueOf(0)); cldata.addNewColumn().setBigIntegerValue(BigInteger.valueOf(0)); - XmlCursor xml = root.getXml().newCursor(); - try { + try (final XmlCursor xml = root.getXml().newCursor()){ xml.toEndToken(); - XmlCursor grpCur = grp.newCursor(); - try { + try (final XmlCursor grpCur = grp.newCursor()){ grpCur.copyXmlContents(xml); xml.toPrevSibling(); shape = (CTShape)xml.getObject(); - } finally { - grpCur.dispose(); } - } finally { - xml.dispose(); } return shape; @@ -302,16 +279,13 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { * @return the comment shape or null */ public CTShape findCommentShape(int row, int col){ - XmlCursor cur = root.getXml().newCursor(); - try { + try (final XmlCursor cur = root.getXml().newCursor()){ for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { XmlObject itm = cur.getObject(); if (matchCommentShape(itm, row, col)) { return (CTShape)itm; } } - } finally { - cur.dispose(); } return null; } @@ -337,8 +311,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { } protected boolean removeCommentShape(int row, int col){ - XmlCursor cur = root.getXml().newCursor(); - try { + try (final XmlCursor cur = root.getXml().newCursor()) { for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { XmlObject itm = cur.getObject(); if (matchCommentShape(itm, row, col)) { @@ -346,8 +319,6 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { return true; } } - } finally { - cur.dispose(); } return false; } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java index fafac08ee9..8c3329e797 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java @@ -49,8 +49,7 @@ public final class XSSFPasswordHelper { * @param prefix the prefix of the password attributes, may be null */ public static void setPassword(XmlObject xobj, String password, HashAlgorithm hashAlgo, String prefix) { - XmlCursor cur = xobj.newCursor(); - try { + try (final XmlCursor cur = xobj.newCursor()) { if (password == null) { cur.removeAttribute(getAttrName(prefix, "password")); cur.removeAttribute(getAttrName(prefix, "algorithmName")); @@ -84,8 +83,6 @@ public final class XSSFPasswordHelper { cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), enc64.encodeToString(salt)); cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount); } - } finally { - cur.dispose(); } } @@ -103,9 +100,8 @@ public final class XSSFPasswordHelper { public static boolean validatePassword(XmlObject xobj, String password, String prefix) { // TODO: is "velvetSweatshop" the default password? if (password == null) return false; - - XmlCursor cur = xobj.newCursor(); - try { + + try (final XmlCursor cur = xobj.newCursor()) { String xorHashVal = cur.getAttributeText(getAttrName(prefix, "password")); String algoName = cur.getAttributeText(getAttrName(prefix, "algorithmName")); String hashVal = cur.getAttributeText(getAttrName(prefix, "hashValue")); @@ -129,8 +125,6 @@ public final class XSSFPasswordHelper { byte[] hash2 = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false); return Arrays.equals(hash1, hash2); } - } finally { - cur.dispose(); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java index a37ff23ca7..4393b93311 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java @@ -46,25 +46,25 @@ public class XWPFComment implements IBody { } protected void init() { - XmlCursor cursor = ctComment.newCursor(); - cursor.selectPath("./*"); - while (cursor.toNextSelection()) { - XmlObject o = cursor.getObject(); - if (o instanceof CTP) { - XWPFParagraph p = new XWPFParagraph((CTP) o, this); - bodyElements.add(p); - paragraphs.add(p); - } else if (o instanceof CTTbl) { - XWPFTable t = new XWPFTable((CTTbl) o, this); - bodyElements.add(t); - tables.add(t); - } else if (o instanceof CTSdtBlock) { - XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this); - bodyElements.add(c); - } + try (XmlCursor cursor = ctComment.newCursor()) { + cursor.selectPath("./*"); + while (cursor.toNextSelection()) { + XmlObject o = cursor.getObject(); + if (o instanceof CTP) { + XWPFParagraph p = new XWPFParagraph((CTP) o, this); + bodyElements.add(p); + paragraphs.add(p); + } else if (o instanceof CTTbl) { + XWPFTable t = new XWPFTable((CTTbl) o, this); + bodyElements.add(t); + tables.add(t); + } else if (o instanceof CTSdtBlock) { + XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this); + bodyElements.add(c); + } + } } - cursor.dispose(); } /** @@ -174,30 +174,29 @@ public class XWPFComment implements IBody { paragraphs.add(pos, newP); } int i = 0; - XmlCursor p2 = p.newCursor(); - cursor.toCursor(p2); - p2.dispose(); + try (XmlCursor p2 = p.newCursor()) { + cursor.toCursor(p2); + } 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(); + try (XmlCursor p2 = p.newCursor()) { + cursor.toCursor(p2); + cursor.toEndToken(); + } return newP; } return null; } private boolean isCursorInCmt(XmlCursor cursor) { - XmlCursor verify = cursor.newCursor(); - verify.toParent(); - boolean result = (verify.getObject() == this.ctComment); - verify.dispose(); - return result; + try (XmlCursor verify = cursor.newCursor()) { + verify.toParent(); + return (verify.getObject() == this.ctComment); + } } @Override @@ -221,19 +220,19 @@ public class XWPFComment implements IBody { 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++; + try (XmlCursor cursor2 = t.newCursor()) { + while (cursor2.toPrevSibling()) { + o = cursor2.getObject(); + if (o instanceof CTP || o instanceof CTTbl) { + i++; + } } } - cursor2.dispose(); bodyElements.add(i, newT); - cursor2 = t.newCursor(); - cursor.toCursor(cursor2); - cursor.toEndToken(); - cursor2.dispose(); + try (XmlCursor cursor2 = t.newCursor()) { + cursor.toCursor(cursor2); + cursor.toEndToken(); + } return newT; } return null; @@ -257,19 +256,15 @@ public class XWPFComment implements IBody { public XWPFTableCell getTableCell(CTTc cell) { XmlObject o; CTRow row; - final XmlCursor cursor = cell.newCursor(); - try { + try (final XmlCursor cursor = cell.newCursor()) { cursor.toParent(); o = cursor.getObject(); if (!(o instanceof CTRow)) { - cursor.dispose(); return null; } row = (CTRow) o; cursor.toParent(); o = cursor.getObject(); - } finally { - cursor.dispose(); } if (!(o instanceof CTTbl)) { return null; @@ -314,9 +309,9 @@ public class XWPFComment implements IBody { public void removeParagraph(XWPFParagraph paragraph) { if (paragraphs.contains(paragraph)) { CTP ctP = paragraph.getCTP(); - XmlCursor c = ctP.newCursor(); - c.removeXml(); - c.dispose(); + try (XmlCursor c = ctP.newCursor()) { + c.removeXml(); + } paragraphs.remove(paragraph); bodyElements.remove(paragraph); } @@ -325,17 +320,16 @@ public class XWPFComment implements IBody { public void removeTable(XWPFTable table) { if (tables.contains(table)) { CTTbl ctTbl = table.getCTTbl(); - XmlCursor c = ctTbl.newCursor(); - c.removeXml(); - c.dispose(); + try (XmlCursor c = ctTbl.newCursor()) { + c.removeXml(); + } tables.remove(table); bodyElements.remove(table); } } public XWPFTable createTable(int rows, int cols) { - XWPFTable table = new XWPFTable(ctComment.addNewTbl(), this, rows, - cols); + XWPFTable table = new XWPFTable(ctComment.addNewTbl(), this, rows, cols); tables.add(table); bodyElements.add(table); return table; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java index 0bbdf47766..5ea795fe66 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java @@ -554,8 +554,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo public XWPFTableCell getTableCell(CTTc cell) { XmlObject o; CTRow row; - final XmlCursor cursor = cell.newCursor(); - try { + try (XmlCursor cursor = cell.newCursor()) { cursor.toParent(); o = cursor.getObject(); if (!(o instanceof CTRow)) { @@ -564,8 +563,6 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo row = (CTRow) o; cursor.toParent(); o = cursor.getObject(); - } finally { - cursor.dispose(); } if (!(o instanceof CTTbl)) { return null; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index ca4c95f76d..2077b857ff 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -113,8 +113,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ @SuppressWarnings("deprecation") private void buildRunsInOrderFromXml(XmlObject object) { - XmlCursor c = object.newCursor(); - try { + try (XmlCursor c = object.newCursor()) { c.selectPath("child::*"); while (c.toNextSelection()) { XmlObject o = c.getObject(); @@ -166,8 +165,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para } } } - } finally { - c.dispose(); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java index 74ab5de4ac..da4fc7de35 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java @@ -43,48 +43,48 @@ public class XWPFSDTContent implements ISDTContent { if (sdtRun == null) { return; } - XmlCursor cursor = sdtRun.newCursor(); - cursor.selectPath("./*"); - while (cursor.toNextSelection()) { - XmlObject o = cursor.getObject(); - if (o instanceof CTR) { - XWPFRun run = new XWPFRun((CTR) o, parent); - bodyElements.add(run); - } else if (o instanceof CTSdtRun) { - XWPFSDT c = new XWPFSDT(((CTSdtRun) o), part); - bodyElements.add(c); + try (final XmlCursor cursor = sdtRun.newCursor()) { + cursor.selectPath("./*"); + while (cursor.toNextSelection()) { + XmlObject o = cursor.getObject(); + if (o instanceof CTR) { + XWPFRun run = new XWPFRun((CTR) o, parent); + bodyElements.add(run); + } else if (o instanceof CTSdtRun) { + XWPFSDT c = new XWPFSDT(((CTSdtRun) o), part); + bodyElements.add(c); + } } } - cursor.dispose(); } public XWPFSDTContent(CTSdtContentBlock block, IBody part, IRunBody parent) { if (block == null) { return; } - XmlCursor cursor = block.newCursor(); - cursor.selectPath("./*"); - while (cursor.toNextSelection()) { - XmlObject o = cursor.getObject(); - if (o instanceof CTP) { - XWPFParagraph p = new XWPFParagraph((CTP) o, part); - bodyElements.add(p); - // paragraphs.add(p); - } else if (o instanceof CTTbl) { - XWPFTable t = new XWPFTable((CTTbl) o, part); - bodyElements.add(t); - // tables.add(t); - } else if (o instanceof CTSdtBlock) { - XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part); - bodyElements.add(c); - // contentControls.add(c); - } else if (o instanceof CTR) { - XWPFRun run = new XWPFRun((CTR) o, parent); - // runs.add(run); - bodyElements.add(run); + try (final XmlCursor cursor = block.newCursor()) { + cursor.selectPath("./*"); + while (cursor.toNextSelection()) { + XmlObject o = cursor.getObject(); + if (o instanceof CTP) { + XWPFParagraph p = new XWPFParagraph((CTP) o, part); + bodyElements.add(p); + // paragraphs.add(p); + } else if (o instanceof CTTbl) { + XWPFTable t = new XWPFTable((CTTbl) o, part); + bodyElements.add(t); + // tables.add(t); + } else if (o instanceof CTSdtBlock) { + XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part); + bodyElements.add(c); + // contentControls.add(c); + } else if (o instanceof CTR) { + XWPFRun run = new XWPFRun((CTR) o, parent); + // runs.add(run); + bodyElements.add(run); + } } } - cursor.dispose(); } @Override diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java index 8432b39d28..d43e24fc43 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java @@ -50,49 +50,47 @@ public class XWPFSDTContentCell implements ISDTContent { return; } StringBuilder sb = new StringBuilder(); - XmlCursor cursor = sdtContentCell.newCursor(); - - //keep track of the following, - //and add "\n" only before the start of a body - //element if it is not the first body element. - - //index of cell in row - int tcCnt = 0; - //count of body objects - int iBodyCnt = 0; - int depth = 1; - - while (cursor.hasNextToken() && depth > 0) { - TokenType t = cursor.toNextToken(); - if (t.isText()) { - sb.append(cursor.getTextValue()); - } else if (isStartToken(cursor, "tr")) { - tcCnt = 0; - iBodyCnt = 0; - } else if (isStartToken(cursor, "tc")) { - if (tcCnt++ > 0) { - sb.append("\t"); + try (final XmlCursor cursor = sdtContentCell.newCursor()) { + //keep track of the following, + //and add "\n" only before the start of a body + //element if it is not the first body element. + + //index of cell in row + int tcCnt = 0; + //count of body objects + int iBodyCnt = 0; + int depth = 1; + + while (cursor.hasNextToken() && depth > 0) { + TokenType t = cursor.toNextToken(); + if (t.isText()) { + sb.append(cursor.getTextValue()); + } else if (isStartToken(cursor, "tr")) { + tcCnt = 0; + iBodyCnt = 0; + } else if (isStartToken(cursor, "tc")) { + if (tcCnt++ > 0) { + sb.append("\t"); + } + iBodyCnt = 0; + } else if (isStartToken(cursor, "p") || + isStartToken(cursor, "tbl") || + isStartToken(cursor, "sdt")) { + if (iBodyCnt > 0) { + sb.append("\n"); + } + iBodyCnt++; } - iBodyCnt = 0; - } else if (isStartToken(cursor, "p") || - isStartToken(cursor, "tbl") || - isStartToken(cursor, "sdt")) { - if (iBodyCnt > 0) { - sb.append("\n"); + if (cursor.isStart()) { + depth++; + } else if (cursor.isEnd()) { + depth--; } - iBodyCnt++; - } - if (cursor.isStart()) { - depth++; - } else if (cursor.isEnd()) { - depth--; } + text = sb.toString(); } - text = sb.toString(); - cursor.dispose(); } - private boolean isStartToken(XmlCursor cursor, String string) { if (!cursor.isStart()) { return false; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index fe3140613a..efe489ef9c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -86,8 +86,7 @@ public class XWPFTableCell implements IBody, ICell { paragraphs = new ArrayList<>(); tables = new ArrayList<>(); - XmlCursor cursor = ctTc.newCursor(); - try { + try (XmlCursor cursor = ctTc.newCursor()) { cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); @@ -110,8 +109,6 @@ public class XWPFTableCell implements IBody, ICell { bodyElements.add(c); } } - } finally { - cursor.dispose(); } } @@ -279,11 +276,8 @@ public class XWPFTableCell implements IBody, ICell { paragraphs.add(pos, newP); } int i = 0; - final XmlCursor p2 = p.newCursor(); - try { + try (XmlCursor p2 = p.newCursor()) { cursor.toCursor(p2); - } finally { - p2.dispose(); } while (cursor.toPrevSibling()) { o = cursor.getObject(); @@ -291,11 +285,8 @@ public class XWPFTableCell implements IBody, ICell { i++; } bodyElements.add(i, newP); - final XmlCursor p3 = p.newCursor(); - try { + try (XmlCursor p3 = p.newCursor()) { cursor.toCursor(p3); - } finally { - p3.dispose(); } cursor.toEndToken(); return newP; @@ -322,23 +313,17 @@ public class XWPFTableCell implements IBody, ICell { tables.add(pos, newT); } int i = 0; - final XmlCursor cursor2 = t.newCursor(); - try { + try (XmlCursor cursor2 = t.newCursor()) { while (cursor2.toPrevSibling()) { o = cursor2.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } - } finally { - cursor2.dispose(); } bodyElements.add(i, newT); - final XmlCursor cursor3 = t.newCursor(); - try { + try (XmlCursor cursor3 = t.newCursor()) { cursor.toCursor(cursor3); cursor.toEndToken(); - } finally { - cursor3.dispose(); } return newT; } @@ -349,13 +334,10 @@ public class XWPFTableCell implements IBody, ICell { * verifies that cursor is on the right position */ private boolean isCursorInTableCell(XmlCursor cursor) { - XmlCursor verify = cursor.newCursor(); - try { + try (XmlCursor verify = cursor.newCursor()) { verify.toParent(); boolean result = (verify.getObject() == this.ctTc); return result; - } finally { - verify.dispose(); } } @@ -495,8 +477,7 @@ public class XWPFTableCell implements IBody, ICell { public XWPFTableCell getTableCell(CTTc cell) { XmlObject o; CTRow row; - final XmlCursor cursor = cell.newCursor(); - try { + try (final XmlCursor cursor = cell.newCursor()) { cursor.toParent(); o = cursor.getObject(); if (!(o instanceof CTRow)) { @@ -505,8 +486,6 @@ public class XWPFTableCell implements IBody, ICell { row = (CTRow) o; cursor.toParent(); o = cursor.getObject(); - } finally { - cursor.dispose(); } if (!(o instanceof CTTbl)) { return null; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index 18783f64c4..f27df6dc24 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -180,8 +180,7 @@ public class XWPFTableRow { List cells = new ArrayList<>(); //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(); - try { + try (XmlCursor cursor = ctRow.newCursor()) { cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); @@ -191,8 +190,6 @@ public class XWPFTableRow { cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody())); } } - } finally { - cursor.dispose(); } return cells; } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFExamples.java b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFExamples.java index 78ae286f7e..3e82f28f0e 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFExamples.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFExamples.java @@ -84,13 +84,12 @@ class TestXSLFExamples { ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}"); String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main"; - XmlCursor cur = ext.newCursor(); - cur.toEndToken(); - cur.beginElement(new QName(p14Ns, "media", "p14")); - cur.insertNamespace("p14", p14Ns); - cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId()); - cur.dispose(); - + try (XmlCursor cur = ext.newCursor()) { + cur.toEndToken(); + cur.beginElement(new QName(p14Ns, "media", "p14")); + cur.insertNamespace("p14", p14Ns); + cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId()); + } CTSlide xslide = slide1.getXmlObject(); CTTimeNodeList ctnl;