mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
Use android compatible method for loading and saving card preferences
This commit is contained in:
@@ -51,11 +51,5 @@
|
|||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
<version>2.3.20</version>
|
<version>2.3.20</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>xmlpull</groupId>
|
|
||||||
<artifactId>xmlpull</artifactId>
|
|
||||||
<version>1.1.3.1</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,28 +1,25 @@
|
|||||||
package forge.card;
|
package forge.card;
|
||||||
|
|
||||||
import javax.xml.stream.*;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
|
|
||||||
import forge.item.IPaperCard;
|
import forge.item.IPaperCard;
|
||||||
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 cards
|
* Preferences associated with individual cards
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CardPreferences {
|
public class CardPreferences {
|
||||||
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, CardPreferences> allPrefs = new HashMap<String, CardPreferences>();
|
private static Map<String, CardPreferences> allPrefs = new HashMap<String, CardPreferences>();
|
||||||
|
|
||||||
public static CardPreferences getPrefs(IPaperCard card) {
|
public static CardPreferences getPrefs(IPaperCard card) {
|
||||||
@@ -39,77 +36,43 @@ public class CardPreferences {
|
|||||||
allPrefs.clear();
|
allPrefs.clear();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final XMLInputFactory in = XMLInputFactory.newInstance();
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
final XMLEventReader reader = in.createXMLEventReader(new FileInputStream(ForgeConstants.CARD_PREFS_FILE));
|
final Document document = builder.parse(ForgeConstants.CARD_PREFS_FILE);
|
||||||
|
final NodeList cards = document.getElementsByTagName("card");
|
||||||
XMLEvent event;
|
for (int i = 0; i < cards.getLength(); i++) {
|
||||||
StartElement element;
|
final CardPreferences prefs = new CardPreferences();
|
||||||
Iterator<?> attributes;
|
final Element el = (Element)cards.item(i);
|
||||||
Attribute attribute;
|
allPrefs.put(el.getAttribute("name"), prefs);
|
||||||
String tagname;
|
prefs.starCount = Integer.parseInt(el.getAttribute("stars"));
|
||||||
CardPreferences prefs;
|
|
||||||
|
|
||||||
while (reader.hasNext()) {
|
|
||||||
event = reader.nextEvent();
|
|
||||||
|
|
||||||
if (event.isStartElement()) {
|
|
||||||
element = event.asStartElement();
|
|
||||||
tagname = element.getName().getLocalPart();
|
|
||||||
|
|
||||||
if (tagname.equals("card")) {
|
|
||||||
prefs = new CardPreferences();
|
|
||||||
attributes = element.getAttributes();
|
|
||||||
|
|
||||||
while (attributes.hasNext()) {
|
|
||||||
attribute = (Attribute) attributes.next();
|
|
||||||
switch (attribute.getName().toString()) {
|
|
||||||
case "name":
|
|
||||||
allPrefs.put(attribute.getValue(), prefs);
|
|
||||||
break;
|
|
||||||
case "stars":
|
|
||||||
prefs.starCount = Integer.parseInt(attribute.getValue());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save() {
|
public static void save() {
|
||||||
try {
|
try {
|
||||||
final XMLOutputFactory out = XMLOutputFactory.newInstance();
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
final XMLEventWriter writer = out.createXMLEventWriter(new FileOutputStream(ForgeConstants.CARD_PREFS_FILE));
|
Document document = builder.newDocument();
|
||||||
|
Element root = document.createElement("preferences");
|
||||||
writer.add(EVENT_FACTORY.createStartDocument());
|
root.setAttribute("type", "cards");
|
||||||
writer.add(NEWLINE);
|
document.appendChild(root);
|
||||||
writer.add(EVENT_FACTORY.createStartElement("", "", "preferences"));
|
|
||||||
writer.add(EVENT_FACTORY.createAttribute("type", "cards"));
|
|
||||||
writer.add(NEWLINE);
|
|
||||||
|
|
||||||
for (Map.Entry<String, CardPreferences> entry : allPrefs.entrySet()) {
|
for (Map.Entry<String, CardPreferences> entry : allPrefs.entrySet()) {
|
||||||
if (entry.getValue().starCount > 0) {
|
if (entry.getValue().starCount > 0) {
|
||||||
writer.add(TAB);
|
Element card = document.createElement("card");
|
||||||
writer.add(EVENT_FACTORY.createStartElement("", "", "card"));
|
card.setAttribute("name", entry.getKey());
|
||||||
writer.add(EVENT_FACTORY.createAttribute("name", entry.getKey()));
|
card.setAttribute("stars", String.valueOf(entry.getValue().starCount));
|
||||||
writer.add(EVENT_FACTORY.createAttribute("stars", String.valueOf(entry.getValue().starCount)));
|
root.appendChild(card);
|
||||||
writer.add(EVENT_FACTORY.createEndElement("", "", "card"));
|
|
||||||
writer.add(NEWLINE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
XmlUtil.saveDocument(document, ForgeConstants.CARD_PREFS_FILE);
|
||||||
writer.add(EVENT_FACTORY.createEndDocument());
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
catch (final Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,8 @@
|
|||||||
*/
|
*/
|
||||||
package forge.util;
|
package forge.util;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
|
||||||
import org.xmlpull.v1.XmlPullParserFactory;
|
|
||||||
import org.xmlpull.v1.XmlSerializer;
|
|
||||||
|
|
||||||
import javax.xml.transform.OutputKeys;
|
import javax.xml.transform.OutputKeys;
|
||||||
import javax.xml.transform.Transformer;
|
import javax.xml.transform.Transformer;
|
||||||
@@ -30,14 +28,7 @@ import javax.xml.transform.dom.DOMSource;
|
|||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Stack;
|
|
||||||
|
|
||||||
|
|
||||||
public class XmlUtil {
|
public class XmlUtil {
|
||||||
@@ -54,6 +45,7 @@ public class XmlUtil {
|
|||||||
final Transformer t = TransformerFactory.newInstance().newTransformer();
|
final 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", "2");
|
||||||
t.transform(new DOMSource(node), new StreamResult(sw));
|
t.transform(new DOMSource(node), new StreamResult(sw));
|
||||||
} catch (final TransformerException te) {
|
} catch (final TransformerException te) {
|
||||||
System.out.println("nodeToString Transformer Exception");
|
System.out.println("nodeToString Transformer Exception");
|
||||||
@@ -61,135 +53,13 @@ public class XmlUtil {
|
|||||||
return sw.toString();
|
return sw.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void read(String filename, XmlTagReader reader) {
|
public static void saveDocument(final Document document, String filename) throws TransformerException {
|
||||||
try {
|
Transformer t = TransformerFactory.newInstance().newTransformer();
|
||||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
|
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
factory.setNamespaceAware(true);
|
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||||
XmlPullParser xml = factory.newPullParser();
|
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||||
xml.setInput(new InputStreamReader(new FileInputStream(filename)));
|
DOMSource source = new DOMSource(document);
|
||||||
|
StreamResult result = new StreamResult(new File(filename));
|
||||||
int eventType = xml.getEventType();
|
t.transform(source, result);
|
||||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
|
||||||
switch (eventType) {
|
|
||||||
case XmlPullParser.START_TAG:
|
|
||||||
reader.tagStart(xml);
|
|
||||||
break;
|
|
||||||
case XmlPullParser.END_TAG:
|
|
||||||
reader.tagEnd(xml);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
eventType = xml.next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (final FileNotFoundException e) {
|
|
||||||
/* ignore if file doesn't exist */
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static abstract class XmlTagReader {
|
|
||||||
public abstract void tagStart(XmlPullParser xml);
|
|
||||||
public void tagEnd(XmlPullParser xml) {
|
|
||||||
//not required to override but can be
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void write(String filename, XmlTagWriter writer) {
|
|
||||||
FileOutputStream outputStream = null;
|
|
||||||
try {
|
|
||||||
outputStream = new FileOutputStream(filename);
|
|
||||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
|
|
||||||
XmlSerializer xml = factory.newSerializer();
|
|
||||||
xml.setOutput(outputStream, null);
|
|
||||||
xml.startDocument(null, true);
|
|
||||||
xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
|
|
||||||
writer.writeTags(new XmlWriter(xml));
|
|
||||||
xml.endDocument();
|
|
||||||
xml.flush();
|
|
||||||
outputStream.close();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
if (outputStream != null) {
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
}
|
|
||||||
catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
new File(filename).delete(); //delete file if exception
|
|
||||||
}
|
|
||||||
catch (Exception e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class XmlWriter {
|
|
||||||
private XmlSerializer xml;
|
|
||||||
private Stack<String> tagnames;
|
|
||||||
|
|
||||||
private XmlWriter(XmlSerializer xml0) {
|
|
||||||
xml = xml0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startTag(String tagname) {
|
|
||||||
startTag(tagname, null);
|
|
||||||
}
|
|
||||||
public void startTag(String tagname, Map<String, String> attrs) {
|
|
||||||
try {
|
|
||||||
xml.startTag("", tagname);
|
|
||||||
if (attrs != null) {
|
|
||||||
for (Entry<String, String> attr : attrs.entrySet()) {
|
|
||||||
xml.attribute("", attr.getKey(), attr.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tagnames.push(tagname);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeTag(String tagname, Map<String, String> attrs) {
|
|
||||||
writeTag(tagname, attrs, null);
|
|
||||||
}
|
|
||||||
public void writeTag(String tagname, String value) {
|
|
||||||
writeTag(tagname, null, value);
|
|
||||||
}
|
|
||||||
public void writeTag(String tagname, Map<String, String> attrs, String value) {
|
|
||||||
try {
|
|
||||||
xml.startTag("", tagname);
|
|
||||||
if (attrs != null) {
|
|
||||||
for (Entry<String, String> attr : attrs.entrySet()) {
|
|
||||||
xml.attribute("", attr.getKey(), attr.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (value != null) {
|
|
||||||
xml.text(value);
|
|
||||||
}
|
|
||||||
xml.endTag("", tagname);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void endTag() {
|
|
||||||
try {
|
|
||||||
xml.endTag("", tagnames.pop());
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static abstract class XmlTagWriter {
|
|
||||||
public abstract void writeTags(XmlWriter xml);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user