From 1daac106a7327e9e3e2c3483e9263235c2f47112 Mon Sep 17 00:00:00 2001 From: Equip de Desenvolupadors de PBE Date: Tue, 4 Dec 2012 08:19:02 +0000 Subject: [PATCH] Parser completado, falta testearlo y la funcion de Marc para que acabe de funcionar --- src/com/upc/pbe/upcnews/List.java | 11 +++++++++-- src/com/upc/pbe/upcnews/ParentList.java | 38 +++++++++++++++++++++++++++++++++++--- src/com/upc/pbe/upcnews/Parser.java | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 203 insertions(+), 15 deletions(-) diff --git a/src/com/upc/pbe/upcnews/List.java b/src/com/upc/pbe/upcnews/List.java index f0412eb..25e566a 100644 --- a/src/com/upc/pbe/upcnews/List.java +++ b/src/com/upc/pbe/upcnews/List.java @@ -5,13 +5,15 @@ import java.util.ArrayList; public class List { private boolean validated; + private int quality; private int sequenceFirst; private int maxDuration; - private ArrayList segments; + private ArrayList segments; //Lista de SEGMENTOS private ParentList parent; - public List() + public List(int q) { + quality = q; validated = false; segments = new ArrayList(); } @@ -46,6 +48,11 @@ public class List return maxDuration; } + public int getQuality() + { + return quality; + } + public void setParent(ParentList p) { parent = p; diff --git a/src/com/upc/pbe/upcnews/ParentList.java b/src/com/upc/pbe/upcnews/ParentList.java index 815115d..4a9eae4 100644 --- a/src/com/upc/pbe/upcnews/ParentList.java +++ b/src/com/upc/pbe/upcnews/ParentList.java @@ -6,13 +6,18 @@ public class ParentList { private String ID; private int currentQuality; + private String Type, Name; + private boolean Default; //Estos 3 últimos son para los ext-x-media, los ext-x-stream lo IGNORAN private ArrayList lists; //per cada qualitat public ParentList(String ID) { this.ID = ID; currentQuality = 0; - lists = new ArrayList(); + Type = ""; + Name = ""; + Default = false; + lists = new ArrayList(); //Lista de CALIDADES } public String getID() @@ -20,15 +25,42 @@ public class ParentList return ID; } - public int getQuality() + public int getCurrentQuality() { return currentQuality; } - public void setQuality(int q) + public void setCurrentQuality(int q) { currentQuality = q; } + public boolean getDefault() + { + return Default; + } + public void setDefault(boolean d) + { + Default = d; + } + + public String getType() + { + return Type; + } + public void setType(String t) + { + Type = t; + } + + public String getName() + { + return Name; + } + public void setName(String n) + { + Name = n; + } + public ArrayList getLists() { return lists; diff --git a/src/com/upc/pbe/upcnews/Parser.java b/src/com/upc/pbe/upcnews/Parser.java index 84b1725..68cf3b2 100644 --- a/src/com/upc/pbe/upcnews/Parser.java +++ b/src/com/upc/pbe/upcnews/Parser.java @@ -30,10 +30,13 @@ public class Parser { ArrayList lists = new ArrayList(); lists.add(new ParentList("")); - lists.get(0).getLists().add(new List()); + lists.get(0).getLists().add(new List(-1)); String[] lines = file.split("\n"); for(int i = 0; i < lines.length; i++) { + /* + * TO-DO: Detectar los \ y juntarlos en una sola linea + */ parseLine(lines[i],lists); } /* @@ -47,6 +50,10 @@ public class Parser * PD: En el último nivel de profundidad hay una lista de segmentos (debajo de calidades). * Allí está el auténtico tesoro de Narnia. */ + for(int i = 0; i < lists.size(); i++) + { + sortQuality(lists.get(i)); + } return lists; } @@ -62,6 +69,40 @@ public class Parser return -1; } + private void sortQuality(ParentList ppls) + { + ArrayList lists = ppls.getLists(); + if(lists.get(0).getQuality() == -1) + { + /* + * Se trata de ext-x-media's y estos no van por calidad + */ + return; + } + //BUBBLE-SORT!! (Me da un poco de verguenza, pero no quiero ponerme con quicksort... + while(true) + { + boolean sorted = true; + int i = 0; + do + { + if(lists.get(i).getQuality() < lists.get(i+1).getQuality()) + { + sorted = false; + List aux = lists.get(i); + lists.set(i, lists.get(i+1)); + lists.set(i+1, aux); + } + + }while(++i != lists.size()); + if(sorted) + { + break; + } + } + + } + public void parseLine(String line, ArrayList lists) throws ErrorException, WarningException, InfoException { if(line.isEmpty()) @@ -72,7 +113,7 @@ public class Parser else { ParentList ppls = lists.get(currentList); //VIVA HONDURAS!!! - List pls = ppls.getLists().get(ppls.getQuality()); + List pls = ppls.getLists().get(ppls.getCurrentQuality()); if(!pls.getValidated()) { if(line.equals(STARTWORD)) @@ -98,8 +139,7 @@ public class Parser else if(extTag[0].equals("#EXT-X-TARGETDURATION")) { pls.setMaxDuration(Integer.parseInt(extTag[1])); - } - + } else if(extTag[0].equals("#EXTINF")) { if(fileType == -1) @@ -146,13 +186,14 @@ public class Parser String[] arguments = extTag[1].split(","); for(int i = 0; i < arguments.length; i++) { - if(arguments[i].equals("PROGRAM-ID")) + String[] argument = arguments[i].split("="); + if(argument[0].equals("PROGRAM-ID")) { - programID = arguments[i]; + programID = argument[1]; } - else if(arguments[i].equals("BANDWIDTH")) + else if(argument[0].equals("BANDWIDTH")) { - bandwidth = Integer.parseInt(arguments[i]); + bandwidth = Integer.parseInt(argument[1]); } } if(programID.equals("")|| bandwidth == -1) @@ -181,16 +222,124 @@ public class Parser * ppls y pls se actualizan en la siguiente linea procesada */ } + else + { + ppls = lists.get(currentList); + ppls.getLists().add(new List(bandwidth)); + } } } + else + { + ppls.getLists().add(new List(bandwidth)); + } } else if(extTag[0].equals("#EXT-X-MEDIA")) { - //TO-DO + if(fileType == -1) + { + fileType = 1; + } + else if(fileType == 0) + { + throw new ErrorException("Invalid file, should contain Segments, " + + "but lists were found."); + } + String Type = ""; + String Name = ""; + String GroupID = ""; + String URI = ""; + boolean Default = false; + String[] arguments = extTag[1].split(","); + for(int i = 0; i < arguments.length; i++) + { + String[] argument = arguments[i].split("="); + if(argument[0].equals("NAME")) + { + Name = argument[1]; + } + else if(argument[0].equals("TYPE")) + { + Type = argument[1]; + } + else if(argument[0].equals("GROUP-ID")) + { + GroupID = argument[1]; + } + else if(argument[0].equals("DEFAULT")) + { + if(argument[1].equals("YES")) + { + Default = true; + } + else if(argument[1].equals("NO")) + { + Default = true; + } + else + { + throw new ErrorException("Invalid value for argument DEFAULT on line " + + currentLine); + } + } + else if(argument[0].equals("URI")) + { + URI = argument[1]; + } + } + if(Type.equals("") || Name.equals("") || URI.equals("") || GroupID.equals("")) + { + throw new ErrorException("Playlist on line " + currentLine + + " has missing arguments"); + } + ParentList pl = new ParentList(GroupID); + pl.setDefault(Default); + pl.setName(Name); + pl.setType(Type); + /* + * Procesar URI y descargar listas recursivamente y añadir a la lista actual. + * + * TO-DO: Necesito la funcion de Marc + */ + List l = new List(-1); + if(!GroupID.equals(ppls.getID())) + { + if(ppls.getID().equals("")) + { + /* + * Este caso se da cuando no se ha creado ninguna lista. + * Hay que sustituir la por defecto por una lista real. + */ + lists.add(0,pl); + } + else + { + + currentList = searchID(ppls.getID(), lists); + if(currentList == -1) + { + lists.add(pl); + currentList = lists.size()+1; + } + else + { + ppls = lists.get(currentList); + ppls.getLists().add(l); + } + } + } + else + { + ppls.getLists().add(l); + } } else if(extTag[0].equals("#EXT-X-ENDLIST")) { + if(expectSegment || expectList) + { + throw new ErrorException("Unexpected end of list!"); + } //END REACHED, IGNORE AND LET THE FILE REACH EOF } @@ -242,4 +391,4 @@ public class Parser currentLine++; } } -} +} \ No newline at end of file -- libgit2 0.22.2