1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.sourceforge.subsonic.androidapp.service.parser; |
20 |
|
|
21 |
|
import java.io.Reader; |
22 |
|
import java.util.List; |
23 |
|
import java.util.ArrayList; |
24 |
|
|
25 |
|
import org.xmlpull.v1.XmlPullParser; |
26 |
|
|
27 |
|
import android.content.Context; |
28 |
|
import net.sourceforge.subsonic.androidapp.R; |
29 |
|
import net.sourceforge.subsonic.androidapp.domain.Artist; |
30 |
|
import net.sourceforge.subsonic.androidapp.domain.Indexes; |
31 |
|
import net.sourceforge.subsonic.androidapp.util.ProgressListener; |
32 |
|
import android.util.Log; |
33 |
|
|
34 |
|
|
35 |
|
@author |
36 |
|
|
|
|
| 95,5% |
Uncovered Elements: 3 (66) |
Complexity: 12 |
Complexity Density: 0,27 |
|
37 |
|
public class IndexesParser extends AbstractParser { |
38 |
|
private static final String TAG = IndexesParser.class.getSimpleName(); |
39 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
40 |
3
|
public IndexesParser(Context context) {... |
41 |
3
|
super(context); |
42 |
|
} |
43 |
|
|
|
|
| 95,2% |
Uncovered Elements: 3 (63) |
Complexity: 11 |
Complexity Density: 0,26 |
|
44 |
3
|
public Indexes parse(Reader reader, ProgressListener progressListener) throws Exception {... |
45 |
|
|
46 |
3
|
long t0 = System.currentTimeMillis(); |
47 |
3
|
updateProgress(progressListener, R.string.parser_reading); |
48 |
3
|
init(reader); |
49 |
|
|
50 |
3
|
List<Artist> artists = new ArrayList<Artist>(); |
51 |
3
|
List<Artist> shortcuts = new ArrayList<Artist>(); |
52 |
3
|
Long lastModified = null; |
53 |
3
|
int eventType; |
54 |
3
|
String index = "#"; |
55 |
3
|
boolean changed = false; |
56 |
|
|
57 |
3
|
do { |
58 |
123
|
eventType = nextParseEvent(); |
59 |
123
|
if (eventType == XmlPullParser.START_TAG) { |
60 |
37
|
String name = getElementName(); |
61 |
37
|
if ("indexes".equals(name)) { |
62 |
1
|
changed = true; |
63 |
1
|
lastModified = getLong("lastModified"); |
64 |
36
|
} else if ("index".equals(name)) { |
65 |
10
|
index = get("name"); |
66 |
|
|
67 |
26
|
} else if ("artist".equals(name)) { |
68 |
22
|
Artist artist = new Artist(); |
69 |
22
|
artist.setId(get("id")); |
70 |
22
|
artist.setName(get("name")); |
71 |
22
|
artist.setIndex(index); |
72 |
22
|
artists.add(artist); |
73 |
|
|
74 |
22
|
if (artists.size() % 10 == 0) { |
75 |
2
|
String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size()); |
76 |
2
|
updateProgress(progressListener, msg); |
77 |
|
} |
78 |
4
|
} else if ("shortcut".equals(name)) { |
79 |
1
|
Artist shortcut = new Artist(); |
80 |
1
|
shortcut.setId(get("id")); |
81 |
1
|
shortcut.setName(get("name")); |
82 |
1
|
shortcut.setIndex("*"); |
83 |
1
|
shortcuts.add(shortcut); |
84 |
3
|
} else if ("error".equals(name)) { |
85 |
0
|
handleError(); |
86 |
|
} |
87 |
|
} |
88 |
123
|
} while (eventType != XmlPullParser.END_DOCUMENT); |
89 |
|
|
90 |
3
|
validate(); |
91 |
|
|
92 |
3
|
if (!changed) { |
93 |
2
|
return null; |
94 |
|
} |
95 |
|
|
96 |
1
|
long t1 = System.currentTimeMillis(); |
97 |
1
|
Log.d(TAG, "Got " + artists.size() + " artist(s) in " + (t1 - t0) + "ms."); |
98 |
|
|
99 |
1
|
String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size()); |
100 |
1
|
updateProgress(progressListener, msg); |
101 |
|
|
102 |
1
|
return new Indexes(lastModified == null ? 0L : lastModified, shortcuts, artists); |
103 |
|
} |
104 |
|
} |