Wraps codeblocks that could throw IOException in try-catch.

Catches IOException and throws RuntimeException instead. This is in line with code-practice.
This way the upgrade won't break any code.
This commit is contained in:
Robert van der Spek 2018-07-17 10:33:39 +02:00
parent 3c852e4474
commit 12c38f6135
2 changed files with 28 additions and 24 deletions

View File

@ -60,16 +60,19 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or else write out one sitemap immediately. * or else write out one sitemap immediately.
* @param url the URL to add to this sitemap * @param url the URL to add to this sitemap
* @return this * @return this
* @throws IOException when closing of streams has failed
*/ */
public THIS addUrl(U url) throws IOException { public THIS addUrl(U url) {
if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps"); if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
UrlUtils.checkUrl(url.getUrl(), baseUrl); UrlUtils.checkUrl(url.getUrl(), baseUrl);
if (urls.size() == maxUrls) { if (urls.size() == maxUrls) {
if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index."); if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index.");
if (baseDir != null) { if (baseDir != null) {
if (mapCount == 0) mapCount++; if (mapCount == 0) mapCount++;
writeSiteMap(); try {
writeSiteMap();
} catch(IOException ex) {
throw new RuntimeException("Closing of stream failed.", ex);
}
mapCount++; mapCount++;
urls.clear(); urls.clear();
} }
@ -83,9 +86,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or write out one sitemap immediately. * or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap * @param urls the URLs to add to this sitemap
* @return this * @return this
* @throws IOException when closing of streams has failed.
*/ */
public THIS addUrls(Iterable<? extends U> urls) throws IOException { public THIS addUrls(Iterable<? extends U> urls) {
for (U url : urls) addUrl(url); for (U url : urls) addUrl(url);
return getThis(); return getThis();
} }
@ -95,9 +97,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or write out one sitemap immediately. * or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap * @param urls the URLs to add to this sitemap
* @return this * @return this
* @throws IOException when closing of streams has failed.
*/ */
public THIS addUrls(U... urls) throws IOException { public THIS addUrls(U... urls) {
for (U url : urls) addUrl(url); for (U url : urls) addUrl(url);
return getThis(); return getThis();
} }
@ -215,9 +216,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
/** /**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated. * After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
* The sitemap index is written to {baseDir}/sitemap_index.xml * The sitemap index is written to {baseDir}/sitemap_index.xml
* @throws IOException when closing of streams has failed
*/ */
public File writeSitemapsWithIndex() throws IOException { public File writeSitemapsWithIndex() {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first"); if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first");
File outFile = new File(baseDir, "sitemap_index.xml"); File outFile = new File(baseDir, "sitemap_index.xml");
return writeSitemapsWithIndex(outFile); return writeSitemapsWithIndex(outFile);
@ -227,9 +227,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated. * After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
* *
* @param outFile the destination file of the sitemap index. * @param outFile the destination file of the sitemap index.
* @throws IOException when closing of streams has failed
*/ */
public File writeSitemapsWithIndex(File outFile) throws IOException { public File writeSitemapsWithIndex(File outFile) {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first"); if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first");
SitemapIndexGenerator sig; SitemapIndexGenerator sig;
sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build(); sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build();

View File

@ -64,31 +64,36 @@ public class SitemapValidator {
} }
/** Validates an ordinary web sitemap file (NOT a Google-specific sitemap) */ /** Validates an ordinary web sitemap file (NOT a Google-specific sitemap) */
public static void validateWebSitemap(File sitemap) throws SAXException, IOException { public static void validateWebSitemap(File sitemap) throws SAXException {
lazyLoad(); lazyLoad();
validateXml(sitemap, sitemapSchema); validateXml(sitemap, sitemapSchema);
} }
/** Validates a sitemap index file */ /** Validates a sitemap index file */
public static void validateSitemapIndex(File sitemap) throws SAXException, IOException { public static void validateSitemapIndex(File sitemap) throws SAXException {
lazyLoad(); lazyLoad();
validateXml(sitemap, sitemapIndexSchema); validateXml(sitemap, sitemapIndexSchema);
} }
private static void validateXml(File sitemap, Schema schema) throws SAXException, IOException { private static void validateXml(File sitemap, Schema schema) throws SAXException {
Validator validator = schema.newValidator();
FileReader reader = null;
try { try {
reader = new FileReader(sitemap); Validator validator = schema.newValidator();
SAXSource source = new SAXSource(new InputSource(reader)); FileReader reader = null;
validator.validate(source); try {
} catch (IOException e) { reader = new FileReader(sitemap);
throw new RuntimeException(e); SAXSource source = new SAXSource(new InputSource(reader));
} finally { validator.validate(source);
if(reader != null) { } catch (IOException e) {
reader.close(); throw new RuntimeException(e);
} finally {
if(reader != null) {
reader.close();
}
} }
} catch (IOException ex) {
throw new RuntimeException("Unable to close stream.", ex);
} }
} }
} }