Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

lucboruta
Copy link
Contributor

@lucboruta lucboruta commented Dec 15, 2017

This PR adds a convenience method to fetch a sitemap from the specified URL, recursively traverse any enclosed sitemap index, and perform the specified action for each sitemap URL. It is strongly inspired by methods like Iterable#forEach and Stream#forEach.

The code is based on SitemapParser.java by @Chaiavi, with extra checks to avoid NullPointerExceptions.

I added the method to SiteMapParser rather than AbstractSiteMap since the parser is needed to recurse into sitemap indices.

I'm open to suggestions regarding the method's name and null-friendliness.

@lewismc
Copy link
Member

lewismc commented Dec 15, 2017

@lucboruta this is really nice. I think a unit test to demonstrate a Consumer action would be ideal.

@lucboruta
Copy link
Contributor Author

Cool. Since we don't want unit tests to depend on remote content, I'll overload the first method with a few other methods working on byte arrays, content types, etc., as was done for SiteMapParser#parseSiteMap.

@lucboruta
Copy link
Contributor Author

Since the different parseSiteMap methods all return an AbstractSiteMap, duplicating all these signatures would be cumbersome. I simply added SiteMapParser#walkSiteMap(AbstractSiteMap,Consumer) and a unit test for that new method.

This PR now adds two public convenience methods:

  • SiteMapParser#walkSiteMap(URL,Consumer) to fetch and traverse a sitemap, starting from the URL of the online sitemap;
  • SiteMapParser#walkSiteMap(AbstractSiteMap,Consumer) to traverse a sitemap, starting from any AbstractSiteMap.

}
} else {
final Collection<SiteMapURL> links = ((SiteMap) sitemap).getSiteMapUrls();
for (final SiteMapURL url : links) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why not write the loop as links.stream().filter(Objects::nonNull).forEach(action);?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@sebastian-nagel
Copy link
Contributor

+1 lgtm

  • adds a useful API method (esp. by fetching recursively sitemaps of a sitemap index)
  • for the long therm we could move the execution of the action to the parsing code to reduce the memory footprint

*/
public void walkSiteMap(URL onlineSitemapUrl, Consumer<SiteMapURL> action) throws UnknownFormatException, IOException {
if (action == null) {
return;
Copy link
Member

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

Copy link
Contributor Author

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 like SiteMapParser#parseSiteMap(URL) are null-safe, hence the early return statement, for the sake of consistency.

*/
public void walkSiteMap(AbstractSiteMap sitemap, Consumer<SiteMapURL> action) throws UnknownFormatException, IOException {
if (sitemap == null || action == null) {
return;
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

sebastian-nagel added a commit that referenced this pull request Apr 25, 2018
Add SiteMapParser#walkSiteMap(URL,Consumer)
@sebastian-nagel
Copy link
Contributor

Resolved conflicts, rebased, updated changelog and merged 8ca46ff. Thanks, @lucboruta! Thanks, for the reviews (@Chaiavi, @lewismc)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants