From: PJ Fanning Date: Fri, 15 Jul 2022 21:02:15 +0000 (+0000) Subject: [github-343] use try-with-resources. Thanks to XenoAmess. This closes #343 X-Git-Tag: REL_5_2_3~192 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=619db4dde52480406269ae526c69ed87f1103462;p=poi.git [github-343] use try-with-resources. Thanks to XenoAmess. This closes #343 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902748 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java index e5595f6883..74b96109e2 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/XPathHelper.java @@ -37,15 +37,15 @@ public final class XPathHelper { private static final Logger LOG = LogManager.getLogger(XPathHelper.class); private static final String OSGI_ERROR = - "Schemas (*.xsb) for can't be loaded - usually this happens when OSGI " + - "loading is used and the thread context classloader has no reference to " + - "the xmlbeans classes - please either verify if the .xsb is on the " + - "classpath or alternatively try to use the poi-ooxml-full-x.x.jar"; + "Schemas (*.xsb) for can't be loaded - usually this happens when OSGI " + + "loading is used and the thread context classloader has no reference to " + + "the xmlbeans classes - please either verify if the .xsb is on the " + + "classpath or alternatively try to use the poi-ooxml-full-x.x.jar"; private static final String MC_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006"; private static final String MAC_DML_NS = "http://schemas.microsoft.com/office/mac/drawingml/2008/main"; private static final QName ALTERNATE_CONTENT_TAG = new QName(MC_NS, "AlternateContent"); - // AlternateContentDocument.AlternateContent.type.getName(); + // AlternateContentDocument.AlternateContent.type.getName(); private XPathHelper() {} @@ -109,9 +109,10 @@ public final class XPathHelper { public static T selectProperty(XmlObject startObject, Class resultClass, XSLFShape.ReparseFactory factory, QName[]... path) throws XmlException { XmlObject xo = startObject; - XmlCursor innerCur = null; - try (XmlCursor cur = startObject.newCursor()) { - innerCur = selectProperty(cur, path, 0, factory != null, false); + try ( + XmlCursor cur = startObject.newCursor(); + XmlCursor innerCur = selectProperty(cur, path, 0, factory != null, false) + ) { if (innerCur == null) { return null; } @@ -122,7 +123,7 @@ public final class XPathHelper { if (xo instanceof XmlAnyTypeImpl) { String errorTxt = OSGI_ERROR .replace("", resultClass.getSimpleName()) - .replace("", resultClass.getSimpleName().toLowerCase(Locale.ROOT)+"*"); + .replace("", resultClass.getSimpleName().toLowerCase(Locale.ROOT) + "*"); if (factory == null) { throw new XmlException(errorTxt); } else { @@ -130,11 +131,7 @@ public final class XPathHelper { } } - return (T)xo; - } finally { - if (innerCur != null) { - innerCur.close(); - } + return (T) xo; } } @@ -169,8 +166,8 @@ public final class XPathHelper { // it never happens when using poi-ooxml-full jar but may happen with the abridged poi-ooxml-lite jar if (!reparseAlternate) { throw new XmlException(OSGI_ERROR - .replace("", "AlternateContent") - .replace("", "alternatecontentelement") + .replace("", "AlternateContent") + .replace("", "alternatecontentelement") ); } try {