package com.github.dtschust.zork.parser; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.objects.*; import java.util.Map; import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; public abstract class ZorkParser { private final Map> strategies; public ZorkParser(final PropertyParseStrategy creatureStrategy, final PropertyParseStrategy containerStrategy, final PropertyParseStrategy itemStrategy, final PropertyParseStrategy roomStrategy) { this.strategies = Map.ofEntries( Map.entry(CREATURE, creatureStrategy), Map.entry(CONTAINER, containerStrategy), Map.entry(ITEM, itemStrategy), Map.entry(ROOM, roomStrategy) ); } protected abstract Property getRootProperty(final String filename); public ZorkGame parse(final String filename) { ZorkGame data = new ZorkGame(); final Property rootElement = getRootProperty(filename); // Every single first generation child is a room, container, creature, or item. So load them in for (final Property element : rootElement.subProperties()) { final String name = element.name(); final ZorkObjectTypes t = ZorkObjectTypes.fromPropertyName(name) .orElseThrow(() -> new IllegalStateException("Unexpected value: " + name)); final ZorkObject built = strategies.get(t).parse(element); data.addObjectThroughLookup(t, built); } return data; } }