From adc96cdca9336b84c0f7127724355a2363616eeb Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 16 May 2016 09:54:01 +0000 Subject: [PATCH] Dispose XmlCursor Replacing a method argument (XmlCursor) with a local instance and then updating it, doesn't make sense git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744002 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFDocument.java | 8 +- .../poi/xwpf/usermodel/XWPFFootnote.java | 14 ++- .../poi/xwpf/usermodel/XWPFHeaderFooter.java | 32 ++++--- .../poi/xwpf/usermodel/XWPFParagraph.java | 87 +++++++++---------- .../poi/xwpf/usermodel/XWPFSDTContent.java | 3 +- .../xwpf/usermodel/XWPFSDTContentCell.java | 3 +- .../poi/xwpf/usermodel/XWPFTableCell.java | 30 ++++--- .../poi/xwpf/usermodel/XWPFTableRow.java | 3 +- 8 files changed, 100 insertions(+), 80 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 74de72db33..23a63d0e80 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -667,11 +667,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { private boolean isCursorInBody(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); - try { - return (verify.getObject() == this.ctDocument.getBody()); - } finally { - verify.dispose(); - } + boolean result = (verify.getObject() == this.ctDocument.getBody()); + verify.dispose(); + return result; } private int getPosOfBodyElement(IBodyElement needle) { diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java index a2f14a7e76..b5f24dd718 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFootnote.java @@ -269,8 +269,10 @@ public class XWPFFootnote implements Iterable, IBody { i++; } bodyElements.add(i, newT); - cursor = t.newCursor(); + XmlCursor c2 = t.newCursor(); + cursor.toCursor(c2); cursor.toEndToken(); + c2.dispose(); return newT; } return null; @@ -283,7 +285,7 @@ public class XWPFFootnote implements Iterable, IBody { * @return the inserted paragraph * @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor) */ - public XWPFParagraph insertNewParagraph(XmlCursor cursor) { + public XWPFParagraph insertNewParagraph(final XmlCursor cursor) { if (isCursorInFtn(cursor)) { String uri = CTP.type.getName().getNamespaceURI(); String localPart = "p"; @@ -302,15 +304,19 @@ public class XWPFFootnote implements Iterable, IBody { paragraphs.add(pos, newP); } int i = 0; - cursor.toCursor(p.newCursor()); + XmlCursor p2 = p.newCursor(); + cursor.toCursor(p2); + p2.dispose(); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newP); - cursor.toCursor(p.newCursor()); + p2 = p.newCursor(); + cursor.toCursor(p2); cursor.toEndToken(); + p2.dispose(); return newP; } return null; diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java index 415204aace..f4a13e454e 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java @@ -355,15 +355,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo paragraphs.add(pos, newP); } int i = 0; - cursor.toCursor(p.newCursor()); + XmlCursor p2 = p.newCursor(); + cursor.toCursor(p2); + p2.dispose(); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newP); - cursor.toCursor(p.newCursor()); + p2 = p.newCursor(); + cursor.toCursor(p2); cursor.toEndToken(); + p2.dispose(); return newP; } return null; @@ -374,7 +378,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo * @param cursor * @return the inserted table */ - public XWPFTable insertNewTbl(XmlCursor cursor) { + public XWPFTable insertNewTbl(final XmlCursor cursor) { if (isCursorInHdrF(cursor)) { String uri = CTTbl.type.getName().getNamespaceURI(); String localPart = "tbl"; @@ -394,15 +398,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo tables.add(pos, newT); } int i = 0; - cursor = t.newCursor(); - while (cursor.toPrevSibling()) { - o = cursor.getObject(); - if (o instanceof CTP || o instanceof CTTbl) + XmlCursor cursor2 = t.newCursor(); + while (cursor2.toPrevSibling()) { + o = cursor2.getObject(); + if (o instanceof CTP || o instanceof CTTbl) { i++; + } } + cursor2.dispose(); bodyElements.add(i, newT); - cursor = t.newCursor(); + cursor2 = t.newCursor(); + cursor.toCursor(cursor2); cursor.toEndToken(); + cursor2.dispose(); return newT; } return null; @@ -416,10 +424,9 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo private boolean isCursorInHdrF(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); - if (verify.getObject() == this.headerFooter) { - return true; - } - return false; + boolean result = (verify.getObject() == this.headerFooter); + verify.dispose(); + return result; } @@ -492,6 +499,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo cursor.toParent(); XmlObject o = cursor.getObject(); if (!(o instanceof CTRow)) { + cursor.dispose(); return null; } CTRow row = (CTRow) o; diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 57ff6bcea6..4e2d3654d1 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -1396,58 +1396,59 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para int beginRunPos = 0, candCharPos = 0; boolean newList = false; - @SuppressWarnings("deprecation") CTR[] rArray = paragraph.getRArray(); for (int runPos = startRun; runPos < rArray.length; runPos++) { int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0; CTR ctRun = rArray[runPos]; XmlCursor c = ctRun.newCursor(); c.selectPath("./*"); - while (c.toNextSelection()) { - XmlObject o = c.getObject(); - if (o instanceof CTText) { - if (textPos >= startText) { - String candidate = ((CTText) o).getStringValue(); - if (runPos == startRun) - charPos = startChar; - else - charPos = 0; - - for (; charPos < candidate.length(); charPos++) { - if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) { - beginTextPos = textPos; - beginCharPos = charPos; - beginRunPos = runPos; - newList = true; - } - if (candidate.charAt(charPos) == searched.charAt(candCharPos)) { - if (candCharPos + 1 < searched.length()) - candCharPos++; - else if (newList) { - TextSegement segement = new TextSegement(); - segement.setBeginRun(beginRunPos); - segement.setBeginText(beginTextPos); - segement.setBeginChar(beginCharPos); - segement.setEndRun(runPos); - segement.setEndText(textPos); - segement.setEndChar(charPos); - return segement; + try { + while (c.toNextSelection()) { + XmlObject o = c.getObject(); + if (o instanceof CTText) { + if (textPos >= startText) { + String candidate = ((CTText) o).getStringValue(); + if (runPos == startRun) + charPos = startChar; + else + charPos = 0; + + for (; charPos < candidate.length(); charPos++) { + if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) { + beginTextPos = textPos; + beginCharPos = charPos; + beginRunPos = runPos; + newList = true; + } + if (candidate.charAt(charPos) == searched.charAt(candCharPos)) { + if (candCharPos + 1 < searched.length()) + candCharPos++; + else if (newList) { + TextSegement segement = new TextSegement(); + segement.setBeginRun(beginRunPos); + segement.setBeginText(beginTextPos); + segement.setBeginChar(beginCharPos); + segement.setEndRun(runPos); + segement.setEndText(textPos); + segement.setEndChar(charPos); + return segement; + } + } else { + candCharPos = 0; } - } else { - candCharPos = 0; } } - } - textPos++; - } else if (o instanceof CTProofErr) { - c.removeXml(); - } else if (o instanceof CTRPr) ; - //do nothing - else - candCharPos = 0; + textPos++; + } else if (o instanceof CTProofErr) { + c.removeXml(); + } else if (o instanceof CTRPr) ; + //do nothing + else + candCharPos = 0; + } + } finally { + c.dispose(); } - - c.dispose(); } return null; } @@ -1465,10 +1466,8 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para int textEnd = segment.getEndText(); int charEnd = segment.getEndChar(); StringBuilder out = new StringBuilder(); - @SuppressWarnings("deprecation") CTR[] rArray = paragraph.getRArray(); for (int i = runBegin; i <= runEnd; i++) { - @SuppressWarnings("deprecation") CTText[] tArray = rArray[i].getTArray(); int startText = 0, endText = tArray.length - 1; if (i == runBegin) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java index 6942aa749d..d160e662e3 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java @@ -76,7 +76,8 @@ public class XWPFSDTContent implements ISDTContent { runs.add(run); bodyElements.add(run); } - } + } + cursor.dispose(); } public String getText() { diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java index 17f388b59e..4ded4787ea 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java @@ -84,7 +84,8 @@ public class XWPFSDTContentCell implements ISDTContent { depth--; } } - text = sb.toString(); + text = sb.toString(); + cursor.dispose(); } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 39a92f41cf..4f1a77cfb7 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -258,7 +258,7 @@ public class XWPFTableCell implements IBody, ICell { * @param cursor * @return the inserted paragraph */ - public XWPFParagraph insertNewParagraph(XmlCursor cursor) { + public XWPFParagraph insertNewParagraph(final XmlCursor cursor) { if (!isCursorInTableCell(cursor)) { return null; } @@ -280,19 +280,23 @@ public class XWPFTableCell implements IBody, ICell { paragraphs.add(pos, newP); } int i = 0; - cursor.toCursor(p.newCursor()); + XmlCursor p2 = p.newCursor(); + cursor.toCursor(p2); + p2.dispose(); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newP); - cursor.toCursor(p.newCursor()); + p2 = p.newCursor(); + cursor.toCursor(p2); + p2.dispose(); cursor.toEndToken(); return newP; } - public XWPFTable insertNewTbl(XmlCursor cursor) { + public XWPFTable insertNewTbl(final XmlCursor cursor) { if (isCursorInTableCell(cursor)) { String uri = CTTbl.type.getName().getNamespaceURI(); String localPart = "tbl"; @@ -312,15 +316,18 @@ public class XWPFTableCell implements IBody, ICell { tables.add(pos, newT); } int i = 0; - cursor = t.newCursor(); - while (cursor.toPrevSibling()) { - o = cursor.getObject(); + XmlCursor cursor2 = t.newCursor(); + while (cursor2.toPrevSibling()) { + o = cursor2.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } + cursor2.dispose(); bodyElements.add(i, newT); - cursor = t.newCursor(); + cursor2 = t.newCursor(); + cursor.toCursor(cursor2); cursor.toEndToken(); + cursor2.dispose(); return newT; } return null; @@ -332,10 +339,9 @@ public class XWPFTableCell implements IBody, ICell { private boolean isCursorInTableCell(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); - if (verify.getObject() == this.ctTc) { - return true; - } - return false; + boolean result = (verify.getObject() == this.ctTc); + verify.dispose(); + return result; } /** diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index 2ec97ec298..eb1ec77753 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -146,7 +146,8 @@ public class XWPFTableRow { } else if (o instanceof CTSdtCell) { cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody())); } - } + } + cursor.dispose(); return cells; } -- 2.39.5