added a finally block to ensure that the InputStream is closed if an exception is raised

master
Mohammed Le Doze 15 years ago
parent 5367907eee
commit 635c7017a9

@ -10,14 +10,11 @@
* For any reuse or distribution, you must make clear to others the * For any reuse or distribution, you must make clear to others the
* license terms of this work. * license terms of this work.
*/ */
package com.moviejukebox.themoviedb.tools; package com.moviejukebox.themoviedb.tools;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -35,6 +32,7 @@ import org.xml.sax.SAXException;
* *
*/ */
public class DOMHelper { public class DOMHelper {
/** /**
* Gets the string value of the tag element name passed * Gets the string value of the tag element name passed
* @param element * @param element
@ -43,7 +41,7 @@ public class DOMHelper {
*/ */
public static String getValueFromElement(Element element, String tagName) { public static String getValueFromElement(Element element, String tagName) {
String returnValue = ""; String returnValue = "";
try { try {
NodeList elementNodeList = element.getElementsByTagName(tagName); NodeList elementNodeList = element.getElementsByTagName(tagName);
Element tagElement = (Element) elementNodeList.item(0); Element tagElement = (Element) elementNodeList.item(0);
@ -52,7 +50,7 @@ public class DOMHelper {
} catch (Exception ignore) { } catch (Exception ignore) {
return returnValue; return returnValue;
} }
return returnValue; return returnValue;
} }
@ -60,22 +58,27 @@ public class DOMHelper {
* Get a DOM document from the supplied URL * Get a DOM document from the supplied URL
* @param url * @param url
* @return * @return
* @throws MalformedURLException
* @throws IOException * @throws IOException
* @throws ParserConfigurationException * @throws ParserConfigurationException
* @throws SAXException * @throws SAXException
*/ */
public static Document getEventDocFromUrl(String url) public static Document getEventDocFromUrl(String url)
throws MalformedURLException, IOException, ParserConfigurationException, SAXException, UnsupportedEncodingException { throws IOException, ParserConfigurationException, SAXException {
//InputStream in = (new URL(url)).openStream(); Document doc = null;
String webPage = WebBrowser.request(url); InputStream in = null;
InputStream in = new ByteArrayInputStream(webPage.getBytes("UTF-8")); try {
String webPage = WebBrowser.request(url);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); in = new ByteArrayInputStream(webPage.getBytes("UTF-8"));
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(in); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc.getDocumentElement().normalize(); doc = db.parse(in);
doc.getDocumentElement().normalize();
} finally {
if (in != null) {
in.close();
}
}
return doc; return doc;
} }
} }

Loading…
Cancel
Save