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
This commit is contained in:
Andreas Beeker 2016-05-16 09:54:01 +00:00
parent 12425ac107
commit 84be4acea9
8 changed files with 100 additions and 80 deletions

View File

@ -667,11 +667,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
private boolean isCursorInBody(XmlCursor cursor) { private boolean isCursorInBody(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor(); XmlCursor verify = cursor.newCursor();
verify.toParent(); verify.toParent();
try { boolean result = (verify.getObject() == this.ctDocument.getBody());
return (verify.getObject() == this.ctDocument.getBody()); verify.dispose();
} finally { return result;
verify.dispose();
}
} }
private int getPosOfBodyElement(IBodyElement needle) { private int getPosOfBodyElement(IBodyElement needle) {

View File

@ -269,8 +269,10 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
i++; i++;
} }
bodyElements.add(i, newT); bodyElements.add(i, newT);
cursor = t.newCursor(); XmlCursor c2 = t.newCursor();
cursor.toCursor(c2);
cursor.toEndToken(); cursor.toEndToken();
c2.dispose();
return newT; return newT;
} }
return null; return null;
@ -283,7 +285,7 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
* @return the inserted paragraph * @return the inserted paragraph
* @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor) * @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor)
*/ */
public XWPFParagraph insertNewParagraph(XmlCursor cursor) { public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
if (isCursorInFtn(cursor)) { if (isCursorInFtn(cursor)) {
String uri = CTP.type.getName().getNamespaceURI(); String uri = CTP.type.getName().getNamespaceURI();
String localPart = "p"; String localPart = "p";
@ -302,15 +304,19 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
paragraphs.add(pos, newP); paragraphs.add(pos, newP);
} }
int i = 0; int i = 0;
cursor.toCursor(p.newCursor()); XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) { while (cursor.toPrevSibling()) {
o = cursor.getObject(); o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl) if (o instanceof CTP || o instanceof CTTbl)
i++; i++;
} }
bodyElements.add(i, newP); bodyElements.add(i, newP);
cursor.toCursor(p.newCursor()); p2 = p.newCursor();
cursor.toCursor(p2);
cursor.toEndToken(); cursor.toEndToken();
p2.dispose();
return newP; return newP;
} }
return null; return null;

View File

@ -355,15 +355,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
paragraphs.add(pos, newP); paragraphs.add(pos, newP);
} }
int i = 0; int i = 0;
cursor.toCursor(p.newCursor()); XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) { while (cursor.toPrevSibling()) {
o = cursor.getObject(); o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl) if (o instanceof CTP || o instanceof CTTbl)
i++; i++;
} }
bodyElements.add(i, newP); bodyElements.add(i, newP);
cursor.toCursor(p.newCursor()); p2 = p.newCursor();
cursor.toCursor(p2);
cursor.toEndToken(); cursor.toEndToken();
p2.dispose();
return newP; return newP;
} }
return null; return null;
@ -374,7 +378,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* @param cursor * @param cursor
* @return the inserted table * @return the inserted table
*/ */
public XWPFTable insertNewTbl(XmlCursor cursor) { public XWPFTable insertNewTbl(final XmlCursor cursor) {
if (isCursorInHdrF(cursor)) { if (isCursorInHdrF(cursor)) {
String uri = CTTbl.type.getName().getNamespaceURI(); String uri = CTTbl.type.getName().getNamespaceURI();
String localPart = "tbl"; String localPart = "tbl";
@ -394,15 +398,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
tables.add(pos, newT); tables.add(pos, newT);
} }
int i = 0; int i = 0;
cursor = t.newCursor(); XmlCursor cursor2 = t.newCursor();
while (cursor.toPrevSibling()) { while (cursor2.toPrevSibling()) {
o = cursor.getObject(); o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) if (o instanceof CTP || o instanceof CTTbl) {
i++; i++;
}
} }
cursor2.dispose();
bodyElements.add(i, newT); bodyElements.add(i, newT);
cursor = t.newCursor(); cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken(); cursor.toEndToken();
cursor2.dispose();
return newT; return newT;
} }
return null; return null;
@ -416,10 +424,9 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
private boolean isCursorInHdrF(XmlCursor cursor) { private boolean isCursorInHdrF(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor(); XmlCursor verify = cursor.newCursor();
verify.toParent(); verify.toParent();
if (verify.getObject() == this.headerFooter) { boolean result = (verify.getObject() == this.headerFooter);
return true; verify.dispose();
} return result;
return false;
} }
@ -492,6 +499,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
cursor.toParent(); cursor.toParent();
XmlObject o = cursor.getObject(); XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) { if (!(o instanceof CTRow)) {
cursor.dispose();
return null; return null;
} }
CTRow row = (CTRow) o; CTRow row = (CTRow) o;

