-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SiteMapParser#walkSiteMap(URL,Consumer) #190
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,9 @@ | |
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
|
@@ -265,6 +267,78 @@ public AbstractSiteMap parseSiteMap(String contentType, byte[] content, URL url) | |
throw new UnknownFormatException("Can't parse a sitemap with the MediaType of: " + contentType + " (at: " + url + ")"); | ||
} | ||
|
||
/** | ||
* Fetch a sitemap from the specified URL, recursively fetching and | ||
* traversing the content of any enclosed sitemap index, and performing the | ||
* specified action for each sitemap URL until all URLs have been processed | ||
* or the action throws an exception. | ||
* <p> | ||
* This method is a convenience method for a user who has a sitemap URL and | ||
* wants a simple way to traverse it. | ||
* <p> | ||
* Exceptions thrown by the action are relayed to the caller. | ||
* | ||
* @param onlineSitemapUrl | ||
* URL of the online sitemap | ||
* @param action | ||
* The action to be performed for each element | ||
* @throws UnknownFormatException | ||
* if there is an error parsing the sitemap | ||
* @throws IOException | ||
* if there is an error fetching the content of any | ||
* {@link java.net.URL} | ||
*/ | ||
public void walkSiteMap(URL onlineSitemapUrl, Consumer<SiteMapURL> action) throws UnknownFormatException, IOException { | ||
if (action == null) { | ||
return; | ||
} | ||
walkSiteMap(parseSiteMap(onlineSitemapUrl), action); | ||
} | ||
|
||
/** | ||
* Traverse a sitemap, recursively fetching and traversing the content of | ||
* any enclosed sitemap index, and performing the specified action for each | ||
* sitemap URL until all URLs have been processed or the action throws an | ||
* exception. | ||
* <p> | ||
* This method is a convenience method for a user who has a sitemap and | ||
* wants a simple way to traverse it. | ||
* <p> | ||
* Exceptions thrown by the action are relayed to the caller. | ||
* | ||
* @param sitemap | ||
* The sitemap to traverse | ||
* @param action | ||
* The action to be performed for each element | ||
* @throws UnknownFormatException | ||
* if there is an error parsing the sitemap | ||
* @throws IOException | ||
* if there is an error fetching the content of any | ||
* {@link java.net.URL} | ||
*/ | ||
public void walkSiteMap(AbstractSiteMap sitemap, Consumer<SiteMapURL> action) throws UnknownFormatException, IOException { | ||
if (sitemap == null || action == null) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment using an slf4j logger will be appreciated here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
if (sitemap.isIndex()) { | ||
final Collection<AbstractSiteMap> links = ((SiteMapIndex) sitemap).getSitemaps(); | ||
for (final AbstractSiteMap asm : links) { | ||
if (asm == null) { | ||
continue; | ||
} | ||
walkSiteMap(asm.getUrl(), action); | ||
} | ||
} else { | ||
final Collection<SiteMapURL> links = ((SiteMap) sitemap).getSiteMapUrls(); | ||
for (final SiteMapURL url : links) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: why not write the loop as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No good reason, it just mirrors the branch for sitemap indices above. I can add a commit with your version if you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem. It's a matter of taste whether to use "classic" loops or functional streams. |
||
if (url == null) { | ||
continue; | ||
} | ||
action.accept(url); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Parse the given XML content. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment using an slf4j logger will be appreciated here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
I would usually throw a
NullPointerException
in this context, but I noticed that methods likeSiteMapParser#parseSiteMap(URL)
are null-safe, hence the early return statement, for the sake of consistency.