Commit ba3c93e2 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Use parameterized XPath query

parent 23b2d885
package de.hdm_stuttgart.mi.sda1.exam.movie_by_actor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
......@@ -14,57 +14,61 @@ import org.jdom2.xpath.XPathFactory;
import de.hdm_stuttgart.mi.unitmarking.jdom.DomFilter;
public class Moviedb2MovieByActor implements DomFilter {
final Logger log = LogManager.getLogger(Moviedb2MovieByActor.class);
Element movieDb;
static private final String SEARCH_BY_ID_KEY = "searchById";
static { // Initialize static parameterized Xpath query object,
// see https://freedocs.mi.hdm-stuttgart.de/__slidedom.html#/sda1_dom_fig_domXpathVariables.
final Map<String, Object> xpathVarsNamespacePrefix = new HashMap<>();
xpathVarsNamespacePrefix.put(SEARCH_BY_ID_KEY, null /* default namespace */);
searchMovieByActorId = XPathFactory.instance().compile(
"/movieDb/movies/movie[cast/actor[@ref=$" + SEARCH_BY_ID_KEY + "]]",
Filters.element(),
xpathVarsNamespacePrefix);
}
static private final XPathExpression<Element> searchMovieByActorId;
@Override
public Document process(final Document input) {
final Element html = new Element("html");
final Document htmlDoc = new Document(html);
final Element body = new Element("body");
html.addContent(body);
final Element dl = new Element("dl");
body.addContent(new Element("h1")
.addContent("List of actors among with their corresponding films"))
.addContent(dl);
movieDb = input.getRootElement();
movieDb.getChild("persons").getChildren().stream().forEach(
input.getRootElement().getChild("persons").getChildren().forEach(
p -> addFilmsToActor(dl, p)
);
);
return htmlDoc;
}
void addFilmsToActor(final Element dl, final Element person) {
final String movies2actorId = "movie[cast/actor[@ref='"
+ person.getAttributeValue("id") +"']]";
final XPathExpression<Element> xpath =
XPathFactory.instance().compile(movies2actorId, Filters.element());
final List<Element> titles = xpath.evaluate(movieDb.getChild("movies"));
if (0 < titles.size()) {
private void addFilmsToActor(final Element dl, final Element person) {
searchMovieByActorId.setVariable(SEARCH_BY_ID_KEY, person.getAttributeValue("id"));
final List<Element> movies = searchMovieByActorId.evaluate(person);
if (0 < movies.size()) {
final Element dt = new Element("dt"),
dd = new Element("dd");
dl.addContent(dt).addContent(dd);
dt.addContent(person.getChild("name").getText());
titles.stream()
.map(t -> t.getChild("title").getText())
.forEach(s -> {
dd.addContent(new Element("p").addContent(s));
});
movies.stream()
.map(t -> t.getChild("title").getText())
.forEach(s -> dd.addContent(new Element("p").addContent(s)));
}
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment