summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/opml/OpmlWriter.java
blob: 9ad7def05877c7a88933059f1980169e595c801e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package de.danoeh.antennapod.opml;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.Feed;

/** Writes OPML documents. */
public class OpmlWriter {
	private static final String TAG = "OpmlWriter";
	private static final String ENCODING = "UTF-8";
	private static final String OPML_VERSION = "2.0";
	private static final String OPML_TITLE = "AntennaPod Subscriptions";

	/**
	 * Takes a list of feeds and a writer and writes those into an OPML
	 * document.
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 * @throws IllegalArgumentException
	 */
	public void writeDocument(List<Feed> feeds, Writer writer)
			throws IllegalArgumentException, IllegalStateException, IOException {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Starting to write document");
		XmlSerializer xs = Xml.newSerializer();
		xs.setOutput(writer);

		xs.startDocument(ENCODING, false);
		xs.startTag(null, OpmlSymbols.OPML);
		xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION);

		xs.startTag(null, OpmlSymbols.HEAD);
		xs.startTag(null, OpmlSymbols.TITLE);
		xs.text(OPML_TITLE);
		xs.endTag(null, OpmlSymbols.TITLE);
		xs.endTag(null, OpmlSymbols.HEAD);

		xs.startTag(null, OpmlSymbols.BODY);
		for (Feed feed : feeds) {
			xs.startTag(null, OpmlSymbols.OUTLINE);
			xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle());
			if (feed.getType() != null) {
				xs.attribute(null, OpmlSymbols.TYPE, feed.getType());
			}
			xs.attribute(null, OpmlSymbols.XMLURL, feed.getDownload_url());
			if (feed.getLink() != null) {
				xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink());
			}
			xs.endTag(null, OpmlSymbols.OUTLINE);
		}
		xs.endTag(null, OpmlSymbols.BODY);
		xs.endTag(null, OpmlSymbols.OPML);
		xs.endDocument();
		if (AppConfig.DEBUG)
			Log.d(TAG, "Finished writing document");
	}
}