summaryrefslogtreecommitdiffstats
path: root/src/de/animux/android/andmal/api/anime/AnimeList.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/animux/android/andmal/api/anime/AnimeList.java')
-rw-r--r--src/de/animux/android/andmal/api/anime/AnimeList.java141
1 files changed, 57 insertions, 84 deletions
diff --git a/src/de/animux/android/andmal/api/anime/AnimeList.java b/src/de/animux/android/andmal/api/anime/AnimeList.java
index 78a1e09..0fbb4ea 100644
--- a/src/de/animux/android/andmal/api/anime/AnimeList.java
+++ b/src/de/animux/android/andmal/api/anime/AnimeList.java
@@ -1,117 +1,90 @@
package de.animux.android.andmal.api.anime;
-import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.util.Collection;
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.MalList;
+import de.animux.android.andmal.api.MalObject;
+import de.animux.android.andmal.api.MalObjectStore;
+import de.animux.android.andmal.api.MalState;
import de.animux.android.andmal.api.anime.State;
import de.animux.android.andmal.util.SortedLinkedList;
-public class AnimeList extends DefaultHandler {
+public class AnimeList extends MalList implements MalObjectStore<Anime> {
- private Anime currentAnime;
- private StringBuffer currentValue;
+ // cache last instance of this class to restore if a new one is created with
+ // the same username and password
+ private static AnimeList cache;
- private HashMap<State,List<Anime>> animes;
+ private Map<MalState, Collection<MalObject>> 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 AnimeList(String username, String password) {
+ super(username, password);
- public Anime getCurrentAnime() {
- return currentAnime;
+ if (((cache != null) && cache.getUsername().equals(username))
+ && cache.getPassword().equals(username)) {
+ animes = cache.getObjects();
+ } else {
+ cache = this;
+ needsRefresh = true;
+ }
}
- public StringBuffer getCurrentValue() {
- return currentValue;
- }
+ public AnimeList() {
+ super();
- public Map<State, List<Anime>> getAnimes() {
- return animes;
+ if (cache != null && cache.getUsername() != null
+ && cache.getUsername().equals(this.getUsername())) {
+ animes = cache.getObjects();
+ }
+ else {
+ cache = this;
+ needsRefresh = true;
+ }
}
@Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- currentValue.append(new String(ch).substring(start, length - start)
- .replaceAll("&amp;", "&"));
+ public void refresh() {
+ if (this.getUsername() == null)
+ return;
+
+ animes = new HashMap<MalState, Collection<MalObject>>();
+ animes.put(State.WATCHING, new SortedLinkedList<MalObject>());
+ animes.put(State.COMPLETED, new SortedLinkedList<MalObject>());
+ animes.put(State.ONHOLD, new SortedLinkedList<MalObject>());
+ animes.put(State.DROPPED, new SortedLinkedList<MalObject>());
+ animes.put(State.PLANTOWATCH, new SortedLinkedList<MalObject>());
+
+ try {
+ URL url = new URL(
+ "http://myanimelist.net/malappinfo.php?status=all&type=anime&u="
+ + this.getUsername());
+ InputStream stream = url.openStream();
+
+ SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
+ sax.parse(stream, new AnimeListParser(this));
+ needsRefresh = false;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
@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()));
+ public void add(Anime object) {
+ if (animes.containsKey(State.valueOf(object.getMyStatus()))) {
+ animes.get(State.valueOf(object.getMyStatus())).add(object);
}
-
- 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();
- }
+ public Map<MalState, Collection<MalObject>> getObjects() {
+ return animes;
}
}