aboutsummaryrefslogtreecommitdiff
path: root/subsonic-android/src/github/daneren2005/subdroid/service/parser
diff options
context:
space:
mode:
Diffstat (limited to 'subsonic-android/src/github/daneren2005/subdroid/service/parser')
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/AbstractParser.java138
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/AlbumListParser.java62
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/ErrorParser.java49
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/IndexesParser.java104
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/JukeboxStatusParser.java62
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/LicenseParser.java62
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/LyricsParser.java65
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryEntryParser.java59
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryParser.java71
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicFoldersParser.java69
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistParser.java62
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistsParser.java67
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/RandomSongsParser.java62
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResult2Parser.java75
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResultParser.java67
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/SubsonicRESTException.java19
-rw-r--r--subsonic-android/src/github/daneren2005/subdroid/service/parser/VersionParser.java47
17 files changed, 1140 insertions, 0 deletions
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/AbstractParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/AbstractParser.java
new file mode 100644
index 00000000..8f136e1c
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/AbstractParser.java
@@ -0,0 +1,138 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import java.io.Reader;
+
+import org.xmlpull.v1.XmlPullParser;
+
+import android.content.Context;
+import android.util.Xml;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.Version;
+import github.daneren2005.subdroid.util.ProgressListener;
+import github.daneren2005.subdroid.util.Util;
+
+/**
+ * @author Sindre Mehus
+ */
+public abstract class AbstractParser {
+
+ private final Context context;
+ private XmlPullParser parser;
+ private boolean rootElementFound;
+
+ public AbstractParser(Context context) {
+ this.context = context;
+ }
+
+ protected Context getContext() {
+ return context;
+ }
+
+ protected void handleError() throws Exception {
+ int code = getInteger("code");
+ String message;
+ switch (code) {
+ case 20:
+ message = context.getResources().getString(R.string.parser_upgrade_client);
+ break;
+ case 30:
+ message = context.getResources().getString(R.string.parser_upgrade_server);
+ break;
+ case 40:
+ message = context.getResources().getString(R.string.parser_not_authenticated);
+ break;
+ case 50:
+ message = context.getResources().getString(R.string.parser_not_authorized);
+ break;
+ default:
+ message = get("message");
+ break;
+ }
+ throw new SubsonicRESTException(code, message);
+ }
+
+ protected void updateProgress(ProgressListener progressListener, int messageId) {
+ if (progressListener != null) {
+ progressListener.updateProgress(messageId);
+ }
+ }
+
+ protected void updateProgress(ProgressListener progressListener, String message) {
+ if (progressListener != null) {
+ progressListener.updateProgress(message);
+ }
+ }
+
+ protected String getText() {
+ return parser.getText();
+ }
+
+ protected String get(String name) {
+ return parser.getAttributeValue(null, name);
+ }
+
+ protected boolean getBoolean(String name) {
+ return "true".equals(get(name));
+ }
+
+ protected Integer getInteger(String name) {
+ String s = get(name);
+ return s == null ? null : Integer.valueOf(s);
+ }
+
+ protected Long getLong(String name) {
+ String s = get(name);
+ return s == null ? null : Long.valueOf(s);
+ }
+
+ protected Float getFloat(String name) {
+ String s = get(name);
+ return s == null ? null : Float.valueOf(s);
+ }
+
+ protected void init(Reader reader) throws Exception {
+ parser = Xml.newPullParser();
+ parser.setInput(reader);
+ rootElementFound = false;
+ }
+
+ protected int nextParseEvent() throws Exception {
+ return parser.next();
+ }
+
+ protected String getElementName() {
+ String name = parser.getName();
+ if ("subsonic-response".equals(name)) {
+ rootElementFound = true;
+ String version = get("version");
+ if (version != null) {
+ Util.setServerRestVersion(context, new Version(version));
+ }
+ }
+ return name;
+ }
+
+ protected void validate() throws Exception {
+ if (!rootElementFound) {
+ throw new Exception(context.getResources().getString(R.string.background_task_parse_error));
+ }
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/AlbumListParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/AlbumListParser.java
new file mode 100644
index 00000000..a1b3a5aa
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/AlbumListParser.java
@@ -0,0 +1,62 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class AlbumListParser extends MusicDirectoryEntryParser {
+
+ public AlbumListParser(Context context) {
+ super(context);
+ }
+
+ public MusicDirectory parse(Reader reader, ProgressListener progressListener) throws Exception {
+
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ MusicDirectory dir = new MusicDirectory();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("album".equals(name)) {
+ dir.addChild(parseEntry());
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return dir;
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/ErrorParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/ErrorParser.java
new file mode 100644
index 00000000..6323eeb6
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/ErrorParser.java
@@ -0,0 +1,49 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class ErrorParser extends AbstractParser {
+
+ public ErrorParser(Context context) {
+ super(context);
+ }
+
+ public void parse(Reader reader) throws Exception {
+
+ init(reader);
+
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG && "error".equals(getElementName())) {
+ handleError();
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/IndexesParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/IndexesParser.java
new file mode 100644
index 00000000..6ffc7a5d
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/IndexesParser.java
@@ -0,0 +1,104 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import java.io.Reader;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.xmlpull.v1.XmlPullParser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.Artist;
+import github.daneren2005.subdroid.domain.Indexes;
+import github.daneren2005.subdroid.util.ProgressListener;
+import android.util.Log;
+
+/**
+ * @author Sindre Mehus
+ */
+public class IndexesParser extends AbstractParser {
+ private static final String TAG = IndexesParser.class.getSimpleName();
+
+ public IndexesParser(Context context) {
+ super(context);
+ }
+
+ public Indexes parse(Reader reader, ProgressListener progressListener) throws Exception {
+
+ long t0 = System.currentTimeMillis();
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ List<Artist> artists = new ArrayList<Artist>();
+ List<Artist> shortcuts = new ArrayList<Artist>();
+ Long lastModified = null;
+ int eventType;
+ String index = "#";
+ boolean changed = false;
+
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("indexes".equals(name)) {
+ changed = true;
+ lastModified = getLong("lastModified");
+ } else if ("index".equals(name)) {
+ index = get("name");
+
+ } else if ("artist".equals(name)) {
+ Artist artist = new Artist();
+ artist.setId(get("id"));
+ artist.setName(get("name"));
+ artist.setIndex(index);
+ artists.add(artist);
+
+ if (artists.size() % 10 == 0) {
+ String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size());
+ updateProgress(progressListener, msg);
+ }
+ } else if ("shortcut".equals(name)) {
+ Artist shortcut = new Artist();
+ shortcut.setId(get("id"));
+ shortcut.setName(get("name"));
+ shortcut.setIndex("*");
+ shortcuts.add(shortcut);
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+
+ if (!changed) {
+ return null;
+ }
+
+ long t1 = System.currentTimeMillis();
+ Log.d(TAG, "Got " + artists.size() + " artist(s) in " + (t1 - t0) + "ms.");
+
+ String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size());
+ updateProgress(progressListener, msg);
+
+ return new Indexes(lastModified == null ? 0L : lastModified, shortcuts, artists);
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/JukeboxStatusParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/JukeboxStatusParser.java
new file mode 100644
index 00000000..2d3a0e88
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/JukeboxStatusParser.java
@@ -0,0 +1,62 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import java.io.Reader;
+
+import org.xmlpull.v1.XmlPullParser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.domain.JukeboxStatus;
+
+/**
+ * @author Sindre Mehus
+ */
+public class JukeboxStatusParser extends AbstractParser {
+
+ public JukeboxStatusParser(Context context) {
+ super(context);
+ }
+
+ public JukeboxStatus parse(Reader reader) throws Exception {
+
+ init(reader);
+
+ JukeboxStatus jukeboxStatus = new JukeboxStatus();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("jukeboxPlaylist".equals(name) || "jukeboxStatus".equals(name)) {
+ jukeboxStatus.setPositionSeconds(getInteger("position"));
+ jukeboxStatus.setCurrentIndex(getInteger("currentIndex"));
+ jukeboxStatus.setPlaying(getBoolean("playing"));
+ jukeboxStatus.setGain(getFloat("gain"));
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+
+ return jukeboxStatus;
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/LicenseParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/LicenseParser.java
new file mode 100644
index 00000000..695c4744
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/LicenseParser.java
@@ -0,0 +1,62 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+import github.daneren2005.subdroid.domain.ServerInfo;
+import github.daneren2005.subdroid.domain.Version;
+
+/**
+ * @author Sindre Mehus
+ */
+public class LicenseParser extends AbstractParser {
+
+ public LicenseParser(Context context) {
+ super(context);
+ }
+
+ public ServerInfo parse(Reader reader) throws Exception {
+
+ init(reader);
+
+ ServerInfo serverInfo = new ServerInfo();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("subsonic-response".equals(name)) {
+ serverInfo.setRestVersion(new Version(get("version")));
+ } else if ("license".equals(name)) {
+ serverInfo.setLicenseValid(getBoolean("valid"));
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+
+ return serverInfo;
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/LyricsParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/LyricsParser.java
new file mode 100644
index 00000000..5fef9ce2
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/LyricsParser.java
@@ -0,0 +1,65 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2010 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.Lyrics;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class LyricsParser extends AbstractParser {
+
+ public LyricsParser(Context context) {
+ super(context);
+ }
+
+ public Lyrics parse(Reader reader, ProgressListener progressListener) throws Exception {
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ Lyrics lyrics = null;
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("lyrics".equals(name)) {
+ lyrics = new Lyrics();
+ lyrics.setArtist(get("artist"));
+ lyrics.setTitle(get("title"));
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ } else if (eventType == XmlPullParser.TEXT) {
+ if (lyrics != null && lyrics.getText() == null) {
+ lyrics.setText(getText());
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ return lyrics;
+ }
+}
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryEntryParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryEntryParser.java
new file mode 100644
index 00000000..01ba1081
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryEntryParser.java
@@ -0,0 +1,59 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+
+/**
+ * @author Sindre Mehus
+ */
+public class MusicDirectoryEntryParser extends AbstractParser {
+
+ public MusicDirectoryEntryParser(Context context) {
+ super(context);
+ }
+
+ protected MusicDirectory.Entry parseEntry() {
+ MusicDirectory.Entry entry = new MusicDirectory.Entry();
+ entry.setId(get("id"));
+ entry.setParent(get("parent"));
+ entry.setTitle(get("title"));
+ entry.setDirectory(getBoolean("isDir"));
+ entry.setCoverArt(get("coverArt"));
+ entry.setArtist(get("artist"));
+
+ if (!entry.isDirectory()) {
+ entry.setAlbum(get("album"));
+ entry.setTrack(getInteger("track"));
+ entry.setYear(getInteger("year"));
+ entry.setGenre(get("genre"));
+ entry.setContentType(get("contentType"));
+ entry.setSuffix(get("suffix"));
+ entry.setTranscodedContentType(get("transcodedContentType"));
+ entry.setTranscodedSuffix(get("transcodedSuffix"));
+ entry.setSize(getLong("size"));
+ entry.setDuration(getInteger("duration"));
+ entry.setBitRate(getInteger("bitRate"));
+ entry.setPath(get("path"));
+ entry.setVideo(getBoolean("isVideo"));
+ }
+ return entry;
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryParser.java
new file mode 100644
index 00000000..d705991c
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicDirectoryParser.java
@@ -0,0 +1,71 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import android.util.Log;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class MusicDirectoryParser extends MusicDirectoryEntryParser {
+
+ private static final String TAG = MusicDirectoryParser.class.getSimpleName();
+
+ public MusicDirectoryParser(Context context) {
+ super(context);
+ }
+
+ public MusicDirectory parse(Reader reader, ProgressListener progressListener) throws Exception {
+
+ long t0 = System.currentTimeMillis();
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ MusicDirectory dir = new MusicDirectory();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("child".equals(name)) {
+ dir.addChild(parseEntry());
+ } else if ("directory".equals(name)) {
+ dir.setName(get("name"));
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ long t1 = System.currentTimeMillis();
+ Log.d(TAG, "Got music directory in " + (t1 - t0) + "ms.");
+
+ return dir;
+ }
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicFoldersParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicFoldersParser.java
new file mode 100644
index 00000000..e043aeb4
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/MusicFoldersParser.java
@@ -0,0 +1,69 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xmlpull.v1.XmlPullParser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicFolder;
+import github.daneren2005.subdroid.domain.Playlist;
+import github.daneren2005.subdroid.util.ProgressListener;
+
+/**
+ * @author Sindre Mehus
+ */
+public class MusicFoldersParser extends AbstractParser {
+
+ public MusicFoldersParser(Context context) {
+ super(context);
+ }
+
+ public List<MusicFolder> parse(Reader reader, ProgressListener progressListener) throws Exception {
+
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ List<MusicFolder> result = new ArrayList<MusicFolder>();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String tag = getElementName();
+ if ("musicFolder".equals(tag)) {
+ String id = get("id");
+ String name = get("name");
+ result.add(new MusicFolder(id, name));
+ } else if ("error".equals(tag)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return result;
+ }
+
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistParser.java
new file mode 100644
index 00000000..7826a712
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistParser.java
@@ -0,0 +1,62 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class PlaylistParser extends MusicDirectoryEntryParser {
+
+ public PlaylistParser(Context context) {
+ super(context);
+ }
+
+ public MusicDirectory parse(Reader reader, ProgressListener progressListener) throws Exception {
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ MusicDirectory dir = new MusicDirectory();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("entry".equals(name)) {
+ dir.addChild(parseEntry());
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return dir;
+ }
+
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistsParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistsParser.java
new file mode 100644
index 00000000..bd8a5bfe
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/PlaylistsParser.java
@@ -0,0 +1,67 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.Playlist;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Sindre Mehus
+ */
+public class PlaylistsParser extends AbstractParser {
+
+ public PlaylistsParser(Context context) {
+ super(context);
+ }
+
+ public List<Playlist> parse(Reader reader, ProgressListener progressListener) throws Exception {
+
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ List<Playlist> result = new ArrayList<Playlist>();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String tag = getElementName();
+ if ("playlist".equals(tag)) {
+ String id = get("id");
+ String name = get("name");
+ result.add(new Playlist(id, name));
+ } else if ("error".equals(tag)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return result;
+ }
+
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/RandomSongsParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/RandomSongsParser.java
new file mode 100644
index 00000000..eaeb7214
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/RandomSongsParser.java
@@ -0,0 +1,62 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+
+/**
+ * @author Sindre Mehus
+ */
+public class RandomSongsParser extends MusicDirectoryEntryParser {
+
+ public RandomSongsParser(Context context) {
+ super(context);
+ }
+
+ public MusicDirectory parse(Reader reader, ProgressListener progressListener) throws Exception {
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ MusicDirectory dir = new MusicDirectory();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("song".equals(name)) {
+ dir.addChild(parseEntry());
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return dir;
+ }
+
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResult2Parser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResult2Parser.java
new file mode 100644
index 00000000..cb2f9ec2
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResult2Parser.java
@@ -0,0 +1,75 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.domain.SearchResult;
+import github.daneren2005.subdroid.domain.Artist;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Sindre Mehus
+ */
+public class SearchResult2Parser extends MusicDirectoryEntryParser {
+
+ public SearchResult2Parser(Context context) {
+ super(context);
+ }
+
+ public SearchResult parse(Reader reader, ProgressListener progressListener) throws Exception {
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ List<Artist> artists = new ArrayList<Artist>();
+ List<MusicDirectory.Entry> albums = new ArrayList<MusicDirectory.Entry>();
+ List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("artist".equals(name)) {
+ Artist artist = new Artist();
+ artist.setId(get("id"));
+ artist.setName(get("name"));
+ artists.add(artist);
+ } else if ("album".equals(name)) {
+ albums.add(parseEntry());
+ } else if ("song".equals(name)) {
+ songs.add(parseEntry());
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return new SearchResult(artists, albums, songs);
+ }
+
+} \ No newline at end of file
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResultParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResultParser.java
new file mode 100644
index 00000000..7a899011
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SearchResultParser.java
@@ -0,0 +1,67 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import android.content.Context;
+import github.daneren2005.subdroid.R;
+import github.daneren2005.subdroid.domain.MusicDirectory;
+import github.daneren2005.subdroid.domain.SearchResult;
+import github.daneren2005.subdroid.domain.Artist;
+import github.daneren2005.subdroid.util.ProgressListener;
+import org.xmlpull.v1.XmlPullParser;
+
+import java.io.Reader;
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Sindre Mehus
+ */
+public class SearchResultParser extends MusicDirectoryEntryParser {
+
+ public SearchResultParser(Context context) {
+ super(context);
+ }
+
+ public SearchResult parse(Reader reader, ProgressListener progressListener) throws Exception {
+ updateProgress(progressListener, R.string.parser_reading);
+ init(reader);
+
+ List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
+ int eventType;
+ do {
+ eventType = nextParseEvent();
+ if (eventType == XmlPullParser.START_TAG) {
+ String name = getElementName();
+ if ("match".equals(name)) {
+ songs.add(parseEntry());
+ } else if ("error".equals(name)) {
+ handleError();
+ }
+ }
+ } while (eventType != XmlPullParser.END_DOCUMENT);
+
+ validate();
+ updateProgress(progressListener, R.string.parser_reading_done);
+
+ return new SearchResult(Collections.<Artist>emptyList(), Collections.<MusicDirectory.Entry>emptyList(), songs);
+ }
+
+}
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/SubsonicRESTException.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SubsonicRESTException.java
new file mode 100644
index 00000000..a694eeea
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/SubsonicRESTException.java
@@ -0,0 +1,19 @@
+package github.daneren2005.subdroid.service.parser;
+
+/**
+ * @author Sindre Mehus
+ * @version $Id$
+ */
+public class SubsonicRESTException extends Exception {
+
+ private final int code;
+
+ public SubsonicRESTException(int code, String message) {
+ super(message);
+ this.code = code;
+ }
+
+ public int getCode() {
+ return code;
+ }
+}
diff --git a/subsonic-android/src/github/daneren2005/subdroid/service/parser/VersionParser.java b/subsonic-android/src/github/daneren2005/subdroid/service/parser/VersionParser.java
new file mode 100644
index 00000000..3ccd4f9a
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subdroid/service/parser/VersionParser.java
@@ -0,0 +1,47 @@
+/*
+ This file is part of Subsonic.
+
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+
+ Copyright 2009 (C) Sindre Mehus
+ */
+package github.daneren2005.subdroid.service.parser;
+
+import github.daneren2005.subdroid.domain.Version;
+
+import java.io.BufferedReader;
+import java.io.Reader;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Sindre Mehus
+ */
+public class VersionParser {
+
+ public Version parse(Reader reader) throws Exception {
+
+ BufferedReader bufferedReader = new BufferedReader(reader);
+ Pattern pattern = Pattern.compile("SUBSONIC_ANDROID_VERSION_BEGIN(.*)SUBSONIC_ANDROID_VERSION_END");
+ String line = bufferedReader.readLine();
+ while (line != null) {
+ Matcher finalMatcher = pattern.matcher(line);
+ if (finalMatcher.find()) {
+ return new Version(finalMatcher.group(1));
+ }
+ line = bufferedReader.readLine();
+ }
+ return null;
+ }
+} \ No newline at end of file