Indent and add line break to exported OPML file

This commit is contained in:
Martin Fietz 2015-10-03 21:39:25 +02:00
parent 18e409523b
commit d3f9b2d49f
2 changed files with 28 additions and 6 deletions

View File

@ -13,6 +13,7 @@ public final class OpmlSymbols {
public static final String VERSION = "version"; public static final String VERSION = "version";
public static final String HEAD = "head"; public static final String HEAD = "head";
public static final String TITLE = "title"; public static final String TITLE = "title";
public static final String DATE_CREATED = "dateCreated";
private OpmlSymbols() { private OpmlSymbols() {

View File

@ -2,14 +2,17 @@ package de.danoeh.antennapod.core.opml;
import android.util.Log; import android.util.Log;
import android.util.Xml; import android.util.Xml;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.Date;
import java.util.List; import java.util.List;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.util.DateUtils;
/** Writes OPML documents. */ /** Writes OPML documents. */
public class OpmlWriter { public class OpmlWriter {
private static final String TAG = "OpmlWriter"; private static final String TAG = "OpmlWriter";
@ -27,23 +30,38 @@ public class OpmlWriter {
*/ */
public void writeDocument(List<Feed> feeds, Writer writer) public void writeDocument(List<Feed> feeds, Writer writer)
throws IllegalArgumentException, IllegalStateException, IOException { throws IllegalArgumentException, IllegalStateException, IOException {
if (BuildConfig.DEBUG)
Log.d(TAG, "Starting to write document"); Log.d(TAG, "Starting to write document");
XmlSerializer xs = Xml.newSerializer(); XmlSerializer xs = Xml.newSerializer();
xs.setOutput(writer); xs.setOutput(writer);
xs.startDocument(ENCODING, false); xs.startDocument(ENCODING, false);
xs.text("\n");
xs.startTag(null, OpmlSymbols.OPML); xs.startTag(null, OpmlSymbols.OPML);
xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION); xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION);
xs.text("\n");
xs.text(" ");
xs.startTag(null, OpmlSymbols.HEAD); xs.startTag(null, OpmlSymbols.HEAD);
xs.text("\n");
xs.text(" ");
xs.startTag(null, OpmlSymbols.TITLE); xs.startTag(null, OpmlSymbols.TITLE);
xs.text(OPML_TITLE); xs.text(OPML_TITLE);
xs.endTag(null, OpmlSymbols.TITLE); xs.endTag(null, OpmlSymbols.TITLE);
xs.text("\n");
xs.text(" ");
xs.startTag(null, OpmlSymbols.DATE_CREATED);
xs.text(DateUtils.formatRFC822Date(new Date()));
xs.endTag(null, OpmlSymbols.DATE_CREATED);
xs.text("\n");
xs.text(" ");
xs.endTag(null, OpmlSymbols.HEAD); xs.endTag(null, OpmlSymbols.HEAD);
xs.text("\n");
xs.text(" ");
xs.startTag(null, OpmlSymbols.BODY); xs.startTag(null, OpmlSymbols.BODY);
xs.text("\n");
for (Feed feed : feeds) { for (Feed feed : feeds) {
xs.text(" ");
xs.startTag(null, OpmlSymbols.OUTLINE); xs.startTag(null, OpmlSymbols.OUTLINE);
xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle()); xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle());
xs.attribute(null, OpmlSymbols.TITLE, feed.getTitle()); xs.attribute(null, OpmlSymbols.TITLE, feed.getTitle());
@ -55,11 +73,14 @@ public class OpmlWriter {
xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink()); xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink());
} }
xs.endTag(null, OpmlSymbols.OUTLINE); xs.endTag(null, OpmlSymbols.OUTLINE);
xs.text("\n");
} }
xs.text(" ");
xs.endTag(null, OpmlSymbols.BODY); xs.endTag(null, OpmlSymbols.BODY);
xs.text("\n");
xs.endTag(null, OpmlSymbols.OPML); xs.endTag(null, OpmlSymbols.OPML);
xs.text("\n");
xs.endDocument(); xs.endDocument();
if (BuildConfig.DEBUG)
Log.d(TAG, "Finished writing document"); Log.d(TAG, "Finished writing document");
} }
} }