1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-16 22:45:04 -05:00
k-9/src/com/fsck/k9/preferences/StorageImporter.java

154 lines
5.7 KiB
Java
Raw Normal View History

package com.fsck.k9.preferences;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.helper.DateFormatter;
2011-02-26 19:39:06 -05:00
public class StorageImporter {
public static int importPreferences(Context context, String fileName, String encryptionKey) throws StorageImportExportException {
InputStream is = null;
2011-02-26 19:39:06 -05:00
try {
is = new FileInputStream(fileName);
2011-02-26 19:39:06 -05:00
} catch (FileNotFoundException fnfe) {
throw new StorageImportExportException("Failure opening settings file " + fileName, fnfe);
}
try {
int count = importPreferences(context, is, encryptionKey);
return count;
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
Log.i(K9.LOG_TAG, "Unable to close InputStream for file " + fileName + ": " + e.getLocalizedMessage());
}
}
}
}
2011-02-26 19:39:06 -05:00
public static int importPreferences(Context context, InputStream is, String encryptionKey) throws StorageImportExportException {
try {
Preferences preferences = Preferences.getPreferences(context);
SharedPreferences storage = preferences.getPreferences();
SharedPreferences.Editor editor = storage.edit();
2011-02-26 19:39:06 -05:00
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
StorageImporterHandler handler = new StorageImporterHandler();
xr.setContentHandler(handler);
2011-02-26 19:39:06 -05:00
xr.parse(new InputSource(is));
2011-02-26 19:39:06 -05:00
Element dataset = handler.getRootElement();
String version = dataset.attributes.get("version");
Log.i(K9.LOG_TAG, "Got settings file version " + version);
2011-02-26 19:39:06 -05:00
IStorageImporter storageImporter = null;
2011-02-26 19:39:06 -05:00
if ("1".equals(version)) {
storageImporter = new StorageImporterVersion1();
2011-02-26 19:39:06 -05:00
} else {
throw new StorageImportExportException("Unable to read file of version " + version
+ "; (only version 1 is readable)");
}
int numAccounts = 0;
2011-02-26 19:39:06 -05:00
if (storageImporter != null) {
String data = dataset.data.toString();
numAccounts = storageImporter.importPreferences(preferences, editor, data, encryptionKey);
}
editor.commit();
Preferences.getPreferences(context).refreshAccounts();
DateFormatter.clearChosenFormat();
2011-02-26 19:39:06 -05:00
K9.loadPrefs(Preferences.getPreferences(context));
return numAccounts;
2011-02-26 19:39:06 -05:00
} catch (SAXException se) {
throw new StorageImportExportException("Failure reading settings file", se);
2011-02-26 19:39:06 -05:00
} catch (IOException ie) {
throw new StorageImportExportException("Failure reading settings file", ie);
2011-02-26 19:39:06 -05:00
} catch (ParserConfigurationException pce) {
throw new StorageImportExportException("Failure reading settings file", pce);
}
}
2011-02-26 19:39:06 -05:00
private static class Element {
String name;
Map<String, String> attributes = new HashMap<String, String>();
Map<String, Element> subElements = new HashMap<String, Element>();
StringBuilder data = new StringBuilder();
}
2011-02-26 19:39:06 -05:00
private static class StorageImporterHandler extends DefaultHandler {
private Element rootElement = new Element();
private Stack<Element> mOpenTags = new Stack<Element>();
2011-02-26 19:39:06 -05:00
public Element getRootElement() {
return this.rootElement;
}
@Override
2011-02-26 19:39:06 -05:00
public void startDocument() throws SAXException {
}
@Override
2011-02-26 19:39:06 -05:00
public void endDocument() throws SAXException {
/* Do nothing */
}
@Override
public void startElement(String namespaceURI, String localName,
2011-02-26 19:39:06 -05:00
String qName, Attributes attributes) throws SAXException {
Log.i(K9.LOG_TAG, "Starting element " + localName);
Element element = new Element();
element.name = localName;
mOpenTags.push(element);
2011-02-26 19:39:06 -05:00
for (int i = 0; i < attributes.getLength(); i++) {
String key = attributes.getLocalName(i);
String value = attributes.getValue(i);
Log.i(K9.LOG_TAG, "Got attribute " + key + " = " + value);
element.attributes.put(key, value);
}
}
@Override
2011-02-26 19:39:06 -05:00
public void endElement(String namespaceURI, String localName, String qName) {
Log.i(K9.LOG_TAG, "Ending element " + localName);
Element element = mOpenTags.pop();
Element superElement = mOpenTags.empty() ? null : mOpenTags.peek();
2011-02-26 19:39:06 -05:00
if (superElement != null) {
superElement.subElements.put(element.name, element);
2011-02-26 19:39:06 -05:00
} else {
rootElement = element;
}
}
@Override
2011-02-26 19:39:06 -05:00
public void characters(char ch[], int start, int length) {
String value = new String(ch, start, length);
mOpenTags.peek().data.append(value);
}
}
}