summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java
blob: 25d0b7c354dc30410bc5dc04dcc4a7a8e95d074f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package de.danoeh.antennapod.syndication.namespace.atom;

import org.xml.sax.Attributes;

import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.syndication.handler.HandlerState;
import de.danoeh.antennapod.syndication.namespace.NSRSS20;
import de.danoeh.antennapod.syndication.namespace.Namespace;
import de.danoeh.antennapod.syndication.namespace.SyndElement;
import de.danoeh.antennapod.syndication.util.SyndDateUtils;
import de.danoeh.antennapod.syndication.util.SyndTypeUtils;

public class NSAtom extends Namespace {
	private static final String TAG = "NSAtom";
	public static final String NSTAG = "atom";
	public static final String NSURI = "http://www.w3.org/2005/Atom";

	private static final String FEED = "feed";
	private static final String ID = "id";
	private static final String TITLE = "title";
	private static final String ENTRY = "entry";
	private static final String LINK = "link";
	private static final String UPDATED = "updated";
	private static final String AUTHOR = "author";
	private static final String CONTENT = "content";
	private static final String IMAGE = "logo";
	private static final String SUBTITLE = "subtitle";
	private static final String PUBLISHED = "published";

	private static final String TEXT_TYPE = "type";
	// Link
	private static final String LINK_HREF = "href";
	private static final String LINK_REL = "rel";
	private static final String LINK_TYPE = "type";
	private static final String LINK_TITLE = "title";
	private static final String LINK_LENGTH = "length";
	// rel-values
	private static final String LINK_REL_ALTERNATE = "alternate";
	private static final String LINK_REL_ENCLOSURE = "enclosure";
	private static final String LINK_REL_PAYMENT = "payment";
	private static final String LINK_REL_RELATED = "related";
	private static final String LINK_REL_SELF = "self";
	// type-values
	private static final String LINK_TYPE_ATOM = "application/atom+xml";
	private static final String LINK_TYPE_HTML = "text/html";
	private static final String LINK_TYPE_RSS = "application/rss+xml";

	/** Regexp to test whether an Element is a Text Element. */
	private static final String isText = TITLE + "|" + CONTENT + "|" + "|"
			+ SUBTITLE;

	public static final String isFeed = FEED + "|" + NSRSS20.CHANNEL;
	public static final String isFeedItem = ENTRY + "|" + NSRSS20.ITEM;

	@Override
	public SyndElement handleElementStart(String localName, HandlerState state,
			Attributes attributes) {
		if (localName.equals(ENTRY)) {
			state.setCurrentItem(new FeedItem());
			state.getItems().add(state.getCurrentItem());
			state.getCurrentItem().setFeed(state.getFeed());
		} else if (localName.matches(isText)) {
			String type = attributes.getValue(TEXT_TYPE);
			return new AtomText(localName, this, type);
		} else if (localName.equals(LINK)) {
			String href = attributes.getValue(LINK_HREF);
			String rel = attributes.getValue(LINK_REL);
			SyndElement parent = state.getTagstack().peek();
			if (parent.getName().matches(isFeedItem)) {
				if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
					state.getCurrentItem().setLink(href);
				} else if (rel.equals(LINK_REL_ENCLOSURE)) {
					String strSize = attributes.getValue(LINK_LENGTH);
					long size = 0;
					if (strSize != null)
						size = Long.parseLong(strSize);
					String type = attributes.getValue(LINK_TYPE);
					if (SyndTypeUtils.enclosureTypeValid(type)
							|| (type = SyndTypeUtils
									.getValidMimeTypeFromUrl(href)) != null) {
						state.getCurrentItem().setMedia(
								new FeedMedia(state.getCurrentItem(), href,
										size, type));
					}
				} else if (rel.equals(LINK_REL_PAYMENT)) {
					state.getCurrentItem().setPaymentLink(href);
				}
			} else if (parent.getName().matches(isFeed)) {
				if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
					String type = attributes.getValue(LINK_TYPE);
					if (type != null && type.equals(LINK_TYPE_HTML)) {
						state.getFeed().setLink(href);
					}
				} else if (rel.equals(LINK_REL_PAYMENT)) {
					state.getFeed().setPaymentLink(href);
				}
			}
		}
		return new SyndElement(localName, this);
	}

	@Override
	public void handleElementEnd(String localName, HandlerState state) {
		if (localName.equals(ENTRY)) {
			state.setCurrentItem(null);
		}

		if (state.getTagstack().size() >= 2) {
			AtomText textElement = null;
			String content;
			if (state.getContentBuf() != null) {
				content = state.getContentBuf().toString();
			} else {
				content = new String();
			}
			SyndElement topElement = state.getTagstack().peek();
			String top = topElement.getName();
			SyndElement secondElement = state.getSecondTag();
			String second = secondElement.getName();

			if (top.matches(isText)) {
				textElement = (AtomText) topElement;
				textElement.setContent(content);
			}

			if (top.equals(ID)) {
				if (second.equals(FEED)) {
					state.getFeed().setFeedIdentifier(content);
				} else if (second.equals(ENTRY)) {
					state.getCurrentItem().setItemIdentifier(content);
				}
			} else if (top.equals(TITLE)) {

				if (second.equals(FEED)) {
					state.getFeed().setTitle(textElement.getProcessedContent());
				} else if (second.equals(ENTRY)) {
					state.getCurrentItem().setTitle(
							textElement.getProcessedContent());
				}
			} else if (top.equals(SUBTITLE)) {
				if (second.equals(FEED)) {
					state.getFeed().setDescription(
							textElement.getProcessedContent());
				}
			} else if (top.equals(CONTENT)) {
				if (second.equals(ENTRY)) {
					state.getCurrentItem().setDescription(
							textElement.getProcessedContent());
				}
			} else if (top.equals(UPDATED)) {
				if (second.equals(ENTRY)
						&& state.getCurrentItem().getPubDate() == null) {
					state.getCurrentItem().setPubDate(
							SyndDateUtils.parseRFC3339Date(content));
				}
			} else if (top.equals(PUBLISHED)) {
				if (second.equals(ENTRY)) {
					state.getCurrentItem().setPubDate(
							SyndDateUtils.parseRFC3339Date(content));
				}
			} else if (top.equals(IMAGE)) {
				state.getFeed().setImage(new FeedImage(content, null));
			}

		}
	}

}