46 lines
1.7 KiB
Java
46 lines
1.7 KiB
Java
|
package com.github.dtschust.zork.parser.builders;
|
||
|
|
||
|
import com.github.dtschust.zork.ZorkTrigger;
|
||
|
import com.github.dtschust.zork.parser.DOMUtils;
|
||
|
import com.github.dtschust.zork.parser.dom.Elements;
|
||
|
import com.github.dtschust.zork.types.ZorkItem;
|
||
|
import org.w3c.dom.Element;
|
||
|
|
||
|
import java.util.List;
|
||
|
import java.util.stream.Collectors;
|
||
|
|
||
|
public class ZorkItemParseStrategy implements ZorkParseStrategy<ZorkItem> {
|
||
|
|
||
|
private final ZorkParseStrategy<ZorkTrigger> triggerStrategy;
|
||
|
|
||
|
public ZorkItemParseStrategy(final ZorkParseStrategy<ZorkTrigger> triggerStrategy) {
|
||
|
this.triggerStrategy = triggerStrategy;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ZorkItem parse(final Element source) {
|
||
|
final List<String> prints = Elements.byTagName(source, "turnon").stream()
|
||
|
.flatMap(e -> Elements.innerTextByTagName(e, "print").stream())
|
||
|
.collect(Collectors.toList());
|
||
|
|
||
|
final List<String> actions = Elements.byTagName(source, "turnon").stream()
|
||
|
.flatMap(e -> Elements.innerTextByTagName(e, "action").stream())
|
||
|
.collect(Collectors.toList());
|
||
|
|
||
|
final List<ZorkTrigger> triggers = Elements.byTagName(source, "trigger").stream()
|
||
|
.map(triggerStrategy::parse)
|
||
|
.collect(Collectors.toList());
|
||
|
|
||
|
/* Get all possible item attributes*/
|
||
|
return new ZorkItem(
|
||
|
DOMUtils.getInnerTextByTagName(source, "name", ""),
|
||
|
DOMUtils.getInnerTextByTagName(source, "description", ""),
|
||
|
DOMUtils.getInnerTextByTagName(source, "status", ""),
|
||
|
DOMUtils.getInnerTextByTagName(source, "writing", ""),
|
||
|
triggers,
|
||
|
prints,
|
||
|
actions
|
||
|
);
|
||
|
}
|
||
|
}
|