diff options
Diffstat (limited to 'src/de/animux/android/andmal/api/anime/AnimeList.java')
-rw-r--r-- | src/de/animux/android/andmal/api/anime/AnimeList.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/de/animux/android/andmal/api/anime/AnimeList.java b/src/de/animux/android/andmal/api/anime/AnimeList.java new file mode 100644 index 0000000..78a1e09 --- /dev/null +++ b/src/de/animux/android/andmal/api/anime/AnimeList.java @@ -0,0 +1,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("&", "&")); + } + + @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(); + } + } +} |