parser partial refactor
This commit is contained in:
parent
a782ec5ad7
commit
52d98a4ece
16 changed files with 403 additions and 190 deletions
|
@ -142,7 +142,7 @@ public class Zork {
|
|||
for (String action : tempTrigger.action) {
|
||||
d.dispatch(action);
|
||||
}
|
||||
skip = skip || tempTrigger.hasCommand;
|
||||
skip = skip || tempTrigger.hasCommand();
|
||||
if (tempTrigger.type.equals("single")) {
|
||||
zorkObject.trigger.remove(x);
|
||||
}
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
/* Special Command condition */
|
||||
public class ZorkCommand implements ZorkEvaluatable {
|
||||
public final String command;
|
||||
public class ZorkCommand {
|
||||
public final String commandName;
|
||||
|
||||
public ZorkCommand(String command) {
|
||||
this.command = command;
|
||||
this.commandName = command;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Zork zork) {
|
||||
return command.equals(zork.userInput);
|
||||
public boolean matchesInput(String userInput) {
|
||||
return commandName.equals(userInput);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.github.dtschust.zork;
|
|||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
|
||||
/* Generic condition*/
|
||||
public abstract class ZorkCondition implements ZorkEvaluatable {
|
||||
public abstract class ZorkCondition {
|
||||
public final String object;
|
||||
|
||||
protected ZorkCondition(String object) {
|
||||
|
@ -11,9 +11,4 @@ public abstract class ZorkCondition implements ZorkEvaluatable {
|
|||
}
|
||||
|
||||
public abstract boolean evaluate(ZorkGame game);
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Zork zork) {
|
||||
return this.evaluate(zork.game);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class ZorkConditionHas extends ZorkCondition {
|
|||
else {
|
||||
ZorkContainer containerObject = (ZorkContainer) game.get("container", owner);
|
||||
if (containerObject != null) {
|
||||
return evaluateCondition(containerObject.item.contains(object));
|
||||
return evaluateCondition(containerObject.containsItem(object));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
public interface ZorkEvaluatable {
|
||||
boolean evaluate(Zork zork);
|
||||
}
|
|
@ -1,26 +1,36 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/*Trigger*/
|
||||
public class ZorkTrigger {
|
||||
public final List<ZorkEvaluatable> conditions = new ArrayList<>();
|
||||
public final List<String> print = new ArrayList<>();
|
||||
public final List<String> action = new ArrayList<>();
|
||||
public final String type; /*By default, single*/
|
||||
public boolean hasCommand = false;
|
||||
private final List<ZorkCondition> conditions;
|
||||
private final List<ZorkCommand> commands;
|
||||
public final List<String> print;
|
||||
public final List<String> action;
|
||||
/* By default, "single" */
|
||||
public final String type;
|
||||
|
||||
public ZorkTrigger(String type) {
|
||||
public boolean hasCommand() {
|
||||
return !this.commands.isEmpty();
|
||||
}
|
||||
|
||||
public ZorkTrigger(final String type,
|
||||
final List<ZorkCondition> conditions,
|
||||
final List<ZorkCommand> commands,
|
||||
final List<String> print,
|
||||
final List<String> action) {
|
||||
this.conditions = conditions;
|
||||
this.commands = commands;
|
||||
this.print = print;
|
||||
this.action = action;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public boolean evaluate(Zork zork) {
|
||||
for (ZorkEvaluatable condition : conditions) {
|
||||
if (!condition.evaluate(zork)) {
|
||||
if (!commands.stream().allMatch(c -> c.matchesInput(zork.userInput))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return conditions.stream().allMatch(c -> c.evaluate(zork.game));
|
||||
}
|
||||
}
|
||||
|
|
88
src/main/java/com/github/dtschust/zork/parser/DOMUtils.java
Normal file
88
src/main/java/com/github/dtschust/zork/parser/DOMUtils.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
package com.github.dtschust.zork.parser;
|
||||
|
||||
import org.w3c.dom.CharacterData;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public final class DOMUtils {
|
||||
private DOMUtils() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given a DOM element with one and only one child of text node type, returns the text as a string. If there is no
|
||||
* such node, '?' is returned
|
||||
* Get a string from an element (XML parsing stuff)
|
||||
*
|
||||
* @param e the element
|
||||
* @return the text as string, or '?'
|
||||
*/
|
||||
public static String getInnerText(final Element e) {
|
||||
final Node child = e.getFirstChild();
|
||||
return child instanceof CharacterData ? ((CharacterData) child).getData() : "?";
|
||||
}
|
||||
|
||||
public static String getInnerTextByTagName(final Element element, final String elementName) {
|
||||
return getInnerTextByTagName(element, elementName, Optional.empty());
|
||||
}
|
||||
|
||||
public static String getInnerTextByTagName(final Element element,
|
||||
final String name,
|
||||
final String defaultValue) {
|
||||
return getInnerTextByTagName(element, name, Optional.of(defaultValue));
|
||||
}
|
||||
|
||||
public static String getInnerTextByTagName(final Element element,
|
||||
final String elementName,
|
||||
final Optional<String> defaultValue) {
|
||||
final NodeList field = element.getElementsByTagName(elementName);
|
||||
|
||||
if (field.getLength() == 0) {
|
||||
return defaultValue.orElseThrow(() ->
|
||||
new IllegalArgumentException(elementName + " element count in container is not 1"));
|
||||
}
|
||||
|
||||
final Node first = field.item(0);
|
||||
if (!(first instanceof Element)) {
|
||||
// the contract of getElementsByTagName states that it returns a list of Element objects
|
||||
throw new IllegalStateException("unreachable");
|
||||
}
|
||||
|
||||
return getInnerText((Element) first);
|
||||
}
|
||||
|
||||
// the isInstance check assures the cast to T is safe, otherwise IllegalStateException is thrown
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> Iterable<T> iterable(final NodeList nodeList, Class<T> type) {
|
||||
return () -> new Iterator<>() {
|
||||
private int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index < nodeList.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext())
|
||||
throw new NoSuchElementException();
|
||||
final Node next = nodeList.item(index++);
|
||||
if (next == null || !type.isAssignableFrom(next.getClass())) {
|
||||
throw new IllegalStateException("Element in list is not of type " + type.getCanonicalName());
|
||||
}
|
||||
return (T) next;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static Iterable<Node> iterNodes(final NodeList nodeList) {
|
||||
return iterable(nodeList, Node.class);
|
||||
}
|
||||
|
||||
static Iterable<Element> iterator(final NodeList nodeList) {
|
||||
return iterable(nodeList, Element.class);
|
||||
}
|
||||
}
|
|
@ -1,15 +1,24 @@
|
|||
package com.github.dtschust.zork.parser;
|
||||
|
||||
import com.github.dtschust.zork.*;
|
||||
import com.github.dtschust.zork.types.*;
|
||||
import org.w3c.dom.CharacterData;
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
import com.github.dtschust.zork.parser.builders.Parsers;
|
||||
import com.github.dtschust.zork.parser.dom.Elements;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
import com.github.dtschust.zork.types.ZorkCreature;
|
||||
import com.github.dtschust.zork.types.ZorkItem;
|
||||
import com.github.dtschust.zork.types.ZorkRoom;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ZorkReader {
|
||||
|
||||
|
@ -19,21 +28,6 @@ public class ZorkReader {
|
|||
this.filename = filename;
|
||||
}
|
||||
|
||||
/* Get a string from an element (XML parsing stuff)*/
|
||||
public static String getString(Element e) {
|
||||
Node child = e.getFirstChild();
|
||||
if (child instanceof CharacterData) {
|
||||
CharacterData cd = (CharacterData) child;
|
||||
return cd.getData();
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
private static String getItemOrDefault(Element element, String name, String base) {
|
||||
NodeList field = element.getElementsByTagName(name);
|
||||
return (field.getLength() > 0) ? getString((Element) field.item(0)) : base;
|
||||
}
|
||||
|
||||
private static void addCreature(ZorkGame data, Element element) {
|
||||
final Set<String> vulnerabilities = new HashSet<>();
|
||||
final List<ZorkCondition> conditions = new ArrayList<>();
|
||||
|
@ -42,60 +36,47 @@ public class ZorkReader {
|
|||
|
||||
|
||||
NodeList attacks = element.getElementsByTagName("attack");
|
||||
for (Element attack : iterElements(attacks)) {
|
||||
for (Element attack : DOMUtils.iterator(attacks)) {
|
||||
final List<ZorkCondition> conditionsList = Elements.byTagName(attack, "condition").stream()
|
||||
.map(Parsers.condition::parse)
|
||||
.collect(Collectors.toList());
|
||||
conditions.addAll(conditionsList);
|
||||
|
||||
readConditionsInTrigger(attack, conditions);
|
||||
|
||||
for (Element print : iterElements(attack.getElementsByTagName("print"))) {
|
||||
prints.add(getString(print));
|
||||
for (Element print : DOMUtils.iterator(attack.getElementsByTagName("print"))) {
|
||||
prints.add(DOMUtils.getInnerText(print));
|
||||
}
|
||||
for (Element action : iterElements(attack.getElementsByTagName("action"))) {
|
||||
actions.add(getString(action));
|
||||
for (Element action : DOMUtils.iterator(attack.getElementsByTagName("action"))) {
|
||||
actions.add(DOMUtils.getInnerText(action));
|
||||
}
|
||||
}
|
||||
|
||||
for (Element vuln : iterElements(element.getElementsByTagName("vulnerability"))) {
|
||||
vulnerabilities.add(getString(vuln));
|
||||
for (Element vuln : DOMUtils.iterator(element.getElementsByTagName("vulnerability"))) {
|
||||
vulnerabilities.add(DOMUtils.getInnerText(vuln));
|
||||
}
|
||||
|
||||
/* Get all possible creature attributes */
|
||||
ZorkCreature tempCreature = new ZorkCreature(
|
||||
getItemOrDefault(element, "name", ""),
|
||||
getItemOrDefault(element, "description", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "name", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "description", ""),
|
||||
vulnerabilities,
|
||||
conditions,
|
||||
prints,
|
||||
actions
|
||||
);
|
||||
|
||||
readTriggersInObject(element, tempCreature);
|
||||
tempCreature.updateStatus(getItemOrDefault(element, "status", ""));
|
||||
final List<ZorkTrigger> triggers = Elements.byTagName(element, "trigger").stream()
|
||||
.map(Parsers.trigger::parse)
|
||||
.collect(Collectors.toList());
|
||||
tempCreature.trigger.addAll(triggers);
|
||||
|
||||
tempCreature.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", ""));
|
||||
|
||||
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||
data.addObjectThroughLookup("creature", tempCreature);
|
||||
}
|
||||
|
||||
private static void addContainer(ZorkGame data, Element element) {
|
||||
/*Get all possible container attributes*/
|
||||
ZorkContainer tempCont = new ZorkContainer(
|
||||
getItemOrDefault(element, "name", ""),
|
||||
getItemOrDefault(element, "description", "")
|
||||
);
|
||||
|
||||
readTriggersInObject(element, tempCont);
|
||||
tempCont.updateStatus(getItemOrDefault(element, "status", ""));
|
||||
|
||||
/*Initially assume a closed container*/
|
||||
for (Element accept : iterElements(element.getElementsByTagName("accept"))) {
|
||||
/* If container has an accepts attribute, then it is always open*/
|
||||
tempCont.open();
|
||||
tempCont.accept.add(getString(accept));
|
||||
}
|
||||
|
||||
for (Element item : iterElements(element.getElementsByTagName("item"))) {
|
||||
String itemName = getString(item);
|
||||
tempCont.item.add(itemName);
|
||||
}
|
||||
final ZorkContainer tempCont = Parsers.container.parse(element);
|
||||
|
||||
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||
data.addObjectThroughLookup("container", tempCont);
|
||||
|
@ -107,139 +88,74 @@ public class ZorkReader {
|
|||
|
||||
NodeList turnOn = element.getElementsByTagName("turnon");
|
||||
if (turnOn.getLength() > 0) {
|
||||
for (Element print : iterElements(element.getElementsByTagName("print"))) {
|
||||
prints.add(getString(print));
|
||||
for (Element print : DOMUtils.iterator(element.getElementsByTagName("print"))) {
|
||||
prints.add(DOMUtils.getInnerText(print));
|
||||
}
|
||||
for (Element action : iterElements(element.getElementsByTagName("action"))) {
|
||||
actions.add(getString(action));
|
||||
for (Element action : DOMUtils.iterator(element.getElementsByTagName("action"))) {
|
||||
actions.add(DOMUtils.getInnerText(action));
|
||||
}
|
||||
}
|
||||
|
||||
/* Get all possible item attributes*/
|
||||
ZorkItem tempItem = new ZorkItem(
|
||||
getItemOrDefault(element, "name", ""),
|
||||
getItemOrDefault(element, "description", ""),
|
||||
getItemOrDefault(element, "writing", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "name", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "description", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "writing", ""),
|
||||
prints,
|
||||
actions
|
||||
);
|
||||
|
||||
readTriggersInObject(element, tempItem);
|
||||
final List<ZorkTrigger> triggers = Elements.byTagName(element, "trigger").stream()
|
||||
.map(Parsers.trigger::parse)
|
||||
.collect(Collectors.toList());
|
||||
tempItem.trigger.addAll(triggers);
|
||||
|
||||
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in object lookup */
|
||||
data.addObjectThroughLookup("item", tempItem);
|
||||
tempItem.updateStatus(getItemOrDefault(element, "status", ""));
|
||||
tempItem.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", ""));
|
||||
}
|
||||
|
||||
private static void addRoom(ZorkGame data, Element element) {
|
||||
|
||||
/*Get all possible Room attributes*/
|
||||
ZorkRoom tempRoom = new ZorkRoom(
|
||||
getItemOrDefault(element, "name", ""),
|
||||
getItemOrDefault(element, "description", ""),
|
||||
getItemOrDefault(element, "type", "regular")
|
||||
DOMUtils.getInnerTextByTagName(element, "name", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "description", ""),
|
||||
DOMUtils.getInnerTextByTagName(element, "type", "regular")
|
||||
);
|
||||
|
||||
readTriggersInObject(element, tempRoom);
|
||||
tempRoom.updateStatus(getItemOrDefault(element, "status", ""));
|
||||
final List<ZorkTrigger> triggers = Elements.byTagName(element, "trigger").stream()
|
||||
.map(Parsers.trigger::parse)
|
||||
.collect(Collectors.toList());
|
||||
tempRoom.trigger.addAll(triggers);
|
||||
|
||||
for (Element item : iterElements(element.getElementsByTagName("item"))) {
|
||||
String itemName = getString(item);
|
||||
tempRoom.updateStatus(DOMUtils.getInnerTextByTagName(element, "status", ""));
|
||||
|
||||
for (Element item : DOMUtils.iterator(element.getElementsByTagName("item"))) {
|
||||
String itemName = DOMUtils.getInnerText(item);
|
||||
tempRoom.item.add(itemName);
|
||||
}
|
||||
|
||||
for (Element creature : iterElements(element.getElementsByTagName("creature"))) {
|
||||
String creatureName = getString(creature);
|
||||
for (Element creature : DOMUtils.iterator(element.getElementsByTagName("creature"))) {
|
||||
String creatureName = DOMUtils.getInnerText(creature);
|
||||
tempRoom.creature.add(creatureName);
|
||||
}
|
||||
|
||||
for (Element container : iterElements(element.getElementsByTagName("container"))) {
|
||||
String containerName = getString(container);
|
||||
for (Element container : DOMUtils.iterator(element.getElementsByTagName("container"))) {
|
||||
String containerName = DOMUtils.getInnerText(container);
|
||||
tempRoom.container.add(containerName);
|
||||
}
|
||||
|
||||
for (Element border : iterElements(element.getElementsByTagName("border"))) {
|
||||
String borderDirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
||||
String borderName = getString((Element) border.getElementsByTagName("name").item(0));
|
||||
for (Element border : DOMUtils.iterator(element.getElementsByTagName("border"))) {
|
||||
String borderDirection = DOMUtils.getInnerText((Element) border.getElementsByTagName("direction").item(0));
|
||||
String borderName = DOMUtils.getInnerText((Element) border.getElementsByTagName("name").item(0));
|
||||
tempRoom.border.put(borderDirection, borderName);
|
||||
}
|
||||
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
|
||||
data.addObjectThroughLookup("room", tempRoom);
|
||||
}
|
||||
|
||||
private static void readTriggersInObject(Element element, ZorkObject tempRoom) {
|
||||
for (Element trigger : iterElements(element.getElementsByTagName("trigger"))) {
|
||||
ZorkTrigger tempTrigger = new ZorkTrigger(getItemOrDefault(element, "type", "single"));
|
||||
for (Element command : iterElements(trigger.getElementsByTagName("command"))) {
|
||||
ZorkCommand tempCommand = new ZorkCommand(getString(command));
|
||||
tempTrigger.conditions.add(tempCommand);
|
||||
tempTrigger.hasCommand = true;
|
||||
}
|
||||
|
||||
readConditionsInTrigger(trigger, tempTrigger.conditions);
|
||||
|
||||
for (Element print : iterElements(trigger.getElementsByTagName("print"))) {
|
||||
tempTrigger.print.add(getString(print));
|
||||
}
|
||||
for (Element action : iterElements(trigger.getElementsByTagName("action"))) {
|
||||
tempTrigger.action.add(getString(action));
|
||||
}
|
||||
|
||||
tempRoom.trigger.add(tempTrigger);
|
||||
}
|
||||
}
|
||||
|
||||
private static void readConditionsInTrigger(Element trigger, List<? super ZorkCondition> conditionsList) {
|
||||
for (Element condition : iterElements(trigger.getElementsByTagName("condition"))) {
|
||||
NodeList object = condition.getElementsByTagName("object");
|
||||
NodeList has = condition.getElementsByTagName("has");
|
||||
if (has.getLength() > 0) {
|
||||
NodeList owner = condition.getElementsByTagName("owner");
|
||||
ZorkConditionHas tempConditionHas = new ZorkConditionHas(
|
||||
getString((Element) has.item(0)),
|
||||
getString((Element) object.item(0)),
|
||||
getString((Element) owner.item(0))
|
||||
);
|
||||
conditionsList.add(tempConditionHas);
|
||||
} else {
|
||||
NodeList sstatus = condition.getElementsByTagName("status");
|
||||
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
|
||||
getString((Element) sstatus.item(0)),
|
||||
getString((Element) object.item(0))
|
||||
);
|
||||
conditionsList.add(tempConditionStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> Iterable<T> iterable(final NodeList nodeList) {
|
||||
return () -> new Iterator<>() {
|
||||
private int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index < nodeList.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext())
|
||||
throw new NoSuchElementException();
|
||||
return (T) nodeList.item(index++);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static Iterable<Node> iterNodes(final NodeList nodeList) {
|
||||
return iterable(nodeList);
|
||||
}
|
||||
|
||||
private static Iterable<Element> iterElements(final NodeList nodeList) {
|
||||
return iterable(nodeList);
|
||||
}
|
||||
|
||||
public ZorkGame build() {
|
||||
|
||||
ZorkGame data = new ZorkGame();
|
||||
|
||||
File file = new File(filename);
|
||||
|
@ -259,7 +175,7 @@ public class ZorkReader {
|
|||
Element rootElement = builder.newDocumentBuilder().parse(file).getDocumentElement();
|
||||
|
||||
/* Every single first generation child is a room, container, creature, or item. So load them in*/
|
||||
for (Node node : iterNodes(rootElement.getChildNodes())) {
|
||||
for (Node node : DOMUtils.iterNodes(rootElement.getChildNodes())) {
|
||||
Element element;
|
||||
if (node instanceof Element) {
|
||||
element = (Element) node;
|
||||
|
@ -287,6 +203,7 @@ public class ZorkReader {
|
|||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Invalid XML file, exiting");
|
||||
System.exit(-1);
|
||||
//e.printStackTrace();
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.github.dtschust.zork.parser.builders;
|
||||
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
|
||||
/**
|
||||
* Inversion of control for Zork parse strategies
|
||||
*/
|
||||
public final class Parsers {
|
||||
public static final ZorkParseStrategy<ZorkCondition> condition = new ZorkConditionParseStrategy();
|
||||
public static final ZorkParseStrategy<ZorkTrigger> trigger = new ZorkTriggerListParseStrategy(condition);
|
||||
public static final ZorkParseStrategy<ZorkContainer> container = new ZorkContainerParseStrategy(trigger);
|
||||
|
||||
private Parsers() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.github.dtschust.zork.parser.builders;
|
||||
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.ZorkConditionHas;
|
||||
import com.github.dtschust.zork.ZorkConditionStatus;
|
||||
import com.github.dtschust.zork.parser.DOMUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class ZorkConditionParseStrategy implements ZorkParseStrategy<ZorkCondition> {
|
||||
@Override
|
||||
public ZorkCondition parse(final Element conditionElement) {
|
||||
if (conditionElement.getElementsByTagName("has").getLength() > 0) {
|
||||
return new ZorkConditionHas(
|
||||
DOMUtils.getInnerTextByTagName(conditionElement, "has"),
|
||||
DOMUtils.getInnerTextByTagName(conditionElement, "object"),
|
||||
DOMUtils.getInnerTextByTagName(conditionElement, "owner")
|
||||
);
|
||||
} else {
|
||||
return new ZorkConditionStatus(
|
||||
DOMUtils.getInnerTextByTagName(conditionElement, "status"),
|
||||
DOMUtils.getInnerTextByTagName(conditionElement, "object")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
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.ZorkContainer;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ZorkContainerParseStrategy implements ZorkParseStrategy<ZorkContainer> {
|
||||
private final ZorkParseStrategy<ZorkTrigger> triggerStrategy;
|
||||
|
||||
public ZorkContainerParseStrategy(ZorkParseStrategy<ZorkTrigger> triggerStrategy) {
|
||||
this.triggerStrategy = triggerStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZorkContainer parse(final Element element) {
|
||||
final String name = DOMUtils.getInnerTextByTagName(element, "name", "");
|
||||
|
||||
final String description = DOMUtils.getInnerTextByTagName(element, "description", "");
|
||||
|
||||
final List<ZorkTrigger> triggers = Elements.byTagName(element, "trigger").stream()
|
||||
.map(triggerStrategy::parse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final String status = DOMUtils.getInnerTextByTagName(element, "status", "");
|
||||
|
||||
final List<String> accepts = Elements.innerTextByTagName(element, "accept");
|
||||
|
||||
// If a container has an accepts attribute, then it is always open
|
||||
final boolean open = !accepts.isEmpty();
|
||||
|
||||
final List<String> items = Elements.innerTextByTagName(element, "item");
|
||||
|
||||
return new ZorkContainer(name, description, open, status, items, accepts, triggers);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.github.dtschust.zork.parser.builders;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public interface ZorkParseStrategy<T> {
|
||||
T parse(Element source);
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.github.dtschust.zork.parser.builders;
|
||||
|
||||
import com.github.dtschust.zork.ZorkCommand;
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
import com.github.dtschust.zork.parser.DOMUtils;
|
||||
import com.github.dtschust.zork.parser.dom.Elements;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ZorkTriggerListParseStrategy implements ZorkParseStrategy<ZorkTrigger> {
|
||||
|
||||
private final ZorkParseStrategy<ZorkCondition> conditionStrategy;
|
||||
|
||||
public ZorkTriggerListParseStrategy(final ZorkParseStrategy<ZorkCondition> conditionStrategy) {
|
||||
this.conditionStrategy = conditionStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZorkTrigger parse(final Element trigger) {
|
||||
final String type = DOMUtils.getInnerTextByTagName((Element) trigger.getParentNode(), "type", "single");
|
||||
|
||||
final List<ZorkCommand> commands = Elements.byTagName(trigger, "command").stream()
|
||||
.map(DOMUtils::getInnerText)
|
||||
.map(ZorkCommand::new)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final boolean hasCommand = !commands.isEmpty();
|
||||
|
||||
final List<ZorkCondition> conditions = Elements.byTagName(trigger, "condition").stream()
|
||||
.map(conditionStrategy::parse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final List<String> prints = Elements.innerTextByTagName(trigger, "print");
|
||||
final List<String> actions = Elements.innerTextByTagName(trigger, "action");
|
||||
|
||||
return new ZorkTrigger(type, conditions, commands, prints, actions);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.github.dtschust.zork.parser.dom;
|
||||
|
||||
import com.github.dtschust.zork.parser.DOMUtils;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Elements extends AbstractList<Element> implements RandomAccess {
|
||||
private final NodeList list;
|
||||
|
||||
public static Elements byTagName(final Element parent, final String name) {
|
||||
return new Elements(parent.getElementsByTagName(name));
|
||||
}
|
||||
|
||||
public static List<String> innerTextByTagName(final Element parent, final String name) {
|
||||
return Elements.byTagName(parent, name).stream()
|
||||
.map(DOMUtils::getInnerText)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Elements(final NodeList l) {
|
||||
list = l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Element get(int index) {
|
||||
final Node e = list.item(index);
|
||||
if (!(e instanceof Element)) {
|
||||
// the contract of getElementsByTagName states that it returns a list of Element objects
|
||||
throw new IllegalStateException("unreachable");
|
||||
}
|
||||
return (Element) e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return list.getLength();
|
||||
}
|
||||
}
|
|
@ -1,20 +1,43 @@
|
|||
package com.github.dtschust.zork.types;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Container*/
|
||||
public class ZorkContainer extends ZorkObject {
|
||||
public final Set<String> item = new HashSet<>();
|
||||
public final List<String> accept = new ArrayList<>();
|
||||
protected boolean open = false;
|
||||
public final Set<String> item;
|
||||
private final List<String> accepts;
|
||||
private boolean open;
|
||||
|
||||
public ZorkContainer(String name, String description) {
|
||||
super(name, description);
|
||||
public ZorkContainer(final String name,
|
||||
final String description,
|
||||
final boolean open,
|
||||
final String status,
|
||||
final Collection<String> items,
|
||||
final Collection<String> accepts,
|
||||
final Collection<ZorkTrigger> triggers) {
|
||||
super(name, description, status, triggers);
|
||||
this.open = open;
|
||||
this.item = new HashSet<>(items);
|
||||
this.accepts = new ArrayList<>(accepts);
|
||||
}
|
||||
|
||||
public void addItem(final String item) {
|
||||
this.item.add(item);
|
||||
}
|
||||
|
||||
public void removeItem(final String item) {
|
||||
this.item.remove(item);
|
||||
}
|
||||
|
||||
public boolean containsItem(final String item) {
|
||||
return this.item.contains(item);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.item.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return open;
|
||||
|
@ -23,5 +46,4 @@ public class ZorkContainer extends ZorkObject {
|
|||
public void open() {
|
||||
open = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,20 +3,32 @@ package com.github.dtschust.zork.types;
|
|||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/* Generic object, everything inherits from this*/
|
||||
public abstract class ZorkObject {
|
||||
public final String name;
|
||||
public final String description;
|
||||
public final List<ZorkTrigger> trigger = new ArrayList<>();
|
||||
public final List<ZorkTrigger> trigger;
|
||||
private String status;
|
||||
|
||||
protected ZorkObject(String name, String description) {
|
||||
this(name, description, null, Collections.emptyList());
|
||||
}
|
||||
|
||||
protected ZorkObject(final String name,
|
||||
final String description,
|
||||
final String status,
|
||||
final Collection<ZorkTrigger> triggers) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.status = status;
|
||||
this.trigger = new ArrayList<>(triggers);
|
||||
}
|
||||
|
||||
|
||||
public void updateStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
|
Reference in a new issue