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 |
|
|
23 |
|
import org.xmlpull.v1.XmlPullParser; |
24 |
|
|
25 |
|
import android.content.Context; |
26 |
|
import android.util.Xml; |
27 |
|
import net.sourceforge.subsonic.androidapp.R; |
28 |
|
import net.sourceforge.subsonic.androidapp.domain.Version; |
29 |
|
import net.sourceforge.subsonic.androidapp.util.ProgressListener; |
30 |
|
import net.sourceforge.subsonic.androidapp.util.Util; |
31 |
|
|
32 |
|
|
33 |
|
@author |
34 |
|
|
|
|
| 75,6% |
Uncovered Elements: 19 (78) |
Complexity: 27 |
Complexity Density: 0,57 |
|
35 |
|
public abstract class AbstractParser { |
36 |
|
|
37 |
|
private final Context context; |
38 |
|
private XmlPullParser parser; |
39 |
|
private boolean rootElementFound; |
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
41 |
36
|
public AbstractParser(Context context) {... |
42 |
36
|
this.context = context; |
43 |
|
} |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
45 |
3
|
protected Context getContext() {... |
46 |
3
|
return context; |
47 |
|
} |
48 |
|
|
|
|
| 52,6% |
Uncovered Elements: 9 (19) |
Complexity: 5 |
Complexity Density: 0,26 |
|
49 |
8
|
protected void handleError() throws Exception {... |
50 |
8
|
int code = getInteger("code"); |
51 |
8
|
String message; |
52 |
8
|
switch (code) { |
53 |
0
|
case 20: |
54 |
0
|
message = context.getResources().getString(R.string.parser_upgrade_client); |
55 |
0
|
break; |
56 |
0
|
case 30: |
57 |
0
|
message = context.getResources().getString(R.string.parser_upgrade_server); |
58 |
0
|
break; |
59 |
0
|
case 40: |
60 |
0
|
message = context.getResources().getString(R.string.parser_not_authenticated); |
61 |
0
|
break; |
62 |
4
|
case 50: |
63 |
4
|
message = context.getResources().getString(R.string.parser_not_authorized); |
64 |
4
|
break; |
65 |
4
|
default: |
66 |
4
|
message = get("message"); |
67 |
4
|
break; |
68 |
|
} |
69 |
8
|
throw new SubsonicRESTException(code, message); |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
72 |
44
|
protected void updateProgress(ProgressListener progressListener, int messageId) {... |
73 |
44
|
if (progressListener != null) { |
74 |
34
|
progressListener.updateProgress(messageId); |
75 |
|
} |
76 |
|
} |
77 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
78 |
3
|
protected void updateProgress(ProgressListener progressListener, String message) {... |
79 |
3
|
if (progressListener != null) { |
80 |
3
|
progressListener.updateProgress(message); |
81 |
|
} |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
1
|
protected String getText() {... |
85 |
1
|
return parser.getText(); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
8530
|
protected String get(String name) {... |
89 |
8530
|
return parser.getAttributeValue(null, name); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
466
|
protected boolean getBoolean(String name) {... |
93 |
466
|
return "true".equals(get(name)); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
96 |
816
|
protected Integer getInteger(String name) {... |
97 |
816
|
String s = get(name); |
98 |
816
|
return s == null ? null : Integer.valueOf(s); |
99 |
|
} |
100 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
101 |
203
|
protected Long getLong(String name) {... |
102 |
203
|
String s = get(name); |
103 |
203
|
return s == null ? null : Long.valueOf(s); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
106 |
0
|
protected Float getFloat(String name) {... |
107 |
0
|
String s = get(name); |
108 |
0
|
return s == null ? null : Float.valueOf(s); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
111 |
36
|
protected void init(Reader reader) throws Exception {... |
112 |
36
|
parser = Xml.newPullParser(); |
113 |
36
|
parser.setInput(reader); |
114 |
36
|
rootElementFound = false; |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
7437
|
protected int nextParseEvent() throws Exception {... |
118 |
7437
|
return parser.next(); |
119 |
|
} |
120 |
|
|
|
|
| 90,9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
121 |
2469
|
protected String getElementName() {... |
122 |
2469
|
String name = parser.getName(); |
123 |
2469
|
if ("subsonic-response".equals(name)) { |
124 |
36
|
rootElementFound = true; |
125 |
36
|
String version = get("version"); |
126 |
36
|
if (version != null) { |
127 |
36
|
Util.setServerRestVersion(context, new Version(version)); |
128 |
|
} |
129 |
|
} |
130 |
2469
|
return name; |
131 |
|
} |
132 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
133 |
28
|
protected void validate() throws Exception {... |
134 |
28
|
if (!rootElementFound) { |
135 |
0
|
throw new Exception(context.getResources().getString(R.string.background_task_parse_error)); |
136 |
|
} |
137 |
|
} |
138 |
|
} |