diff --git a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java index 6544c5f..99b5242 100644 --- a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java @@ -1,18 +1,17 @@ package com.redfin.sitemapgenerator; +import org.xml.sax.SAXException; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; -import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.zip.GZIPOutputStream; -import org.xml.sax.SAXException; - abstract class SitemapGenerator> { /** 50000 URLs per sitemap maximum */ public static final int MAX_URLS_PER_SITEMAP = 50000; @@ -69,7 +68,11 @@ abstract class SitemapGenerator write() { if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps"); if (!allowEmptySitemap && urls.isEmpty() && mapCount == 0) throw new RuntimeException("No URLs added, sitemap would be empty; you must add some URLs with addUrls"); - writeSiteMap(); + try { + writeSiteMap(); + } catch (IOException ex) { + throw new RuntimeException("Closing of streams has failed at some point.", ex); + } finished = true; return outFiles; } @@ -231,7 +236,7 @@ abstract class SitemapGenerator\n"); } out.write(""); - out.close(); } } diff --git a/src/main/java/com/redfin/sitemapgenerator/SitemapValidator.java b/src/main/java/com/redfin/sitemapgenerator/SitemapValidator.java index d6e9e73..18af900 100644 --- a/src/main/java/com/redfin/sitemapgenerator/SitemapValidator.java +++ b/src/main/java/com/redfin/sitemapgenerator/SitemapValidator.java @@ -1,9 +1,7 @@ package com.redfin.sitemapgenerator; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; import javax.xml.XMLConstants; import javax.xml.transform.sax.SAXSource; @@ -11,9 +9,10 @@ import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; /** Validates sitemaps and sitemap indexes * @@ -39,23 +38,30 @@ public class SitemapValidator { private synchronized static void lazyLoad() { if (sitemapSchema != null) return; SchemaFactory factory = - SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { - InputStream stream = SitemapValidator.class.getResourceAsStream("sitemap.xsd"); - if (stream == null) throw new RuntimeException("BUG Couldn't load sitemap.xsd"); - StreamSource source = new StreamSource(stream); - sitemapSchema = factory.newSchema(source); - stream.close(); - - stream = SitemapValidator.class.getResourceAsStream("siteindex.xsd"); - if (stream == null) throw new RuntimeException("BUG Couldn't load siteindex.xsd"); - source = new StreamSource(stream); - sitemapIndexSchema = factory.newSchema(source); - stream.close(); + sitemapSchema = lazyLoad(factory, "sitemap.xsd"); + sitemapIndexSchema = lazyLoad(factory, "siteindex.xsd"); } catch (Exception e) { throw new RuntimeException("BUG", e); } } + + private synchronized static Schema lazyLoad(SchemaFactory factory, String resource) throws IOException, SAXException { + InputStream stream = null; + + try { + stream = SitemapValidator.class.getResourceAsStream(resource); + if (stream == null) throw new RuntimeException("BUG Couldn't load " + resource); + StreamSource source = new StreamSource(stream); + return factory.newSchema(source); + } finally { + if(stream != null) { + stream.close(); + } + } + + } /** Validates an ordinary web sitemap file (NOT a Google-specific sitemap) */ public static void validateWebSitemap(File sitemap) throws SAXException { @@ -70,15 +76,24 @@ public class SitemapValidator { } private static void validateXml(File sitemap, Schema schema) throws SAXException { - Validator validator = schema.newValidator(); try { - FileReader reader = new FileReader(sitemap); - SAXSource source = new SAXSource(new InputSource(reader)); - validator.validate(source); - reader.close(); - } catch (IOException e) { - throw new RuntimeException(e); + Validator validator = schema.newValidator(); + FileReader reader = null; + try { + reader = new FileReader(sitemap); + SAXSource source = new SAXSource(new InputSource(reader)); + validator.validate(source); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + if(reader != null) { + reader.close(); + } + } + } catch (IOException ex) { + throw new RuntimeException("Unable to close stream.", ex); } + } }