use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability in project timbuctoo by HuygensING.
the class RsExplorer method extractCapability.
private Capability extractCapability(Result<RsRoot> result) {
String xmlString = result.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("");
Capability capa = null;
try {
capa = Capability.forString(xmlString);
} catch (IllegalArgumentException e) {
result.addError(new RemoteResourceSyncFrameworkException(String.format("invalid value for capability: '%s'", xmlString)));
}
return capa;
}
use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability in project timbuctoo by HuygensING.
the class RsExplorer method explore.
@SuppressWarnings("unchecked")
@Override
public Result<RsRoot> explore(URI uri, ResultIndex index) {
LOG.debug("Exploring URI " + uri);
Result<RsRoot> result = execute(uri, getSitemapConverter());
index.add(result);
Capability capability = extractCapability(result);
if (followParentLinks) {
// rs:ln rel="up" -> points to parent document, a urlset.
String parentLink = result.getContent().map(rsRoot -> rsRoot.getLinkHref("up")).orElse(null);
if (parentLink != null && !index.contains(parentLink)) {
try {
URI parentUri = new URI(parentLink);
Result<RsRoot> parentResult = explore(parentUri, index);
result.addParent(parentResult);
verifyUpRelation(result, parentResult, capability);
} catch (URISyntaxException e) {
index.addInvalidUri(parentLink);
result.addError(e);
result.addInvalidUri(parentLink);
}
}
}
if (followIndexLinks) {
// rs:ln rel="index" -> points to parent index, a sitemapindex.
String indexLink = result.getContent().map(rsRoot -> rsRoot.getLinkHref("index")).orElse(null);
if (indexLink != null && !index.contains(indexLink)) {
try {
URI indexUri = new URI(indexLink);
Result<RsRoot> indexResult = explore(indexUri, index);
result.addParent(indexResult);
verifyIndexRelation(result, indexResult, capability);
} catch (URISyntaxException e) {
index.addInvalidUri(indexLink);
result.addError(e);
result.addInvalidUri(indexLink);
}
}
}
if (followChildLinks) {
// elements <url> or <sitemap> have the location of the children of result.
// children of Urlset with capability resourcelist, resourcedump, changelist, changedump
// are the resources them selves. do not explore these with this explorer.
String xmlString = result.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("invalid");
boolean isSitemapindex = result.getContent().map(rsRoot -> rsRoot instanceof Sitemapindex).orElse(false);
if (Capability.levelfor(xmlString) > Capability.RESOURCELIST.level || isSitemapindex) {
List<RsItem> itemList = result.getContent().map(RsRoot::getItemList).orElse(Collections.emptyList());
for (RsItem item : itemList) {
String childLink = item.getLoc();
if (childLink != null && !index.contains(childLink)) {
try {
URI childUri = new URI(childLink);
Result<RsRoot> childResult = explore(childUri, index);
result.addChild(childResult);
verifyChildRelation(result, childResult, capability);
Optional<RsLn> maybeDescribedByLink = item.getLink("describedBy");
maybeDescribedByLink.ifPresent(rsLn -> loadDescriptionIfApplicable(childResult, rsLn, index));
} catch (URISyntaxException e) {
index.addInvalidUri(childLink);
result.addError(e);
result.addInvalidUri(childLink);
}
}
}
}
}
Optional<RsLn> maybeDescribedByLink = result.getContent().flatMap(rsRoot -> rsRoot.getLink("describedBy"));
maybeDescribedByLink.ifPresent(rsLn -> loadDescriptionIfApplicable(result, rsLn, index));
return result;
}
Aggregations