]> source.dussan.org Git - poi.git/commitdiff
close cursors in finally blocks
authorPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 10:24:04 +0000 (10:24 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 10:24:04 +0000 (10:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896470 13f79535-47bb-0310-9956-ffa450edef68

18 files changed:
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFChart.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFObjectShape.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPropertiesDelegate.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFShape.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTextParagraph.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java

index c54cba50c8ceb41a4f26ba99dfbc0293e74a0276..88921d7f690ff637c52c1bf85340765f4df121bf 100644 (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;
index 1e5b4355bbdeb772c43bed2e0b875d0e7555239e..88295dcbdcd290e0b227c5c60cd8d8fbbc47c47a 100644 (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();
         }
     }
 
index 9b5b9d63dd8881d4dbbe3affba7fe3a3eba32084..e858c02c9b3b4d728acdc6cba94a176fefc1e230 100644 (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"));
-
-
-        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);
-
-        spPr.addNewPrstGeom().setPrst(STShapeType.RECT);
-
-
-        XmlCursor picCur = grpShp.newCursor();
-        picCur.toStartDoc();
-        picCur.moveXmlContents(grCur);
-        picCur.dispose();
-
-        grCur.dispose();
-
+        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();
+
+
+            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();
+            try {
+                picCur.toStartDoc();
+                picCur.moveXmlContents(grCur);
+            } finally {
+                picCur.dispose();
+            }
+        } finally {
+            grCur.dispose();
+        }
 
         return frame;
     }
index 5c839f40feb4a95e195c6d943662cbcaa64506cf..3029942a537f352c64315ce668ebea432ced79a8 100644 (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();
+                    }
                 }
             }
         }
index bc1aee7850a709a704786baba598a0928f7dbd4c..83d15bbcecf5c2b8f20627ae7827334a5af7be1d 100644 (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();
+            }
         }
     }
 
index 0fccb9e8ad79a2ead89236dc095fc186c37d20a0..3965186b8979c3f0f1b797ba15f748dedcce44eb 100644 (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();
-        }
-        if (cur.toChild(XSLFRelation.NS_DRAWINGML, 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();
         }
-        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);
index 6812e86c55c5bdc08833b5dc14c440d8a49594d4..20570c789b6c3437359b526302a35c332c49fd3a 100644 (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
index 2974b46b59701376b7607c3f2604696e99096c3d..b6f2b3350af08e2cf88fa30f1a8dab9eb0a76f6d 100644 (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"));
-
-        CTTable tbl = CTTable.Factory.newInstance();
-        tbl.addNewTblPr();
-        tbl.addNewTblGrid();
-        XmlCursor tblCur = tbl.newCursor();
-
-        tblCur.moveXmlContents(grCur);
-        tblCur.dispose();
-        grCur.dispose();
+        try {
+            grCur.toNextToken();
+            grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl"));
+
+            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;
     }
index ca9f2f64bbe4047db81a5ae6ada474ad4c8cfbaa..77f05767a232080af3580bf9df9a8052d42620f1 100644 (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();
index 6bf672db6de42f40b8a7f36ebc26cce9c0f991ec..e93dd7d788ceb470acd4342a2e75d26808757c27 100644 (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 = createTwoCellAnchor((XSSFClientAnchor) anchor);
-
-        XmlCursor cur2 = ctAnchor.newCursor();
-        cur2.copyXmlContents(cur1);
-        cur2.dispose();
-
-        cur1.toParent();
-        cur1.toFirstChild();
-        cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
-        cur1.toNextSibling();
-        cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
+        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");
+
+            ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
+
+            final XmlCursor cur2 = ctAnchor.newCursor();
+            try {
+                cur2.copyXmlContents(cur1);
+            } finally {
+                cur2.dispose();
+            }
 
-        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;
index 8c1cc90784f04d5e9d91271622861c1bc68e0dab..e1e0df9bb3f5dde75dfebc50284467535067bbf9 100644 (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;
         }
index f9ee63fc44a467f50318ef819d65814bbb283df6..a37ff23ca73a5af578c98c9660cb28b9c3d9d7a8 100644 (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;
         }
index 9e735bacdb131430c85f6916da858f7c53ba3cce..f65667c55de7579d40858a2a200f05e53fc8f802 100644 (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;
         }
index eaa466adab1ef00713b7c9dad7f1fae93d1ffa96..d3ef04e5e14489f1379606fd74671e95ea4dae77 100644 (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();
     }
 
     /**
index ac3b8200e0d1c8dc3f031f503b0db3c939e72183..fea3e33b48028ef90ed77304d7973a4cb77b9b0a 100644 (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;
         }
index cd0ee99b64d097950dd0b2653730b7b91f5f0f93..c3185b4d3ee6c6f6586a4626b81435e555baa068 100644 (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);
+        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 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 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 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 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 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);
+                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
index 0f014c45da7065101c0e3fc566210acca2d517d8..b81cde1eee99067237f0dda1be9135a605161904 100644 (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;
         }
index da96c8320b05048ce84b0fb6022dffedb3687593..18783f64c470c9fb6d4a437c6abc360fd1aab0f3 100644 (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;
     }