Commit 1daac106a7327e9e3e2c3483e9263235c2f47112
1 parent
1288a4bb
Parser completado, falta testearlo y la funcion de Marc para que acabe de funcionar
Showing
3 changed files
with
203 additions
and
15 deletions
src/com/upc/pbe/upcnews/List.java
... | ... | @@ -5,13 +5,15 @@ import java.util.ArrayList; |
5 | 5 | public class List |
6 | 6 | { |
7 | 7 | private boolean validated; |
8 | + private int quality; | |
8 | 9 | private int sequenceFirst; |
9 | 10 | private int maxDuration; |
10 | - private ArrayList<Segment> segments; | |
11 | + private ArrayList<Segment> segments; //Lista de SEGMENTOS | |
11 | 12 | private ParentList parent; |
12 | 13 | |
13 | - public List() | |
14 | + public List(int q) | |
14 | 15 | { |
16 | + quality = q; | |
15 | 17 | validated = false; |
16 | 18 | segments = new ArrayList<Segment>(); |
17 | 19 | } |
... | ... | @@ -46,6 +48,11 @@ public class List |
46 | 48 | return maxDuration; |
47 | 49 | } |
48 | 50 | |
51 | + public int getQuality() | |
52 | + { | |
53 | + return quality; | |
54 | + } | |
55 | + | |
49 | 56 | public void setParent(ParentList p) |
50 | 57 | { |
51 | 58 | parent = p; | ... | ... |
src/com/upc/pbe/upcnews/ParentList.java
... | ... | @@ -6,13 +6,18 @@ public class ParentList |
6 | 6 | { |
7 | 7 | private String ID; |
8 | 8 | private int currentQuality; |
9 | + private String Type, Name; | |
10 | + private boolean Default; //Estos 3 últimos son para los ext-x-media, los ext-x-stream lo IGNORAN | |
9 | 11 | private ArrayList<List> lists; //per cada qualitat |
10 | 12 | |
11 | 13 | public ParentList(String ID) |
12 | 14 | { |
13 | 15 | this.ID = ID; |
14 | 16 | currentQuality = 0; |
15 | - lists = new ArrayList<List>(); | |
17 | + Type = ""; | |
18 | + Name = ""; | |
19 | + Default = false; | |
20 | + lists = new ArrayList<List>(); //Lista de CALIDADES | |
16 | 21 | } |
17 | 22 | |
18 | 23 | public String getID() |
... | ... | @@ -20,15 +25,42 @@ public class ParentList |
20 | 25 | return ID; |
21 | 26 | } |
22 | 27 | |
23 | - public int getQuality() | |
28 | + public int getCurrentQuality() | |
24 | 29 | { |
25 | 30 | return currentQuality; |
26 | 31 | } |
27 | - public void setQuality(int q) | |
32 | + public void setCurrentQuality(int q) | |
28 | 33 | { |
29 | 34 | currentQuality = q; |
30 | 35 | } |
31 | 36 | |
37 | + public boolean getDefault() | |
38 | + { | |
39 | + return Default; | |
40 | + } | |
41 | + public void setDefault(boolean d) | |
42 | + { | |
43 | + Default = d; | |
44 | + } | |
45 | + | |
46 | + public String getType() | |
47 | + { | |
48 | + return Type; | |
49 | + } | |
50 | + public void setType(String t) | |
51 | + { | |
52 | + Type = t; | |
53 | + } | |
54 | + | |
55 | + public String getName() | |
56 | + { | |
57 | + return Name; | |
58 | + } | |
59 | + public void setName(String n) | |
60 | + { | |
61 | + Name = n; | |
62 | + } | |
63 | + | |
32 | 64 | public ArrayList<List> getLists() |
33 | 65 | { |
34 | 66 | return lists; | ... | ... |
src/com/upc/pbe/upcnews/Parser.java
... | ... | @@ -30,10 +30,13 @@ public class Parser |
30 | 30 | { |
31 | 31 | ArrayList<ParentList> lists = new ArrayList<ParentList>(); |
32 | 32 | lists.add(new ParentList("")); |
33 | - lists.get(0).getLists().add(new List()); | |
33 | + lists.get(0).getLists().add(new List(-1)); | |
34 | 34 | String[] lines = file.split("\n"); |
35 | 35 | for(int i = 0; i < lines.length; i++) |
36 | 36 | { |
37 | + /* | |
38 | + * TO-DO: Detectar los \ y juntarlos en una sola linea | |
39 | + */ | |
37 | 40 | parseLine(lines[i],lists); |
38 | 41 | } |
39 | 42 | /* |
... | ... | @@ -47,6 +50,10 @@ public class Parser |
47 | 50 | * PD: En el último nivel de profundidad hay una lista de segmentos (debajo de calidades). |
48 | 51 | * Allí está el auténtico tesoro de Narnia. |
49 | 52 | */ |
53 | + for(int i = 0; i < lists.size(); i++) | |
54 | + { | |
55 | + sortQuality(lists.get(i)); | |
56 | + } | |
50 | 57 | return lists; |
51 | 58 | } |
52 | 59 | |
... | ... | @@ -62,6 +69,40 @@ public class Parser |
62 | 69 | return -1; |
63 | 70 | } |
64 | 71 | |
72 | + private void sortQuality(ParentList ppls) | |
73 | + { | |
74 | + ArrayList<List> lists = ppls.getLists(); | |
75 | + if(lists.get(0).getQuality() == -1) | |
76 | + { | |
77 | + /* | |
78 | + * Se trata de ext-x-media's y estos no van por calidad | |
79 | + */ | |
80 | + return; | |
81 | + } | |
82 | + //BUBBLE-SORT!! (Me da un poco de verguenza, pero no quiero ponerme con quicksort... | |
83 | + while(true) | |
84 | + { | |
85 | + boolean sorted = true; | |
86 | + int i = 0; | |
87 | + do | |
88 | + { | |
89 | + if(lists.get(i).getQuality() < lists.get(i+1).getQuality()) | |
90 | + { | |
91 | + sorted = false; | |
92 | + List aux = lists.get(i); | |
93 | + lists.set(i, lists.get(i+1)); | |
94 | + lists.set(i+1, aux); | |
95 | + } | |
96 | + | |
97 | + }while(++i != lists.size()); | |
98 | + if(sorted) | |
99 | + { | |
100 | + break; | |
101 | + } | |
102 | + } | |
103 | + | |
104 | + } | |
105 | + | |
65 | 106 | public void parseLine(String line, ArrayList<ParentList> lists) throws ErrorException, WarningException, InfoException |
66 | 107 | { |
67 | 108 | if(line.isEmpty()) |
... | ... | @@ -72,7 +113,7 @@ public class Parser |
72 | 113 | else |
73 | 114 | { |
74 | 115 | ParentList ppls = lists.get(currentList); //VIVA HONDURAS!!! |
75 | - List pls = ppls.getLists().get(ppls.getQuality()); | |
116 | + List pls = ppls.getLists().get(ppls.getCurrentQuality()); | |
76 | 117 | if(!pls.getValidated()) |
77 | 118 | { |
78 | 119 | if(line.equals(STARTWORD)) |
... | ... | @@ -98,8 +139,7 @@ public class Parser |
98 | 139 | else if(extTag[0].equals("#EXT-X-TARGETDURATION")) |
99 | 140 | { |
100 | 141 | pls.setMaxDuration(Integer.parseInt(extTag[1])); |
101 | - } | |
102 | - | |
142 | + } | |
103 | 143 | else if(extTag[0].equals("#EXTINF")) |
104 | 144 | { |
105 | 145 | if(fileType == -1) |
... | ... | @@ -146,13 +186,14 @@ public class Parser |
146 | 186 | String[] arguments = extTag[1].split(","); |
147 | 187 | for(int i = 0; i < arguments.length; i++) |
148 | 188 | { |
149 | - if(arguments[i].equals("PROGRAM-ID")) | |
189 | + String[] argument = arguments[i].split("="); | |
190 | + if(argument[0].equals("PROGRAM-ID")) | |
150 | 191 | { |
151 | - programID = arguments[i]; | |
192 | + programID = argument[1]; | |
152 | 193 | } |
153 | - else if(arguments[i].equals("BANDWIDTH")) | |
194 | + else if(argument[0].equals("BANDWIDTH")) | |
154 | 195 | { |
155 | - bandwidth = Integer.parseInt(arguments[i]); | |
196 | + bandwidth = Integer.parseInt(argument[1]); | |
156 | 197 | } |
157 | 198 | } |
158 | 199 | if(programID.equals("")|| bandwidth == -1) |
... | ... | @@ -181,16 +222,124 @@ public class Parser |
181 | 222 | * ppls y pls se actualizan en la siguiente linea procesada |
182 | 223 | */ |
183 | 224 | } |
225 | + else | |
226 | + { | |
227 | + ppls = lists.get(currentList); | |
228 | + ppls.getLists().add(new List(bandwidth)); | |
229 | + } | |
184 | 230 | } |
185 | 231 | } |
232 | + else | |
233 | + { | |
234 | + ppls.getLists().add(new List(bandwidth)); | |
235 | + } | |
186 | 236 | } |
187 | 237 | else if(extTag[0].equals("#EXT-X-MEDIA")) |
188 | 238 | { |
189 | - //TO-DO | |
239 | + if(fileType == -1) | |
240 | + { | |
241 | + fileType = 1; | |
242 | + } | |
243 | + else if(fileType == 0) | |
244 | + { | |
245 | + throw new ErrorException("Invalid file, should contain Segments, " + | |
246 | + "but lists were found."); | |
247 | + } | |
248 | + String Type = ""; | |
249 | + String Name = ""; | |
250 | + String GroupID = ""; | |
251 | + String URI = ""; | |
252 | + boolean Default = false; | |
253 | + String[] arguments = extTag[1].split(","); | |
254 | + for(int i = 0; i < arguments.length; i++) | |
255 | + { | |
256 | + String[] argument = arguments[i].split("="); | |
257 | + if(argument[0].equals("NAME")) | |
258 | + { | |
259 | + Name = argument[1]; | |
260 | + } | |
261 | + else if(argument[0].equals("TYPE")) | |
262 | + { | |
263 | + Type = argument[1]; | |
264 | + } | |
265 | + else if(argument[0].equals("GROUP-ID")) | |
266 | + { | |
267 | + GroupID = argument[1]; | |
268 | + } | |
269 | + else if(argument[0].equals("DEFAULT")) | |
270 | + { | |
271 | + if(argument[1].equals("YES")) | |
272 | + { | |
273 | + Default = true; | |
274 | + } | |
275 | + else if(argument[1].equals("NO")) | |
276 | + { | |
277 | + Default = true; | |
278 | + } | |
279 | + else | |
280 | + { | |
281 | + throw new ErrorException("Invalid value for argument DEFAULT on line " + | |
282 | + currentLine); | |
283 | + } | |
284 | + } | |
285 | + else if(argument[0].equals("URI")) | |
286 | + { | |
287 | + URI = argument[1]; | |
288 | + } | |
289 | + } | |
290 | + if(Type.equals("") || Name.equals("") || URI.equals("") || GroupID.equals("")) | |
291 | + { | |
292 | + throw new ErrorException("Playlist on line " + currentLine + | |
293 | + " has missing arguments"); | |
294 | + } | |
295 | + ParentList pl = new ParentList(GroupID); | |
296 | + pl.setDefault(Default); | |
297 | + pl.setName(Name); | |
298 | + pl.setType(Type); | |
299 | + /* | |
300 | + * Procesar URI y descargar listas recursivamente y añadir a la lista actual. | |
301 | + * | |
302 | + * TO-DO: Necesito la funcion de Marc | |
303 | + */ | |
304 | + List l = new List(-1); | |
305 | + if(!GroupID.equals(ppls.getID())) | |
306 | + { | |
307 | + if(ppls.getID().equals("")) | |
308 | + { | |
309 | + /* | |
310 | + * Este caso se da cuando no se ha creado ninguna lista. | |
311 | + * Hay que sustituir la por defecto por una lista real. | |
312 | + */ | |
313 | + lists.add(0,pl); | |
314 | + } | |
315 | + else | |
316 | + { | |
317 | + | |
318 | + currentList = searchID(ppls.getID(), lists); | |
319 | + if(currentList == -1) | |
320 | + { | |
321 | + lists.add(pl); | |
322 | + currentList = lists.size()+1; | |
323 | + } | |
324 | + else | |
325 | + { | |
326 | + ppls = lists.get(currentList); | |
327 | + ppls.getLists().add(l); | |
328 | + } | |
329 | + } | |
330 | + } | |
331 | + else | |
332 | + { | |
333 | + ppls.getLists().add(l); | |
334 | + } | |
190 | 335 | } |
191 | 336 | |
192 | 337 | else if(extTag[0].equals("#EXT-X-ENDLIST")) |
193 | 338 | { |
339 | + if(expectSegment || expectList) | |
340 | + { | |
341 | + throw new ErrorException("Unexpected end of list!"); | |
342 | + } | |
194 | 343 | //END REACHED, IGNORE AND LET THE FILE REACH EOF |
195 | 344 | } |
196 | 345 | |
... | ... | @@ -242,4 +391,4 @@ public class Parser |
242 | 391 | currentLine++; |
243 | 392 | } |
244 | 393 | } |
245 | 394 | -} |
395 | +} | |
246 | 396 | \ No newline at end of file | ... | ... |