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

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

import android.util.Log;

import de.podfetcher.feed.Feed;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
import de.podfetcher.syndication.namespace.atom.NSAtom;
import de.podfetcher.syndication.namespace.rss20.NSRSS20;

/** 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 {
		if (localName.equals("image") || qName.equals("image")) {
			Log.d(TAG, "Found image");
		}
		Namespace handler = getHandlingNamespace(uri);
		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()) {
			SyndElement top = state.tagstack.peek();
			if (top.getNamespace() != null) {
				top.getNamespace().handleCharacters(state, ch, start, length);
			}
		}
	}

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

		}

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO remove Namespace
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// Find the right namespace
		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());
			}
		}
	}

	private Namespace getHandlingNamespace(String uri) {
		Namespace handler = state.namespaces.get(uri);
		if (handler == null && uri.equals(DEFAULT_PREFIX) &&!state.defaultNamespaces.empty()) {
			handler = state.defaultNamespaces.peek();
		}
		return handler;
	}

	public HandlerState getState() {
		return state;
	}

}