summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/syndication/handler/SyndHandler.java
blob: c1f1396bf932bfc55e2aa3f83623018e1d2bb477 (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
package de.danoeh.antennapod.syndication.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.syndication.namespace.Namespace;
import de.danoeh.antennapod.syndication.namespace.SyndElement;
import de.danoeh.antennapod.syndication.namespace.atom.NSAtom;
import de.danoeh.antennapod.syndication.namespace.content.NSContent;
import de.danoeh.antennapod.syndication.namespace.itunes.NSITunes;
import de.danoeh.antennapod.syndication.namespace.rss20.NSRSS20;
import de.danoeh.antennapod.syndication.namespace.simplechapters.NSSimpleChapters;

/** Superclass for all SAX Handlers which process Syndication formats */
public class SyndHandler extends DefaultHandler {
	private static final String TAG = "SyndHandler";
	private static final String DEFAULT_PREFIX = "";
	protected HandlerState state;

	public SyndHandler(Feed feed, TypeGetter.Type type) {
		state = new HandlerState(feed);
		if (type == TypeGetter.Type.RSS20) {
			state.defaultNamespaces.push(new NSRSS20());
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		state.contentBuf = new StringBuffer();
		Namespace handler = getHandlingNamespace(uri, qName);
		if (handler != null) {
			SyndElement element = handler.handleElementStart(localName, state,
					attributes);
			state.tagstack.push(element);

		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (!state.tagstack.empty()) {
			if (state.getTagstack().size() >= 2) {
				if (state.contentBuf != null) {
					String content = new String(ch, start, length);
					state.contentBuf.append(content);
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		Namespace handler = getHandlingNamespace(uri, qName);
		if (handler != null) {
			handler.handleElementEnd(localName, state);
			state.tagstack.pop();

		}
		state.contentBuf = null;

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		if (state.defaultNamespaces.size() > 1 && prefix.equals(DEFAULT_PREFIX)) {
			state.defaultNamespaces.pop();
		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// Find the right namespace
		if (!state.namespaces.containsKey(uri)) {
			if (uri.equals(NSAtom.NSURI)) {
				if (prefix.equals(DEFAULT_PREFIX)) {
					state.defaultNamespaces.push(new NSAtom());
				} else if (prefix.equals(NSAtom.NSTAG)) {
					state.namespaces.put(uri, new NSAtom());
					if (AppConfig.DEBUG)
						Log.d(TAG, "Recognized Atom namespace");
				}
			} else if (uri.equals(NSContent.NSURI)
					&& prefix.equals(NSContent.NSTAG)) {
				state.namespaces.put(uri, new NSContent());
				if (AppConfig.DEBUG)
					Log.d(TAG, "Recognized Content namespace");
			} else if (uri.equals(NSITunes.NSURI)
					&& prefix.equals(NSITunes.NSTAG)) {
				state.namespaces.put(uri, new NSITunes());
				if (AppConfig.DEBUG)
					Log.d(TAG, "Recognized ITunes namespace");
			} else if (uri.equals(NSSimpleChapters.NSURI)
					&& prefix.equals(NSSimpleChapters.NSTAG)) {
				state.namespaces.put(uri, new NSSimpleChapters());
				if (AppConfig.DEBUG)
					Log.d(TAG, "Recognized SimpleChapters namespace");
			}
		}
	}

	private Namespace getHandlingNamespace(String uri, String qName) {
		Namespace handler = state.namespaces.get(uri);
		if (handler == null && !state.defaultNamespaces.empty() && !qName.contains(":")) {
			handler = state.defaultNamespaces.peek();
		}
		return handler;
	}

	public HandlerState getState() {
		return state;
	}

}