summaryrefslogtreecommitdiffstats
path: root/src/de/animux/android/andmal/api/anime/AnimeList.java
blob: 78a1e09511cdb4a67506942413ff1f02be70828c (plain) (blame)
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
package de.animux.android.andmal.api.anime;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

import de.animux.android.andmal.api.anime.State;
import de.animux.android.andmal.util.SortedLinkedList;

public class AnimeList extends DefaultHandler {

	private Anime currentAnime;
	private StringBuffer currentValue;

	private HashMap<State,List<Anime>> animes;

	public AnimeList(String user) throws IOException,
			ParserConfigurationException, SAXException,
			FactoryConfigurationError {
		currentAnime = null;
		currentValue = new StringBuffer();
		
		animes = new HashMap<State, List<Anime>>();
		animes.put(State.WATCHING, new SortedLinkedList<Anime>());
		animes.put(State.COMPLETED, new SortedLinkedList<Anime>());
		animes.put(State.ONHOLD, new SortedLinkedList<Anime>());
		animes.put(State.DROPPED, new SortedLinkedList<Anime>());
		animes.put(State.PLANTOWATCH, new SortedLinkedList<Anime>());

		URL url = new URL("http://myanimelist.net/malappinfo.php?status=all&type=anime&u=" + user);
		InputStream stream = url.openStream();

		SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
		sax.parse(stream, this);
	}

	public Anime getCurrentAnime() {
		return currentAnime;
	}

	public StringBuffer getCurrentValue() {
		return currentValue;
	}

	public Map<State, List<Anime>> getAnimes() {
		return animes;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue.append(new String(ch).substring(start, length - start)
				.replaceAll("&amp;", "&"));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (localName.equals("anime")) {
			if (animes.containsKey(State.valueOf(currentAnime.getMyStatus()))) {
				animes.get(State.valueOf(currentAnime.getMyStatus())).add(currentAnime);
			}

		} else if (localName.equals("series_animedb_id")) {
			currentAnime.setId(Integer.valueOf(currentValue.toString()));
		} else if (localName.equals("series_title")) {
			currentAnime.setTitle(currentValue.toString());
		} else if (localName.equals("series_synonyms")) {
			currentAnime.setSynonyms(currentValue.toString());
		} else if (localName.equals("series_type")) {
			currentAnime.setType(Integer.valueOf(currentValue.toString()));
		} else if (localName.equals("series_episodes")) {
			currentAnime.setEpisodes(Integer.valueOf(currentValue.toString()));
		} else if (localName.equals("series_status")) {
			currentAnime.setStatus(Integer.valueOf(currentValue.toString()));
		} else if (localName.equals("series_start")) {
			currentAnime.setStart(currentValue.toString());
		} else if (localName.equals("series_end")) {
			currentAnime.setEnd(currentValue.toString());
		} else if (localName.equals("series_image")) {
			currentAnime.setImage(currentValue.toString());
		} else if (localName.equals("my_id")) {
			currentAnime.setMyId(Integer.valueOf(currentValue.toString()));
		} else if (localName.equals("my_watched_episodes")) {
			currentAnime.setWatchedEpisodes(Integer.valueOf(currentValue
					.toString()));
		} else if (localName.equals("my_status")) {
			currentAnime.setMyStatus(Integer.valueOf(currentValue.toString()));
		}

		currentValue.setLength(0);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentValue.setLength(0);
		if (localName.equals("anime")) {
			currentAnime = new Anime();
		}
	}
}