* a valid <code>Building</code>.
*/
public static Building decode(ParsedXML node, IGame game) {
String attrStr = null;
int attrVal = 0;
Building retVal = null;
Vector<Coords> coordVec = new Vector<Coords>();
Map<Coords, Boolean> burning = new HashMap<Coords, Boolean>();
Map<Coords, Integer> curCF = new HashMap<Coords, Integer>();
Map<Coords, Integer> phaseCF = new HashMap<Coords, Integer>();
Enumeration<?> subnodes = null;
ParsedXML subnode = null;
Coords coords = null;
int type = -1;
int id = -1;
String name = null;
// Walk the building node's children.
Enumeration<?> children = node.elements();
while (children.hasMoreElements()) {
ParsedXML child = (ParsedXML) children.nextElement();
String childName = child.getName();
// Handle null child names.
if (null == childName) {
// No-op.
}
// Did we find the buildingData node?
else if (childName.equals("buildingData")) {
// There should be only one buildingData node.
if (null != retVal) {
throw new IllegalStateException(
"More than one 'buildingData' node in a building node.");
}
// Read the coords of the building.
subnodes = child.elements();
while (subnodes.hasMoreElements()) {
subnode = (ParsedXML) subnodes.nextElement();
// Have we found the coords subnode?
if (subnode.getName().equals("coords")) {
coords = CoordsEncoder.decode(subnode, game);
if (null != coords) {
coordVec.addElement(coords);
}
// Do we have a 'currentCF' attribute?
attrStr = subnode.getAttribute("currentCF");
if (null != attrStr) {
// Try to pull the currentCF from the attribute string
try {
attrVal = Integer.parseInt(attrStr);
} catch (NumberFormatException nfexp) {
throw new IllegalStateException(
"Couldn't get an integer from " + attrStr);
}
// Do we have a valid value?
if (0 > attrVal) {
throw new IllegalStateException(
"Illegal value for currentCF: " + attrStr);
}
curCF.put(coords, attrVal);
}
// Do we have a 'phaseCF' attribute?
attrStr = subnode.getAttribute("phaseCF");
if (null != attrStr) {
// Try to pull the phaseCF from the attribute string
try {
attrVal = Integer.parseInt(attrStr);
} catch (NumberFormatException nfexp) {
throw new IllegalStateException(
"Couldn't get an integer from " + attrStr);
}
// Do we have a valid value?
if (0 >= attrVal) {
throw new IllegalStateException(
"Illegal value for phaseCF: " + attrStr);
}
phaseCF.put(coords, attrVal);
}
// Set the building's 'isBurning' flag.
burning.put(coords,StringUtil.parseBoolean(child
.getAttribute("isBurning")));
}
} // Check the next subnode
// We *did* find at least one coords for the building, right?
if (0 >= coordVec.size()) {
throw new IllegalStateException(
"Couldn't decode the coords for the building.");
}
// Get the building type.
attrStr = child.getAttribute("type");
if (null == attrStr) {
throw new IllegalStateException(
"Couldn't decode the buildingData for a building node.");
}
// Try to pull the type from the attribute string
try {
attrVal = Integer.parseInt(attrStr);
} catch (NumberFormatException exp) {
throw new IllegalStateException(
"Couldn't get an integer from " + attrStr);
}
type = attrVal;
// Do we have a valid value?
if (type < 0 || type > Building.WALL) {
throw new IllegalStateException("Illegal value for type: "
+ attrStr);
}
// Get the building id.
attrStr = child.getAttribute("id");
if (null == attrStr) {
throw new IllegalStateException(
"Couldn't decode the buildingData for a building node.");
}
// Try to pull the id from the attribute string
try {
attrVal = Integer.parseInt(attrStr);
} catch (NumberFormatException nfexp) {
throw new IllegalStateException(
"Couldn't get an integer from " + attrStr);
}
id = attrVal;
// Do we have a valid value?
if (0 >= id) {
throw new IllegalStateException("Illegal value for id: "
+ attrStr);
}
// Get the building name.
attrStr = child.getAttribute("name");
if (null == attrStr) {
throw new IllegalStateException(
"Couldn't decode the buildingData for a building node.");
}
name = attrStr;
// Try to create the building.
try {
retVal = new Building(type, id, name, coordVec);
} catch (IllegalArgumentException iaexp) {
throw new IllegalStateException(iaexp.getMessage());
}
for (Coords coord : burning.keySet()) {
retVal.setBurning(burning.get(coord), coord);
}
for (Coords coord : curCF.keySet()) {
retVal.setCurrentCF(curCF.get(coord), coord);
}
for (Coords coord : phaseCF.keySet()) {
retVal.setPhaseCF(phaseCF.get(coord), coord);
}
} // End found-"buildingData"-child
} // Look at the next child.