package org.thoughtcrime.securesms.database; import android.text.TextUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; public class XmlBackup { private static final String PROTOCOL = "protocol"; private static final String ADDRESS = "address"; private static final String CONTACT_NAME = "contact_name"; private static final String DATE = "date"; private static final String READABLE_DATE = "readable_date"; private static final String TYPE = "type"; private static final String SUBJECT = "subject"; private static final String BODY = "body"; private static final String SERVICE_CENTER = "service_center"; private static final String READ = "read"; private static final String STATUS = "status"; private static final String TOA = "toa"; private static final String SC_TOA = "sc_toa"; private static final String LOCKED = "locked"; private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); private final XmlPullParser parser; public XmlBackup(String path) throws XmlPullParserException, FileNotFoundException { this.parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(new FileInputStream(path), null); } public XmlBackupItem getNext() throws IOException, XmlPullParserException { while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); if (!name.equalsIgnoreCase("sms")) { continue; } int attributeCount = parser.getAttributeCount(); if (attributeCount <= 0) { continue; } XmlBackupItem item = new XmlBackupItem(); for (int i=0;i"; private static final String CREATED_BY = ""; private static final String OPEN_TAG_SMSES = ""; private static final String CLOSE_TAG_SMSES = ""; private static final String OPEN_TAG_SMS = " void appendAttribute(StringBuilder stringBuilder, String name, T value) { stringBuilder.append(name).append(OPEN_ATTRIBUTE).append(value).append(CLOSE_ATTRIBUTE); } public void close() throws IOException { bufferedWriter.newLine(); bufferedWriter.write(CLOSE_TAG_SMSES); bufferedWriter.close(); } private String escapeXML(String s) { if (TextUtils.isEmpty(s)) return s; Matcher matcher = PATTERN.matcher( s.replace("&", "&") .replace("<", "<") .replace(">", ">") .replace("\"", """) .replace("'", "'")); StringBuffer st = new StringBuffer(); while (matcher.find()) { String escaped=""; for (char ch: matcher.group(0).toCharArray()) { escaped += ("&#" + ((int) ch) + ";"); } matcher.appendReplacement(st, escaped); } matcher.appendTail(st); return st.toString(); } } }