fix manacost.toString typo

questData - XML serialization for CardPool
This commit is contained in:
Maxmtg
2011-09-01 23:57:02 +00:00
parent d3233cfa1c
commit 16be5d01e5
2 changed files with 56 additions and 27 deletions

View File

@@ -46,7 +46,7 @@ public final class CardManaCost implements Comparable<CardManaCost> {
if (shards.isEmpty()) { return Integer.toString(genericCost); } if (shards.isEmpty()) { return Integer.toString(genericCost); }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
boolean isFirst = false; boolean isFirst = true;
if (genericCost > 0) { sb.append(genericCost); isFirst = false; } if (genericCost > 0) { sb.append(genericCost); isFirst = false; }
for (CardManaCostShard s : shards) { for (CardManaCostShard s : shards) {
if (!isFirst) { sb.append(' '); } if (!isFirst) { sb.append(' '); }

View File

@@ -9,11 +9,14 @@ import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext;
import forge.card.CardDb; import forge.card.CardDb;
import forge.card.CardPool;
import forge.card.CardPrinted; import forge.card.CardPrinted;
import forge.error.ErrorViewer; import forge.error.ErrorViewer;
import forge.properties.ForgeProps; import forge.properties.ForgeProps;
import forge.properties.NewConstants; import forge.properties.NewConstants;
import forge.quest.data.item.QuestInventory; import forge.quest.data.item.QuestInventory;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@@ -23,6 +26,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@@ -63,8 +67,8 @@ public class QuestDataIO {
} }
IgnoringXStream xStream = new IgnoringXStream(); IgnoringXStream xStream = new IgnoringXStream();
xStream.registerConverter(new CardReferenceToXml()); xStream.registerConverter(new CardPoolToXml());
xStream.alias("cref", CardPrinted.class); xStream.alias("CardPool", CardPool.class);
data = (QuestData) xStream.fromXML(xml.toString()); data = (QuestData) xStream.fromXML(xml.toString());
if (data.versionNumber != QuestData.CURRENT_VERSION_NUMBER) { if (data.versionNumber != QuestData.CURRENT_VERSION_NUMBER) {
@@ -124,7 +128,6 @@ public class QuestDataIO {
// card names are stored as plain text - need to read them from there // card names are stored as plain text - need to read them from there
break; break;
} }
//mark the QD as the latest version //mark the QD as the latest version
@@ -140,25 +143,24 @@ public class QuestDataIO {
* *
* @param qd a {@link forge.quest.data.QuestData} object. * @param qd a {@link forge.quest.data.QuestData} object.
*/ */
public static void saveData(QuestData qd) { public static void saveData(final QuestData qd) {
try { try {
XStream xStream = new XStream();
xStream.registerConverter(new CardPoolToXml());
xStream.alias("CardPool", CardPool.class);
File f = ForgeProps.getFile(NewConstants.QUEST.XMLDATA); File f = ForgeProps.getFile(NewConstants.QUEST.XMLDATA);
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(f)); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(f));
GZIPOutputStream zout = new GZIPOutputStream(bout); GZIPOutputStream zout = new GZIPOutputStream(bout);
BufferedOutputStream bout_unp = new BufferedOutputStream(new FileOutputStream(f+".xml"));
XStream xStream = new XStream();
xStream.registerConverter(new CardReferenceToXml());
xStream.alias("cref", CardPrinted.class);
xStream.toXML(qd, zout); xStream.toXML(qd, zout);
xStream.toXML(qd, bout_unp);
zout.flush(); zout.flush();
zout.close(); zout.close();
bout_unp.flush(); BufferedOutputStream boutUnp = new BufferedOutputStream(new FileOutputStream(f + ".xml"));
bout_unp.close(); xStream.toXML(qd, boutUnp);
boutUnp.flush();
boutUnp.close();
} catch (Exception ex) { } catch (Exception ex) {
ErrorViewer.showError(ex, "Error saving Quest Data."); ErrorViewer.showError(ex, "Error saving Quest Data.");
throw new RuntimeException(ex); throw new RuntimeException(ex);
@@ -188,30 +190,57 @@ public class QuestDataIO {
} }
} }
private static class CardReferenceToXml implements Converter { private static class CardPoolToXml implements Converter {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public boolean canConvert(Class clasz) { public boolean canConvert(Class clasz) {
return clasz.equals(CardPrinted.class); return clasz.equals(CardPool.class);
} }
private void writeCardRef(CardPrinted cref, HierarchicalStreamWriter writer)
{
writer.addAttribute("c", cref.getName());
writer.addAttribute("s", cref.getSet());
if (cref.isFoil()) { writer.addAttribute("foil", "1"); }
if (cref.getArtIndex() > 0) { writer.addAttribute("i", Integer.toString(cref.getArtIndex())); }
}
@Override @Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
CardPrinted cref = (CardPrinted) source; CardPool pool = (CardPool) source;
writer.addAttribute("s", cref.getSet()); for (Entry<CardPrinted, Integer> e : pool) {
writer.addAttribute("i", Integer.toString(cref.getArtIndex())); writer.startNode("card");
if (cref.isFoil()) { writer.addAttribute("foil", "1"); } writeCardRef(e.getKey(), writer);
writer.addAttribute("n", cref.getName()); writer.addAttribute("n", e.getValue().toString());
writer.endNode();
}
} }
@Override @Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
String name = reader.getAttribute("n"); CardPool result = new CardPool();
while (reader.hasMoreChildren()) {
reader.moveDown();
CardPrinted cp = readCardPrinted(reader);
String sCnt = reader.getAttribute("n");
int cnt = StringUtils.isNumeric(sCnt) ? Integer.parseInt(sCnt) : 1;
result.add(cp, cnt);
reader.moveUp();
}
return result;
}
private CardPrinted readCardPrinted(final HierarchicalStreamReader reader)
{
String name = reader.getAttribute("c");
String set = reader.getAttribute("s"); String set = reader.getAttribute("s");
short index = Short.parseShort(reader.getAttribute("i")); String sIndex = reader.getAttribute("i");
short index = StringUtils.isNumeric(sIndex) ? Short.parseShort(sIndex) : 0;
boolean foil = "1".equals(reader.getAttribute("foil")); boolean foil = "1".equals(reader.getAttribute("foil"));
CardPrinted card = CardDb.instance().getCard(name, set, index); CardPrinted card = CardDb.instance().getCard(name, set, index);
return foil ? CardPrinted.makeFoiled(card) : card; return foil ? CardPrinted.makeFoiled(card) : card;
} }
} }
} }