Fix issue with saving data.xml file for conquests with a space in the name on Android device

This commit is contained in:
drdev
2016-03-21 05:00:39 +00:00
parent f3cd392eef
commit 56be582db4
4 changed files with 18 additions and 15 deletions

View File

@@ -45,7 +45,7 @@ import java.util.Map.Entry;
import com.google.common.base.Function; import com.google.common.base.Function;
public final class ConquestData { public final class ConquestData {
private static final String XML_FILE = "data.xml"; private static final String XML_FILENAME = "data.xml";
private String name; private String name;
private PaperCard planeswalker; private PaperCard planeswalker;
@@ -55,8 +55,7 @@ public final class ConquestData {
private int aetherShards; private int aetherShards;
private int planeswalkEmblems; private int planeswalkEmblems;
private final File directory; private final File directory, xmlFile;
private final String xmlFilename;
private final ConquestRecord chaosBattleRecord; private final ConquestRecord chaosBattleRecord;
private final Map<String, ConquestPlaneData> planeDataMap = new HashMap<String, ConquestPlaneData>(); private final Map<String, ConquestPlaneData> planeDataMap = new HashMap<String, ConquestPlaneData>();
private final HashSet<PaperCard> unlockedCards = new HashSet<PaperCard>(); private final HashSet<PaperCard> unlockedCards = new HashSet<PaperCard>();
@@ -67,7 +66,7 @@ public final class ConquestData {
public ConquestData(String name0, ConquestPlane startingPlane0, PaperCard startingPlaneswalker0, PaperCard startingCommander0) { public ConquestData(String name0, ConquestPlane startingPlane0, PaperCard startingPlaneswalker0, PaperCard startingCommander0) {
name = name0; name = name0;
directory = new File(ForgeConstants.CONQUEST_SAVE_DIR, name); directory = new File(ForgeConstants.CONQUEST_SAVE_DIR, name);
xmlFilename = directory.getPath() + ForgeConstants.PATH_SEPARATOR + XML_FILE; xmlFile = new File(directory, XML_FILENAME);
aetherShards = FModel.getConquestPreferences().getPrefInt(CQPref.AETHER_START_SHARDS); aetherShards = FModel.getConquestPreferences().getPrefInt(CQPref.AETHER_START_SHARDS);
currentLocation = new ConquestLocation(startingPlane0, 0, 0, 0); currentLocation = new ConquestLocation(startingPlane0, 0, 0, 0);
unlockPlane(startingPlane0); unlockPlane(startingPlane0);
@@ -89,11 +88,11 @@ public final class ConquestData {
public ConquestData(File directory0) { public ConquestData(File directory0) {
name = directory0.getName(); name = directory0.getName();
directory = directory0; directory = directory0;
xmlFilename = directory.getPath() + ForgeConstants.PATH_SEPARATOR + XML_FILE; xmlFile = new File(directory, XML_FILENAME);
ConquestRecord chaosBattleRecord0 = null; ConquestRecord chaosBattleRecord0 = null;
try { try {
XmlReader xml = new XmlReader(xmlFilename); XmlReader xml = new XmlReader(xmlFile);
CardDb cardDb = FModel.getMagicDb().getCommonCards(); CardDb cardDb = FModel.getMagicDb().getCommonCards();
setPlaneswalker(xml.read("planeswalker", cardDb)); setPlaneswalker(xml.read("planeswalker", cardDb));
aetherShards = xml.read("aetherShards", aetherShards); aetherShards = xml.read("aetherShards", aetherShards);
@@ -120,7 +119,7 @@ public final class ConquestData {
FileUtil.ensureDirectoryExists(directory); FileUtil.ensureDirectoryExists(directory);
try { try {
XmlWriter xml = new XmlWriter(xmlFilename, "data"); XmlWriter xml = new XmlWriter(xmlFile, "data");
xml.write("planeswalker", planeswalker); xml.write("planeswalker", planeswalker);
xml.write("aetherShards", aetherShards); xml.write("aetherShards", aetherShards);
xml.write("planeswalkEmblems", planeswalkEmblems); xml.write("planeswalkEmblems", planeswalkEmblems);

View File

@@ -20,9 +20,9 @@ import forge.util.XmlWriter.IXmlWritable;
public class XmlReader { public class XmlReader {
private Element currentElement; private Element currentElement;
public XmlReader(String filename0) throws Exception { public XmlReader(File file0) throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document document = builder.parse(new File(filename0)); final Document document = builder.parse(file0);
currentElement = (Element)document.getFirstChild(); currentElement = (Element)document.getFirstChild();
} }

View File

@@ -47,13 +47,16 @@ public class XmlUtil {
return sw.toString(); return sw.toString();
} }
public static void saveDocument(final Document document, String filename) throws TransformerException { public static void saveDocument(final Document document, final String filename) throws TransformerException {
saveDocument(document, new File(filename));
}
public static void saveDocument(final Document document, final File file) throws TransformerException {
Transformer t = TransformerFactory.newInstance().newTransformer(); Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document); DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(filename)); StreamResult result = new StreamResult(file);
t.transform(source, result); t.transform(source, result);
} }

View File

@@ -1,5 +1,6 @@
package forge.util; package forge.util;
import java.io.File;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@@ -16,17 +17,17 @@ import forge.item.PaperCard;
public class XmlWriter { public class XmlWriter {
private final Document document; private final Document document;
private final String filename; private final File file;
private final Stack<Element> parentElements = new Stack<Element>(); private final Stack<Element> parentElements = new Stack<Element>();
private Element currentElement; private Element currentElement;
public XmlWriter(String filename0, String rootName0) throws Exception { public XmlWriter(File file0, String rootName0) throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.newDocument(); document = builder.newDocument();
currentElement = document.createElement(rootName0); currentElement = document.createElement(rootName0);
document.appendChild(currentElement); document.appendChild(currentElement);
filename = filename0; file = file0;
} }
public void startElement(String name0) { public void startElement(String name0) {
@@ -109,7 +110,7 @@ public class XmlWriter {
} }
public void close() throws Exception { public void close() throws Exception {
XmlUtil.saveDocument(document, filename); XmlUtil.saveDocument(document, file);
} }
public interface IXmlWritable { public interface IXmlWritable {