View File

@ -1396,58 +1396,59 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
int beginRunPos = 0, candCharPos = 0; int beginRunPos = 0, candCharPos = 0;
boolean newList = false; boolean newList = false;
@SuppressWarnings("deprecation")
CTR[] rArray = paragraph.getRArray(); CTR[] rArray = paragraph.getRArray();
for (int runPos = startRun; runPos < rArray.length; runPos++) { for (int runPos = startRun; runPos < rArray.length; runPos++) {
int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0; int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0;
CTR ctRun = rArray[runPos]; CTR ctRun = rArray[runPos];
XmlCursor c = ctRun.newCursor(); XmlCursor c = ctRun.newCursor();
c.selectPath("./*"); c.selectPath("./*");
while (c.toNextSelection()) { try {
XmlObject o = c.getObject(); while (c.toNextSelection()) {
if (o instanceof CTText) { XmlObject o = c.getObject();
if (textPos >= startText) { if (o instanceof CTText) {
String candidate = ((CTText) o).getStringValue(); if (textPos >= startText) {
if (runPos == startRun) String candidate = ((CTText) o).getStringValue();
charPos = startChar; if (runPos == startRun)
else charPos = startChar;
charPos = 0; else
charPos = 0;
for (; charPos < candidate.length(); charPos++) {
if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) { for (; charPos < candidate.length(); charPos++) {
beginTextPos = textPos; if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
beginCharPos = charPos; beginTextPos = textPos;
beginRunPos = runPos; beginCharPos = charPos;
newList = true; beginRunPos = runPos;
} newList = true;
if (candidate.charAt(charPos) == searched.charAt(candCharPos)) { }
if (candCharPos + 1 < searched.length()) if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
candCharPos++; if (candCharPos + 1 < searched.length())
else if (newList) { candCharPos++;
TextSegement segement = new TextSegement(); else if (newList) {
segement.setBeginRun(beginRunPos); TextSegement segement = new TextSegement();
segement.setBeginText(beginTextPos); segement.setBeginRun(beginRunPos);
segement.setBeginChar(beginCharPos); segement.setBeginText(beginTextPos);
segement.setEndRun(runPos); segement.setBeginChar(beginCharPos);
segement.setEndText(textPos); segement.setEndRun(runPos);
segement.setEndChar(charPos); segement.setEndText(textPos);
return segement; segement.setEndChar(charPos);
return segement;
}
} else {
candCharPos = 0;
} }
} else {
candCharPos = 0;
} }
} }
} textPos++;
textPos++; } else if (o instanceof CTProofErr) {
} else if (o instanceof CTProofErr) { c.removeXml();
c.removeXml(); } else if (o instanceof CTRPr) ;
} else if (o instanceof CTRPr) ; //do nothing
//do nothing else
else candCharPos = 0;
candCharPos = 0; }
} finally {
c.dispose();
} }
c.dispose();
} }
return null; return null;
} }
@ -1465,10 +1466,8 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
int textEnd = segment.getEndText(); int textEnd = segment.getEndText();
int charEnd = segment.getEndChar(); int charEnd = segment.getEndChar();
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
@SuppressWarnings("deprecation")
CTR[] rArray = paragraph.getRArray(); CTR[] rArray = paragraph.getRArray();
for (int i = runBegin; i <= runEnd; i++) { for (int i = runBegin; i <= runEnd; i++) {
@SuppressWarnings("deprecation")
CTText[] tArray = rArray[i].getTArray(); CTText[] tArray = rArray[i].getTArray();
int startText = 0, endText = tArray.length - 1; int startText = 0, endText = tArray.length - 1;
if (i == runBegin) if (i == runBegin)

View File

@ -76,7 +76,8 @@ public class XWPFSDTContent implements ISDTContent {
runs.add(run); runs.add(run);
bodyElements.add(run); bodyElements.add(run);
} }
} }
cursor.dispose();
} }
public String getText() { public String getText() {

View File

@ -84,7 +84,8 @@ public class XWPFSDTContentCell implements ISDTContent {
depth--; depth--;
} }
} }
text = sb.toString(); text = sb.toString();
cursor.dispose();
} }

