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;
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;
}
} catch (InvalidFormatException | IOException e) {
throw new POIXMLException(e);
+ } finally {
+ c.dispose();
}
- c.dispose();
}
}
} catch (InvalidFormatException e){
throw new POIXMLException(e);
+ } finally {
+ c.dispose();
}
- c.dispose();
}
}
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;
}
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);
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
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();
+ }
}
}
}
@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();
+ }
}
}
*/
@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;
}
} 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);
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
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;
}
}
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();
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();
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;
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)) {
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;
*/
@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;
}
@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;
}
*/
@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;
}
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();
}
/**
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);
}
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);
}
*/
public void clearHeaderFooter() {
XmlCursor c = headerFooter.newCursor();
- c.removeXmlContents();
- c.dispose();
+ try {
+ c.removeXmlContents();
+ } finally {
+ c.dispose();
+ }
paragraphs.clear();
tables.clear();
bodyElements.clear();
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;
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;
*/
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();
+ }
}
// 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();
}
/**
*/
@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;
}
// 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();
}
}
@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
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
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;
}
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;
*/
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
*/
@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;
}
//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;
}