mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 04:08:01 +00:00
Use android compatible method for loading and saving deck preferences
This commit is contained in:
@@ -1,29 +1,26 @@
|
|||||||
package forge.deck.io;
|
package forge.deck.io;
|
||||||
|
|
||||||
import javax.xml.stream.*;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
|
|
||||||
import forge.deck.DeckProxy;
|
import forge.deck.DeckProxy;
|
||||||
import forge.properties.ForgeConstants;
|
import forge.properties.ForgeConstants;
|
||||||
|
import forge.util.XmlUtil;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preferences associated with individual decks
|
* Preferences associated with individual decks
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class DeckPreferences {
|
public class DeckPreferences {
|
||||||
private static String currentDeck;
|
private static String currentDeck;
|
||||||
private static final XMLEventFactory EVENT_FACTORY = XMLEventFactory.newInstance();
|
|
||||||
private static final XMLEvent NEWLINE = EVENT_FACTORY.createDTD("\n");
|
|
||||||
private static final XMLEvent TAB = EVENT_FACTORY.createDTD("\t");
|
|
||||||
private static Map<String, DeckPreferences> allPrefs = new HashMap<String, DeckPreferences>();
|
private static Map<String, DeckPreferences> allPrefs = new HashMap<String, DeckPreferences>();
|
||||||
|
|
||||||
public static String getCurrentDeck() {
|
public static String getCurrentDeck() {
|
||||||
@@ -50,88 +47,48 @@ public class DeckPreferences {
|
|||||||
allPrefs.clear();
|
allPrefs.clear();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final XMLInputFactory in = XMLInputFactory.newInstance();
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
final XMLEventReader reader = in.createXMLEventReader(new FileInputStream(ForgeConstants.DECK_PREFS_FILE));
|
final Document document = builder.parse(ForgeConstants.DECK_PREFS_FILE);
|
||||||
|
|
||||||
XMLEvent event;
|
final Element root = (Element)document.getElementsByTagName("preferences").item(0);
|
||||||
StartElement element;
|
currentDeck = root.getAttribute("currentDeck");
|
||||||
Iterator<?> attributes;
|
|
||||||
Attribute attribute;
|
|
||||||
String tagname;
|
|
||||||
DeckPreferences prefs;
|
|
||||||
|
|
||||||
while (reader.hasNext()) {
|
final NodeList cards = document.getElementsByTagName("deck");
|
||||||
event = reader.nextEvent();
|
for (int i = 0; i < cards.getLength(); i++) {
|
||||||
|
final DeckPreferences prefs = new DeckPreferences();
|
||||||
if (event.isStartElement()) {
|
final Element el = (Element)cards.item(i);
|
||||||
element = event.asStartElement();
|
allPrefs.put(el.getAttribute("key"), prefs);
|
||||||
tagname = element.getName().getLocalPart();
|
prefs.starCount = Integer.parseInt(el.getAttribute("stars"));
|
||||||
|
|
||||||
if (tagname.equals("deck")) {
|
|
||||||
prefs = new DeckPreferences();
|
|
||||||
attributes = element.getAttributes();
|
|
||||||
|
|
||||||
while (attributes.hasNext()) {
|
|
||||||
attribute = (Attribute) attributes.next();
|
|
||||||
switch (attribute.getName().toString()) {
|
|
||||||
case "key":
|
|
||||||
allPrefs.put(attribute.getValue(), prefs);
|
|
||||||
break;
|
|
||||||
case "stars":
|
|
||||||
prefs.starCount = Integer.parseInt(attribute.getValue());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (tagname.equals("preferences")) {
|
|
||||||
attributes = element.getAttributes();
|
|
||||||
while (attributes.hasNext()) {
|
|
||||||
attribute = (Attribute) attributes.next();
|
|
||||||
switch (attribute.getName().toString()) {
|
|
||||||
case "currentDeck":
|
|
||||||
currentDeck = attribute.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (final FileNotFoundException e) {
|
catch (FileNotFoundException e) {
|
||||||
/* ignore; it's ok if this file doesn't exist */
|
//ok if file not found
|
||||||
}
|
}
|
||||||
catch (final Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void save() {
|
private static void save() {
|
||||||
try {
|
try {
|
||||||
final XMLOutputFactory out = XMLOutputFactory.newInstance();
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
final XMLEventWriter writer = out.createXMLEventWriter(new FileOutputStream(ForgeConstants.DECK_PREFS_FILE));
|
Document document = builder.newDocument();
|
||||||
|
Element root = document.createElement("preferences");
|
||||||
writer.add(EVENT_FACTORY.createStartDocument());
|
root.setAttribute("type", "decks");
|
||||||
writer.add(NEWLINE);
|
root.setAttribute("currentDeck", currentDeck);
|
||||||
writer.add(EVENT_FACTORY.createStartElement("", "", "preferences"));
|
document.appendChild(root);
|
||||||
writer.add(EVENT_FACTORY.createAttribute("type", "decks"));
|
|
||||||
writer.add(EVENT_FACTORY.createAttribute("currentDeck", currentDeck));
|
|
||||||
writer.add(NEWLINE);
|
|
||||||
|
|
||||||
for (Map.Entry<String, DeckPreferences> entry : allPrefs.entrySet()) {
|
for (Map.Entry<String, DeckPreferences> entry : allPrefs.entrySet()) {
|
||||||
if (entry.getValue().starCount > 0) {
|
if (entry.getValue().starCount > 0) {
|
||||||
writer.add(TAB);
|
Element deck = document.createElement("deck");
|
||||||
writer.add(EVENT_FACTORY.createStartElement("", "", "deck"));
|
deck.setAttribute("key", entry.getKey());
|
||||||
writer.add(EVENT_FACTORY.createAttribute("key", entry.getKey()));
|
deck.setAttribute("stars", String.valueOf(entry.getValue().starCount));
|
||||||
writer.add(EVENT_FACTORY.createAttribute("stars", String.valueOf(entry.getValue().starCount)));
|
root.appendChild(deck);
|
||||||
writer.add(EVENT_FACTORY.createEndElement("", "", "deck"));
|
|
||||||
writer.add(NEWLINE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
XmlUtil.saveDocument(document, ForgeConstants.DECK_PREFS_FILE);
|
||||||
writer.add(EVENT_FACTORY.createEndDocument());
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
catch (final Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user