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>
|
||||
<version>2.3.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xmlpull</groupId>
|
||||
<artifactId>xmlpull</artifactId>
|
||||
<version>1.1.3.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
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.properties.ForgeConstants;
|
||||
import forge.util.XmlUtil;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
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
|
||||
*
|
||||
*/
|
||||
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>();
|
||||
|
||||
public static CardPreferences getPrefs(IPaperCard card) {
|
||||
@@ -39,77 +36,43 @@ public class CardPreferences {
|
||||
allPrefs.clear();
|
||||
|
||||
try {
|
||||
final XMLInputFactory in = XMLInputFactory.newInstance();
|
||||
final XMLEventReader reader = in.createXMLEventReader(new FileInputStream(ForgeConstants.CARD_PREFS_FILE));
|
||||
|
||||
XMLEvent event;
|
||||
StartElement element;
|
||||
Iterator<?> attributes;
|
||||
Attribute attribute;
|
||||
String tagname;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
final Document document = builder.parse(ForgeConstants.CARD_PREFS_FILE);
|
||||
final NodeList cards = document.getElementsByTagName("card");
|
||||
for (int i = 0; i < cards.getLength(); i++) {
|
||||
final CardPreferences prefs = new CardPreferences();
|
||||
final Element el = (Element)cards.item(i);
|
||||
allPrefs.put(el.getAttribute("name"), prefs);
|
||||
prefs.starCount = Integer.parseInt(el.getAttribute("stars"));
|
||||
}
|
||||
}
|
||||
catch (final FileNotFoundException e) {
|
||||
/* ignore; it's ok if this file doesn't exist */
|
||||
catch (FileNotFoundException e) {
|
||||
//ok if file not found
|
||||
}
|
||||
catch (final Exception e) {
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
try {
|
||||
final XMLOutputFactory out = XMLOutputFactory.newInstance();
|
||||
final XMLEventWriter writer = out.createXMLEventWriter(new FileOutputStream(ForgeConstants.CARD_PREFS_FILE));
|
||||
|
||||
writer.add(EVENT_FACTORY.createStartDocument());
|
||||
writer.add(NEWLINE);
|
||||
writer.add(EVENT_FACTORY.createStartElement("", "", "preferences"));
|
||||
writer.add(EVENT_FACTORY.createAttribute("type", "cards"));
|
||||
writer.add(NEWLINE);
|
||||
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element root = document.createElement("preferences");
|
||||
root.setAttribute("type", "cards");
|
||||
document.appendChild(root);
|
||||
|
||||
for (Map.Entry<String, CardPreferences> entry : allPrefs.entrySet()) {
|
||||
if (entry.getValue().starCount > 0) {
|
||||
writer.add(TAB);
|
||||
writer.add(EVENT_FACTORY.createStartElement("", "", "card"));
|
||||
writer.add(EVENT_FACTORY.createAttribute("name", entry.getKey()));
|
||||
writer.add(EVENT_FACTORY.createAttribute("stars", String.valueOf(entry.getValue().starCount)));
|
||||
writer.add(EVENT_FACTORY.createEndElement("", "", "card"));
|
||||
writer.add(NEWLINE);
|
||||
Element card = document.createElement("card");
|
||||
card.setAttribute("name", entry.getKey());
|
||||
card.setAttribute("stars", String.valueOf(entry.getValue().starCount));
|
||||
root.appendChild(card);
|
||||
}
|
||||
}
|
||||
|
||||
writer.add(EVENT_FACTORY.createEndDocument());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
XmlUtil.saveDocument(document, ForgeConstants.CARD_PREFS_FILE);
|
||||
}
|
||||
catch (final Exception e) {
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,8 @@
|
||||
*/
|
||||
package forge.util;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
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.Transformer;
|
||||
@@ -30,14 +28,7 @@ import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
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.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Stack;
|
||||
|
||||
|
||||
public class XmlUtil {
|
||||
@@ -54,6 +45,7 @@ public class XmlUtil {
|
||||
final Transformer t = TransformerFactory.newInstance().newTransformer();
|
||||
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
t.transform(new DOMSource(node), new StreamResult(sw));
|
||||
} catch (final TransformerException te) {
|
||||
System.out.println("nodeToString Transformer Exception");
|
||||
@@ -61,135 +53,13 @@ public class XmlUtil {
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
public static void read(String filename, XmlTagReader reader) {
|
||||
try {
|
||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
|
||||
factory.setNamespaceAware(true);
|
||||
XmlPullParser xml = factory.newPullParser();
|
||||
xml.setInput(new InputStreamReader(new FileInputStream(filename)));
|
||||
|
||||
int eventType = xml.getEventType();
|
||||
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);
|
||||
public static void saveDocument(final Document document, String filename) throws TransformerException {
|
||||
Transformer t = TransformerFactory.newInstance().newTransformer();
|
||||
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
DOMSource source = new DOMSource(document);
|
||||
StreamResult result = new StreamResult(new File(filename));
|
||||
t.transform(source, result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user