removed duplication in ZorkReader with static methods
This commit is contained in:
parent
8e54d0684b
commit
6b3d2d142c
1 changed files with 74 additions and 234 deletions
|
@ -7,6 +7,7 @@ import org.w3c.dom.*;
|
|||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class ZorkReader {
|
||||
|
||||
|
@ -90,59 +91,7 @@ public class ZorkReader {
|
|||
tempRoom.creature.put(creatureName, creatureName);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
readTriggersInObject(element, tempRoom);
|
||||
|
||||
NodeList containers = element.getElementsByTagName("container");
|
||||
for (j = 0; j < containers.getLength(); j++) {
|
||||
|
@ -154,9 +103,9 @@ public class ZorkReader {
|
|||
NodeList borders = element.getElementsByTagName("border");
|
||||
for (j = 0; j < borders.getLength(); j++) {
|
||||
Element border = (Element) borders.item(j);
|
||||
String borderdirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
||||
String bordername = getString((Element) border.getElementsByTagName("name").item(0));
|
||||
tempRoom.border.put(borderdirection, bordername);
|
||||
String borderDirection = getString((Element) border.getElementsByTagName("direction").item(0));
|
||||
String borderName = getString((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*/
|
||||
|
@ -202,58 +151,7 @@ public class ZorkReader {
|
|||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
readTriggersInObject(element, tempItem);
|
||||
|
||||
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
|
||||
data.Items.put(tempItem.name, tempItem);
|
||||
|
@ -296,59 +194,7 @@ public class ZorkReader {
|
|||
tempCont.item.put(itemName, itemName);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
readTriggersInObject(element, tempCont);
|
||||
|
||||
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||
data.Containers.put(tempCont.name, tempCont);
|
||||
|
@ -383,28 +229,9 @@ public class ZorkReader {
|
|||
NodeList attacks = element.getElementsByTagName("attack");
|
||||
for (j = 0; j < attacks.getLength(); 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");
|
||||
for (l = 0; l < prints.getLength(); l++) {
|
||||
Element print = (Element) prints.item(l);
|
||||
|
@ -417,58 +244,7 @@ public class ZorkReader {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
readTriggersInObject(element, tempCreature);
|
||||
|
||||
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
|
||||
data.Creatures.put(tempCreature.name, tempCreature);
|
||||
|
@ -487,4 +263,68 @@ public class ZorkReader {
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue