removed duplication in ZorkReader with static methods

This commit is contained in:
Claudio Maggioni 2022-11-16 17:56:58 +01:00
parent 8e54d0684b
commit 6b3d2d142c

View file

@ -7,6 +7,7 @@ import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File; import java.io.File;
import java.util.List;
public class ZorkReader { public class ZorkReader {
@ -90,59 +91,7 @@ public class ZorkReader {
tempRoom.creature.put(creatureName, creatureName); tempRoom.creature.put(creatureName, creatureName);
} }
NodeList triggers = element.getElementsByTagName("trigger"); readTriggersInObject(element, tempRoom);
for (j = 0; j < triggers.getLength(); j++) {
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element) triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l = 0; l < commands.getLength(); l++) {
Element command = (Element) commands.item(l);
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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))
);
tempTrigger.conditions.add(tempConditionHas);
} else {
NodeList sstatus = condition.getElementsByTagName("status");
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
getString((Element) sstatus.item(0)),
getString((Element) object.item(0))
);
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0) {
tempTrigger.type = getString((Element) ttype.item(0));
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
tempTrigger.action.add(getString(action));
}
tempRoom.trigger.add(tempTrigger);
}
NodeList containers = element.getElementsByTagName("container"); NodeList containers = element.getElementsByTagName("container");
for (j = 0; j < containers.getLength(); j++) { for (j = 0; j < containers.getLength(); j++) {
@ -154,9 +103,9 @@ public class ZorkReader {
NodeList borders = element.getElementsByTagName("border"); NodeList borders = element.getElementsByTagName("border");
for (j = 0; j < borders.getLength(); j++) { for (j = 0; j < borders.getLength(); j++) {
Element border = (Element) borders.item(j); Element border = (Element) borders.item(j);
String borderdirection = getString((Element) border.getElementsByTagName("direction").item(0)); String borderDirection = getString((Element) border.getElementsByTagName("direction").item(0));
String bordername = getString((Element) border.getElementsByTagName("name").item(0)); String borderName = getString((Element) border.getElementsByTagName("name").item(0));
tempRoom.border.put(borderdirection, bordername); 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*/ /*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
@ -202,58 +151,7 @@ public class ZorkReader {
} }
NodeList triggers = element.getElementsByTagName("trigger"); readTriggersInObject(element, tempItem);
for (j = 0; j < triggers.getLength(); j++) {
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element) triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l = 0; l < commands.getLength(); l++) {
Element command = (Element) commands.item(l);
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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)));
tempTrigger.conditions.add(tempConditionHas);
} else {
NodeList sstatus = condition.getElementsByTagName("status");
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
getString((Element) sstatus.item(0)),
getString((Element) object.item(0))
);
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0) {
tempTrigger.type = getString((Element) ttype.item(0));
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
tempTrigger.action.add(getString(action));
}
tempItem.trigger.add(tempTrigger);
}
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/ /* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
data.Items.put(tempItem.name, tempItem); data.Items.put(tempItem.name, tempItem);
@ -296,59 +194,7 @@ public class ZorkReader {
tempCont.item.put(itemName, itemName); tempCont.item.put(itemName, itemName);
} }
NodeList triggers = element.getElementsByTagName("trigger"); readTriggersInObject(element, tempCont);
for (j = 0; j < triggers.getLength(); j++) {
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element) triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l = 0; l < commands.getLength(); l++) {
Element command = (Element) commands.item(l);
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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))
);
tempTrigger.conditions.add(tempConditionHas);
} else {
NodeList sstatus = condition.getElementsByTagName("status");
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
getString((Element) sstatus.item(0)),
getString((Element) object.item(0))
);
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0) {
tempTrigger.type = getString((Element) ttype.item(0));
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
tempTrigger.action.add(getString(action));
}
tempCont.trigger.add(tempTrigger);
}
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/ /* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.Containers.put(tempCont.name, tempCont); data.Containers.put(tempCont.name, tempCont);
@ -383,28 +229,9 @@ public class ZorkReader {
NodeList attacks = element.getElementsByTagName("attack"); NodeList attacks = element.getElementsByTagName("attack");
for (j = 0; j < attacks.getLength(); j++) { for (j = 0; j < attacks.getLength(); j++) {
Element attack = (Element) attacks.item(j); Element attack = (Element) attacks.item(j);
NodeList conditions = attack.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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)));
tempCreature.conditions.add(tempConditionHas);
} else {
NodeList sstatus = condition.getElementsByTagName("status");
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
getString((Element) sstatus.item(0)),
getString((Element) object.item(0))
);
tempCreature.conditions.add(tempConditionStatus);
}
} readConditionsInTrigger(attack, tempCreature.conditions);
NodeList prints = attack.getElementsByTagName("print"); NodeList prints = attack.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) { for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l); Element print = (Element) prints.item(l);
@ -417,58 +244,7 @@ public class ZorkReader {
} }
} }
NodeList triggers = element.getElementsByTagName("trigger"); readTriggersInObject(element, tempCreature);
for (j = 0; j < triggers.getLength(); j++) {
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element) triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l = 0; l < commands.getLength(); l++) {
Element command = (Element) commands.item(l);
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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)));
tempTrigger.conditions.add(tempConditionHas);
} else {
NodeList sstatus = condition.getElementsByTagName("status");
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(
getString((Element) sstatus.item(0)),
getString((Element) object.item(0))
);
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0) {
tempTrigger.type = getString((Element) ttype.item(0));
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
tempTrigger.action.add(getString(action));
}
tempCreature.trigger.add(tempTrigger);
}
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/ /* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
data.Creatures.put(tempCreature.name, tempCreature); data.Creatures.put(tempCreature.name, tempCreature);
@ -487,4 +263,68 @@ public class ZorkReader {
return data; return data;
} }
private static void readTriggersInObject(Element element, ZorkObject tempRoom) {
int j;
int l;
NodeList triggers = element.getElementsByTagName("trigger");
for (j = 0; j < triggers.getLength(); j++) {
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element) triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l = 0; l < commands.getLength(); l++) {
Element command = (Element) commands.item(l);
ZorkCommand tempCommand = new ZorkCommand(getString(command));
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
readConditionsInTrigger(trigger, tempTrigger.conditions);
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0) {
tempTrigger.type = getString((Element) ttype.item(0));
} else {
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l = 0; l < prints.getLength(); l++) {
Element print = (Element) prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l = 0; l < actions.getLength(); l++) {
Element action = (Element) actions.item(l);
tempTrigger.action.add(getString(action));
}
tempRoom.trigger.add(tempTrigger);
}
}
private static void readConditionsInTrigger(Element trigger, List<? super ZorkCondition> conditionsList) {
int l;
NodeList conditions = trigger.getElementsByTagName("condition");
for (l = 0; l < conditions.getLength(); l++) {
Element condition = (Element) conditions.item(l);
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);
}
}
}
} }