View File

@ -258,7 +258,7 @@ public class XWPFTableCell implements IBody, ICell {
* @param cursor * @param cursor
* @return the inserted paragraph * @return the inserted paragraph
*/ */
public XWPFParagraph insertNewParagraph(XmlCursor cursor) { public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
if (!isCursorInTableCell(cursor)) { if (!isCursorInTableCell(cursor)) {
return null; return null;
} }
@ -280,19 +280,23 @@ public class XWPFTableCell implements IBody, ICell {
paragraphs.add(pos, newP); paragraphs.add(pos, newP);
} }
int i = 0; int i = 0;
cursor.toCursor(p.newCursor()); XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) { while (cursor.toPrevSibling()) {
o = cursor.getObject(); o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl) if (o instanceof CTP || o instanceof CTTbl)
i++; i++;
} }
bodyElements.add(i, newP); bodyElements.add(i, newP);
cursor.toCursor(p.newCursor()); p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
cursor.toEndToken(); cursor.toEndToken();
return newP; return newP;
} }
public XWPFTable insertNewTbl(XmlCursor cursor) { public XWPFTable insertNewTbl(final XmlCursor cursor) {
if (isCursorInTableCell(cursor)) { if (isCursorInTableCell(cursor)) {
String uri = CTTbl.type.getName().getNamespaceURI(); String uri = CTTbl.type.getName().getNamespaceURI();
String localPart = "tbl"; String localPart = "tbl";
@ -312,15 +316,18 @@ public class XWPFTableCell implements IBody, ICell {
tables.add(pos, newT); tables.add(pos, newT);
} }
int i = 0; int i = 0;
cursor = t.newCursor(); XmlCursor cursor2 = t.newCursor();
while (cursor.toPrevSibling()) { while (cursor2.toPrevSibling()) {
o = cursor.getObject(); o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) if (o instanceof CTP || o instanceof CTTbl)
i++; i++;
} }
cursor2.dispose();
bodyElements.add(i, newT); bodyElements.add(i, newT);
cursor = t.newCursor(); cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken(); cursor.toEndToken();
cursor2.dispose();
return newT; return newT;
} }
return null; return null;
@ -332,10 +339,9 @@ public class XWPFTableCell implements IBody, ICell {
private boolean isCursorInTableCell(XmlCursor cursor) { private boolean isCursorInTableCell(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor(); XmlCursor verify = cursor.newCursor();
verify.toParent(); verify.toParent();
if (verify.getObject() == this.ctTc) { boolean result = (verify.getObject() == this.ctTc);
return true; verify.dispose();
} return result;
return false;
} }
/** /**

View File

@ -146,7 +146,8 @@ public class XWPFTableRow {
} else if (o instanceof CTSdtCell) { } else if (o instanceof CTSdtCell) {
cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody())); cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody()));
} }
} }
cursor.dispose();
return cells; return cells;
} }