summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/cmpc/util/MedialistParser.java
blob: 8f69af0f8081502927868bca78282e46c549f370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package de.schwarzrot.cmpc.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.google.gson.stream.JsonReader;
import de.schwarzrot.cmpc.domain.Media;
import de.schwarzrot.cmpc.domain.MediaType;
import de.schwarzrot.cmpc.domain.PlayList;


public class MedialistParser {
    public PlayList parseListChunk(InputStream is) {
        JsonReader reader = null;
        PlayList rv = null;

        try {
            reader = new JsonReader(new InputStreamReader(is, "UTF-8"));

            rv = readPlayList(reader);
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Throwable t) {
            }
        }
        return rv;
    }


    @Test
    public void testJSonParsing() {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(new File("test.json"));
            parseListChunk(fis);
            fis.close();
            fis = null;
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Throwable t) {
                }
            }
        }
    }


    protected Media readMedia(JsonReader reader) {
        Media rv = new Media();

        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if ("name".equals(name)) {
                    rv.setName(reader.nextString());
                } else if ("type".equals(name)) {
                    rv.setType(reader.nextInt());
                } else if ("format".equals(name)) {
                    rv.setFormat(reader.nextString());
                } else if ("width".equals(name)) {
                    rv.setWidth(reader.nextInt());
                } else if ("height".equals(name)) {
                    rv.setHeight(reader.nextInt());
                } else if ("path".equals(name)) {
                    rv.setPath(reader.nextString());
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }


    protected List<Media> readMediaArray(JsonReader reader) {
        List<Media> rv = new ArrayList<Media>();

        try {
            reader.beginArray();
            while (reader.hasNext()) {
                rv.add(readMedia(reader));
            }
            reader.endArray();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }


    protected MediaType readMediaType(JsonReader reader) {
        MediaType rv = new MediaType();

        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if ("name".equals(name)) {
                    rv.setName(reader.nextString());
                } else if ("start".equals(name)) {
                    rv.setStart(reader.nextLong());
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }


    protected PlayList readPlayList(JsonReader reader) {
        PlayList rv = new PlayList();

        try {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if ("total".equals(name)) {
                    rv.setTotal(reader.nextLong());
                } else if ("types".equals(name)) {
                    rv.setTypes(readTypeArray(reader));
                } else if ("results".equals(name)) {
                    rv.setResults(readMediaArray(reader));
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (Throwable t) {
            t.printStackTrace();
            rv = null;
        }
        return rv;
    }


    protected List<MediaType> readTypeArray(JsonReader reader) {
        List<MediaType> rv = new ArrayList<MediaType>();

        try {
            reader.beginArray();
            while (reader.hasNext()) {
                rv.add(readMediaType(reader));
            }
            reader.endArray();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return rv;
    }
}