From d429f9113bf475937c3d885ad77b87f25f81fada Mon Sep 17 00:00:00 2001 From: McLoo Date: Tue, 15 Apr 2014 15:19:10 +0200 Subject: [PATCH] Replace XML serializer in plaintext export Fixes #342 - using regex pattern/matcher to escape chars below 0x0020 and above 0xd7ff - using String.Replace to escape XML entities - changed XmlPullParser from Xml.newPullParser() to XmlPullParserFactory parser to fix import on GB --- .../securesms/database/XmlBackup.java | 112 ++++++++++++------ 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/XmlBackup.java b/src/org/thoughtcrime/securesms/database/XmlBackup.java index f75b3a0556..2049d6a86f 100644 --- a/src/org/thoughtcrime/securesms/database/XmlBackup.java +++ b/src/org/thoughtcrime/securesms/database/XmlBackup.java @@ -1,17 +1,17 @@ package org.thoughtcrime.securesms.database; -import android.util.Log; -import android.util.Xml; - +import org.whispersystems.textsecure.util.Util; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlSerializer; +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.util.regex.Matcher; +import java.util.regex.Pattern; public class XmlBackup { @@ -24,11 +24,14 @@ public class XmlBackup { 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 final XmlPullParser parser; public XmlBackup(String path) throws XmlPullParserException, FileNotFoundException { - this.parser = Xml.newPullParser(); + this.parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(new FileInputStream(path), null); } @@ -41,7 +44,7 @@ public class XmlBackup { String name = parser.getName(); - if (!name.equals("sms")) { + if (!name.equalsIgnoreCase("sms")) { continue; } @@ -139,46 +142,81 @@ public class XmlBackup { public static class Writer { - private BufferedWriter writer; - private XmlSerializer serializer; + private static final String XML_HEADER = ""; + 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 { - this.serializer.endTag("", "smses"); - this.serializer.endDocument(); + bufferedWriter.newLine(); + bufferedWriter.write(CLOSE_TAG_SMSES); + bufferedWriter.close(); } + + private String escapeXML(String s) { + if (Util.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(); + } + } }