|
From: <bur...@us...> - 2010-06-12 14:02:21
|
Revision: 6993
http://freecol.svn.sourceforge.net/freecol/?rev=6993&view=rev
Author: burschik
Date: 2010-06-12 14:02:15 +0000 (Sat, 12 Jun 2010)
Log Message:
-----------
Serialize Nation and nation types.
Modified Paths:
--------------
freecol/trunk/src/net/sf/freecol/common/model/EuropeanNationType.java
freecol/trunk/src/net/sf/freecol/common/model/IndianNationType.java
freecol/trunk/src/net/sf/freecol/common/model/Nation.java
freecol/trunk/src/net/sf/freecol/common/model/ResourceType.java
Modified: freecol/trunk/src/net/sf/freecol/common/model/EuropeanNationType.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/common/model/EuropeanNationType.java 2010-06-12 09:15:42 UTC (rev 6992)
+++ freecol/trunk/src/net/sf/freecol/common/model/EuropeanNationType.java 2010-06-12 14:02:15 UTC (rev 6993)
@@ -28,6 +28,7 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
import net.sf.freecol.common.model.Settlement.SettlementType;
import net.sf.freecol.common.model.Unit.Role;
@@ -170,4 +171,54 @@
}
}
+ /**
+ * Makes an XML-representation of this object.
+ *
+ * @param out The output stream.
+ * @throws XMLStreamException if there are any problems writing to the
+ * stream.
+ */
+ protected void toXMLImpl(XMLStreamWriter out) throws XMLStreamException {
+ // Start element:
+ out.writeStartElement(getXMLElementTagName());
+
+ // Add attributes:
+ out.writeAttribute(ID_ATTRIBUTE_TAG, getId());
+ out.writeAttribute("ref", Boolean.toString(ref));
+
+ writeFeatures(out);
+
+ // default map
+ for (Map.Entry<String, AbstractUnit> entry : startingUnitMap.get(null).entrySet()) {
+ writeUnit(out, entry.getKey(), entry.getValue(), false);
+ }
+ // expert map
+ for (Map.Entry<String, AbstractUnit> entry : startingUnitMap.get("true").entrySet()) {
+ writeUnit(out, entry.getKey(), entry.getValue(), true);
+ }
+
+ // End element:
+ out.writeEndElement();
+
+ }
+
+ protected void writeUnit(XMLStreamWriter out, String id, AbstractUnit unit, boolean expert)
+ throws XMLStreamException {
+ out.writeStartElement("unit");
+ out.writeAttribute(ID_ATTRIBUTE_TAG, id);
+ out.writeAttribute("type", unit.getId());
+ out.writeAttribute("role", unit.getRole().toString().toLowerCase());
+ out.writeAttribute("number", String.valueOf(unit.getNumber()));
+ if (expert) {
+ out.writeAttribute("expert-starting-units", "true");
+ }
+ out.writeEndElement();
+ }
+
+
+ public static String getXMLElementTagName() {
+ return "european-nation-type";
+ }
+
+
}
Modified: freecol/trunk/src/net/sf/freecol/common/model/IndianNationType.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/common/model/IndianNationType.java 2010-06-12 09:15:42 UTC (rev 6992)
+++ freecol/trunk/src/net/sf/freecol/common/model/IndianNationType.java 2010-06-12 14:02:15 UTC (rev 6993)
@@ -28,6 +28,7 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
import net.sf.freecol.common.model.Settlement.SettlementType;
import net.sf.freecol.common.util.RandomChoice;
@@ -226,12 +227,12 @@
while (in.nextTag() != XMLStreamConstants.END_ELEMENT) {
String childName = in.getLocalName();
if ("skill".equals(childName)) {
- UnitType unitType = specification.getUnitType(in.getAttributeValue(null, "id"));
+ UnitType unitType = specification.getUnitType(in.getAttributeValue(null, ID_ATTRIBUTE_TAG));
int probability = getAttribute(in, "probability", 0);
skills.add(new RandomChoice<UnitType>(unitType, probability));
in.nextTag(); // close this element
} else if (Region.getXMLElementTagName().equals(childName)) {
- regions.add(in.getAttributeValue(null, "id"));
+ regions.add(in.getAttributeValue(null, ID_ATTRIBUTE_TAG));
in.nextTag(); // close this element
} else {
super.readChild(in, specification);
@@ -248,4 +249,43 @@
}
+ /**
+ * Makes an XML-representation of this object.
+ *
+ * @param out The output stream.
+ * @throws XMLStreamException if there are any problems writing to the
+ * stream.
+ */
+ protected void toXMLImpl(XMLStreamWriter out) throws XMLStreamException {
+ // Start element:
+ out.writeStartElement(getXMLElementTagName());
+
+ // Add attributes:
+ out.writeAttribute(ID_ATTRIBUTE_TAG, getId());
+ out.writeAttribute("number-of-settlements", numberOfSettlements.toString());
+ out.writeAttribute("aggression", aggression.toString());
+ out.writeAttribute("type-of-settlement", getTypeOfSettlement().toString());
+ out.writeAttribute("settlementRadius", Integer.toString(getSettlementRadius()));
+ out.writeAttribute("capitalRadius", Integer.toString(getCapitalRadius()));
+
+ writeFeatures(out);
+
+ for (RandomChoice<UnitType> choice : skills) {
+ out.writeStartElement("skill");
+ out.writeAttribute(ID_ATTRIBUTE_TAG, choice.getObject().getId());
+ out.writeAttribute("probability", Integer.toString(choice.getProbability()));
+ out.writeEndElement();
+ }
+
+ for (String region : regions) {
+ out.writeStartElement(Region.getXMLElementTagName());
+ out.writeAttribute(ID_ATTRIBUTE_TAG, region);
+ out.writeEndElement();
+ }
+
+ // End element:
+ out.writeEndElement();
+
+ }
+
}
Modified: freecol/trunk/src/net/sf/freecol/common/model/Nation.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/common/model/Nation.java 2010-06-12 09:15:42 UTC (rev 6992)
+++ freecol/trunk/src/net/sf/freecol/common/model/Nation.java 2010-06-12 14:02:15 UTC (rev 6993)
@@ -19,10 +19,9 @@
package net.sf.freecol.common.model;
-import java.awt.Color;
-
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
/**
@@ -30,7 +29,7 @@
*/
public class Nation extends FreeColGameObjectType {
- static public String UNKNOWN_NATION_ID = "model.nation.unknownEnemy";
+ public static String UNKNOWN_NATION_ID = "model.nation.unknownEnemy";
private static int nextIndex = 0;
@@ -45,7 +44,8 @@
private boolean selectable;
/**
- * Describe anthem here.
+ * TODO: create audio resource and move all audio resources into
+ * ResourceManager.
*/
private String anthem;
@@ -153,5 +153,34 @@
anthem = in.getAttributeValue(null, "anthem");
}
+ /**
+ * Makes an XML-representation of this object.
+ *
+ * @param out The output stream.
+ * @throws XMLStreamException if there are any problems writing to the
+ * stream.
+ */
+ protected void toXMLImpl(XMLStreamWriter out) throws XMLStreamException {
+ // Start element:
+ out.writeStartElement(getXMLElementTagName());
+ // Add attributes:
+ out.writeAttribute(ID_ATTRIBUTE_TAG, getId());
+ out.writeAttribute("type", type.getId());
+ out.writeAttribute("selectable", Boolean.toString(selectable));
+ out.writeAttribute("anthem", anthem);
+ if (refNation != null) {
+ out.writeAttribute("ref", refNation.getId());
+ }
+
+ // End element:
+ out.writeEndElement();
+
+ }
+
+ public static String getXMLElementTagName() {
+ return "nation";
+ }
+
+
}
Modified: freecol/trunk/src/net/sf/freecol/common/model/ResourceType.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/common/model/ResourceType.java 2010-06-12 09:15:42 UTC (rev 6992)
+++ freecol/trunk/src/net/sf/freecol/common/model/ResourceType.java 2010-06-12 14:02:15 UTC (rev 6993)
@@ -98,6 +98,8 @@
out.writeAttribute("minimum-value", Integer.toString(minValue));
}
+ writeFeatures(out);
+
// End element:
out.writeEndElement();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|