#21 Übergabe von Fehlerinformation in der API

This commit is contained in:
Andreas Penski (init) 2019-05-17 14:00:59 +02:00 committed by Andreas Penski
parent 8d49b0fea3
commit 3d777fa8e5
10 changed files with 289 additions and 86 deletions

View file

@ -0,0 +1,49 @@
package de.kosit.validationtool.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
/**
* @author Andreas Penski
*/
@RequiredArgsConstructor
public class HtmlExtraction {
private final ContentRepository repository;
private XPathExecutable executable;
public List<XdmNode> extract(XdmNode xdmSource) {
try {
final XPathSelector selector = getSelector();
selector.setContextItem(xdmSource);
return selector.stream().map(this::castToNode).collect(Collectors.toList());
} catch (SaxonApiException e) {
throw new IllegalStateException("Can not extract html content", e);
}
}
private XdmNode castToNode(final XdmItem xdmItem) {
return (XdmNode) xdmItem;
}
private XPathSelector getSelector() {
if (executable == null) {
Map<String, String> ns = new HashMap<>();
ns.put("html", "http://www.w3.org/1999/xhtml");
executable = repository.createXPath("//html:html", ns);
}
return executable.load();
}
}