Skip to content
Snippets Groups Projects
Commit ba3c93e2 authored by Goik Martin's avatar Goik Martin
Browse files

Use parameterized XPath query

parent 23b2d885
No related branches found
No related tags found
No related merge requests found
package de.hdm_stuttgart.mi.sda1.exam.movie_by_actor; package de.hdm_stuttgart.mi.sda1.exam.movie_by_actor;
import java.util.HashMap;
import java.util.List; 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.Document;
import org.jdom2.Element; import org.jdom2.Element;
import org.jdom2.filter.Filters; import org.jdom2.filter.Filters;
...@@ -14,57 +14,61 @@ import org.jdom2.xpath.XPathFactory; ...@@ -14,57 +14,61 @@ import org.jdom2.xpath.XPathFactory;
import de.hdm_stuttgart.mi.unitmarking.jdom.DomFilter; import de.hdm_stuttgart.mi.unitmarking.jdom.DomFilter;
public class Moviedb2MovieByActor implements DomFilter { public class Moviedb2MovieByActor implements DomFilter {
final Logger log = LogManager.getLogger(Moviedb2MovieByActor.class); static private final String SEARCH_BY_ID_KEY = "searchById";
Element movieDb; 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 @Override
public Document process(final Document input) { public Document process(final Document input) {
final Element html = new Element("html"); final Element html = new Element("html");
final Document htmlDoc = new Document(html); final Document htmlDoc = new Document(html);
final Element body = new Element("body"); final Element body = new Element("body");
html.addContent(body); html.addContent(body);
final Element dl = new Element("dl"); final Element dl = new Element("dl");
body.addContent(new Element("h1") body.addContent(new Element("h1")
.addContent("List of actors among with their corresponding films")) .addContent("List of actors among with their corresponding films"))
.addContent(dl); .addContent(dl);
movieDb = input.getRootElement(); input.getRootElement().getChild("persons").getChildren().forEach(
movieDb.getChild("persons").getChildren().stream().forEach(
p -> addFilmsToActor(dl, p) p -> addFilmsToActor(dl, p)
); );
return htmlDoc; return htmlDoc;
} }
void addFilmsToActor(final Element dl, final Element person) { private void addFilmsToActor(final Element dl, final Element person) {
final String movies2actorId = "movie[cast/actor[@ref='" searchMovieByActorId.setVariable(SEARCH_BY_ID_KEY, person.getAttributeValue("id"));
+ person.getAttributeValue("id") +"']]";
final List<Element> movies = searchMovieByActorId.evaluate(person);
final XPathExpression<Element> xpath =
XPathFactory.instance().compile(movies2actorId, Filters.element()); if (0 < movies.size()) {
final List<Element> titles = xpath.evaluate(movieDb.getChild("movies"));
if (0 < titles.size()) {
final Element dt = new Element("dt"), final Element dt = new Element("dt"),
dd = new Element("dd"); dd = new Element("dd");
dl.addContent(dt).addContent(dd); dl.addContent(dt).addContent(dd);
dt.addContent(person.getChild("name").getText()); dt.addContent(person.getChild("name").getText());
titles.stream() movies.stream()
.map(t -> t.getChild("title").getText()) .map(t -> t.getChild("title").getText())
.forEach(s -> { .forEach(s -> dd.addContent(new Element("p").addContent(s)));
dd.addContent(new Element("p").addContent(s));
});
} }
} }
}
} \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment