package org.freerealm.xmlwrapper;
import java.util.Iterator;
import org.freerealm.xmlwrapper.player.PlayerManagerXMLWrapper;
import org.freerealm.Realm;
import org.freerealm.settlement.Settlement;
import org.freerealm.settlement.improvement.SettlementImprovementManager;
import org.freerealm.diplomacy.Diplomacy;
import org.freerealm.history.History;
import org.freerealm.map.WorldMap;
import org.freerealm.nation.NationManager;
import org.freerealm.player.Player;
import org.freerealm.player.PlayerManager;
import org.freerealm.resource.ResourceManager;
import org.freerealm.resource.bonus.BonusResourceManager;
import org.freerealm.tile.TileTypeManager;
import org.freerealm.tile.improvement.TileImprovementTypeManager;
import org.freerealm.unit.Unit;
import org.freerealm.unit.UnitTypeManager;
import org.freerealm.vegetation.VegetationManager;
import org.w3c.dom.Node;
import org.freerealm.xmlwrapper.city.CityImprovementManagerXMLWrapper;
import org.freerealm.xmlwrapper.diplomacy.DiplomacyXMLConverter;
import org.freerealm.xmlwrapper.history.FreeRealmHistoryXMLConverter;
import org.freerealm.xmlwrapper.map.BonusResourceManagerXMLConverter;
import org.freerealm.xmlwrapper.map.ResourceManagerXMLWrapper;
import org.freerealm.xmlwrapper.map.TileImprovementManagerXMLConverter;
import org.freerealm.xmlwrapper.map.TileTypeManagerXMLWrapper;
import org.freerealm.xmlwrapper.map.WorldMapXMLWrapper;
import org.freerealm.xmlwrapper.player.NationManagerXMLWrapper;
import org.freerealm.xmlwrapper.unit.UnitTypeManagerXMLWrapper;
import org.freerealm.xmlwrapper.vegetation.VegetationManagerImplXMLConverter;
/**
*
* @author Deniz ARIKAN
*/
public class RealmXMLWrapper implements XMLWrapper {
private Realm realm;
public RealmXMLWrapper(Realm realm) {
this.realm = realm;
}
public String toXML() {
StringBuffer xml = new StringBuffer();
xml.append("<Realm>\n");
xml.append("<numberOfTurns>" + realm.getNumberOfTurns() + "</numberOfTurns>\n");
xml.append("<managedObjectCount>" + realm.getManagedObjectCount() + "</managedObjectCount>\n");
xml.append(XMLConverterUtility.convertPropertiestoXML(realm.getProperties()) + "\n");
xml.append((new ResourceManagerXMLWrapper(realm.getResourceManager())).toXML() + "\n");
xml.append((new BonusResourceManagerXMLConverter()).toXML(realm.getBonusResourceManager()) + "\n");
xml.append((new TileTypeManagerXMLWrapper(realm.getTileTypeManager())).toXML() + "\n");
xml.append((new TileImprovementManagerXMLConverter()).toXML(realm.getTileImprovementTypeManager()) + "\n");
xml.append(new VegetationManagerImplXMLConverter().toXML(realm.getVegetationManager()));
xml.append((new UnitTypeManagerXMLWrapper(realm.getUnitTypeManager())).toXML() + "\n");
xml.append((new CityImprovementManagerXMLWrapper(realm.getSettlementImprovementManager())).toXML() + "\n");
xml.append((new NationManagerXMLWrapper(realm.getNationManager())).toXML() + "\n");
xml.append((new PlayerManagerXMLWrapper(realm.getPlayerManager())).toXML() + "\n");
xml.append((new DiplomacyXMLConverter()).toXML(realm.getDiplomacy()) + "\n");
xml.append((new FreeRealmHistoryXMLConverter()).toXML(realm.getHistory()) + "\n");
xml.append((new WorldMapXMLWrapper(realm.getWorldMap())).toXML());
xml.append("</Realm>\n");
return xml.toString();
}
public void initializeFromNode(Realm realm, Node node) {
this.realm = realm;
Node numberOfTurnsNode = XMLConverterUtility.findNode(node, "numberOfTurns");
int numberOfTurns = Integer.parseInt(numberOfTurnsNode.getFirstChild().getNodeValue());
realm.setNumberOfTurns(numberOfTurns);
Node managedObjectCountNode = XMLConverterUtility.findNode(node, "managedObjectCount");
int managedObjectCount = Integer.parseInt(managedObjectCountNode.getFirstChild().getNodeValue());
realm.setManagedObjectCount(managedObjectCount);
realm.setProperties(XMLConverterUtility.convertNodetoProperties(XMLConverterUtility.findNode(node, "Properties")));
Node resourcesNode = XMLConverterUtility.findNode(node, "Resources");
ResourceManager resourceManager = new ResourceManager();
(new ResourceManagerXMLWrapper(resourceManager)).initializeFromNode(realm, resourcesNode);
realm.setResourceManager(resourceManager);
Node bonusResourcesNode = XMLConverterUtility.findNode(node, "bonusResources");
BonusResourceManager bonusResourceManager = (new BonusResourceManagerXMLConverter()).initializeFromNode(realm, bonusResourcesNode);
realm.setBonusResourceManager(bonusResourceManager);
Node tileTypesNode = XMLConverterUtility.findNode(node, "TileTypes");
TileTypeManager tileTypeManager = new TileTypeManager();
(new TileTypeManagerXMLWrapper(tileTypeManager)).initializeFromNode(realm, tileTypesNode);
realm.setTileTypeManager(tileTypeManager);
Node tileImprovementsNode = XMLConverterUtility.findNode(node, "TileImprovements");
TileImprovementTypeManager tileImprovementManager = (new TileImprovementManagerXMLConverter()).initializeFromNode(realm, tileImprovementsNode);
realm.setTileImprovementTypeManager(tileImprovementManager);
Node vegetationTypesNode = XMLConverterUtility.findNode(node, "VegetationTypes");
VegetationManager vegetationManager = (new VegetationManagerImplXMLConverter()).initializeFromNode(realm, vegetationTypesNode);
realm.setVegetationManager(vegetationManager);
(new TileTypeManagerXMLWrapper(tileTypeManager)).initializeFromNode(realm, tileTypesNode);
realm.setTileTypeManager(tileTypeManager);
Node cityImprovementsNode = XMLConverterUtility.findNode(node, "CityImprovements");
SettlementImprovementManager cityImprovementManager = new SettlementImprovementManager();
(new CityImprovementManagerXMLWrapper(cityImprovementManager)).initializeFromNode(realm, cityImprovementsNode);
realm.setSettlementImprovementManager(cityImprovementManager);
Node unitTypesNode = XMLConverterUtility.findNode(node, "UnitTypes");
UnitTypeManager unitTypeManager = new UnitTypeManager();
(new UnitTypeManagerXMLWrapper(unitTypeManager)).initializeFromNode(realm, unitTypesNode);
realm.setUnitTypeManager(unitTypeManager);
Node nationsNode = XMLConverterUtility.findNode(node, "Nations");
NationManager nationManager = new NationManager();
(new NationManagerXMLWrapper(nationManager)).initializeFromNode(realm, nationsNode);
realm.setNationManager(nationManager);
Node playersNode = XMLConverterUtility.findNode(node, "Players");
PlayerManager playerManager = new PlayerManager();
(new PlayerManagerXMLWrapper(playerManager)).initializeFromNode(realm, playersNode);
realm.setPlayerManager(playerManager);
Node diplomacyNode = XMLConverterUtility.findNode(node, "diplomacy");
Diplomacy diplomacy = (new DiplomacyXMLConverter()).initializeFromNode(realm, diplomacyNode);
realm.setDiplomacy(diplomacy);
Node historyNode = XMLConverterUtility.findNode(node, "history");
History history = (new FreeRealmHistoryXMLConverter()).initializeFromNode(realm, historyNode);
realm.setHistory(history);
Node worldMapNode = XMLConverterUtility.findNode(node, "WorldMap");
WorldMap worldMap = new WorldMap();
(new WorldMapXMLWrapper(worldMap)).initializeFromNode(realm, worldMapNode);
Iterator<Player> playerIterator = realm.getPlayerManager().getPlayersIterator();
while (playerIterator.hasNext()) {
Player player = playerIterator.next();
Iterator<Settlement> settlementsIterator = player.getSettlementsIterator();
while (settlementsIterator.hasNext()) {
Settlement settlement = settlementsIterator.next();
worldMap.addSettlement(settlement);
}
Iterator<Unit> unitsIterator = player.getUnitsIterator();
while (unitsIterator.hasNext()) {
Unit unit = unitsIterator.next();
if (unit.getCoordinate() != null) {
worldMap.addUnit(unit);
}
}
}
realm.setWorldMap(worldMap);
}
}