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.
* @param url the URL to add to this sitemap
* @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");
UrlUtils.checkUrl(url.getUrl(), baseUrl);
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 (baseDir != null) {
if (mapCount == 0) mapCount++;
try {
writeSiteMap();
} catch(IOException ex) {
throw new RuntimeException("Closing of stream failed.", ex);
}
mapCount++;
urls.clear();
}
@ -83,9 +86,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @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);
return getThis();
}
@ -95,9 +97,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @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);
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.
* 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");
File outFile = new File(baseDir, "sitemap_index.xml");
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.
*
* @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");
SitemapIndexGenerator sig;
sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build();

View File

@ -64,18 +64,19 @@ public class SitemapValidator {
}
/** 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();
validateXml(sitemap, sitemapSchema);
}
/** Validates a sitemap index file */
public static void validateSitemapIndex(File sitemap) throws SAXException, IOException {
public static void validateSitemapIndex(File sitemap) throws SAXException {
lazyLoad();
validateXml(sitemap, sitemapIndexSchema);
}
private static void validateXml(File sitemap, Schema schema) throws SAXException, IOException {
private static void validateXml(File sitemap, Schema schema) throws SAXException {
try {
Validator validator = schema.newValidator();
FileReader reader = null;
try {
@ -89,6 +90,10 @@ public class SitemapValidator {
reader.close();
}
}
} catch (IOException ex) {
throw new RuntimeException("Unable to close stream.", ex);
}
}
}