-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseXSPF.java
83 lines (74 loc) · 2.36 KB
/
ParseXSPF.java
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
import nanoxml.*;
import java.util.*;
import java.io.*;
/**
* Parser XSPF
*/
public class ParseXSPF{
public static void main(String args[]){
parse(args[0]);
}
/**
* Parser de archivos XSPF.
* @param filename Archivo XSPF que se debe parsear.
* @return HashMap de la concatenación del título de la canción
* con el autor de la canción a objetos de tipo Song.
*/
@SuppressWarnings("unchecked")
public static HashMap<String,Song> parse(String filename){
HashMap<String,Song> sl = new HashMap<String,Song>();
try{
XMLElement xspf = new XMLElement();
FileReader reader = new FileReader(filename);
xspf.parseFromReader(reader);
if(xspf.getName().compareTo("playlist") == 0){
//Busco el elemento trackList
Enumeration<XMLElement> playlistContents = xspf.enumerateChildren();
XMLElement trackList = null;
do{
trackList = (XMLElement) playlistContents.nextElement();
}while((trackList != null)&&
(trackList.getName().compareTo("trackList") != 0));
if(trackList == null){
System.out.println("Error, lista de reproduccion mal formateada: "
+ "No se encontro trackList");
System.exit(1);
}
//Itero sobre las canciones
Enumeration<XMLElement> tracks = trackList.enumerateChildren();
while(tracks.hasMoreElements()){
XMLElement track = (XMLElement)tracks.nextElement();
Song s = new Song();
Enumeration<XMLElement> attrs = track.enumerateChildren();
while(attrs.hasMoreElements()){
XMLElement attr = (XMLElement)attrs.nextElement();
get_xspf_attr(attr,s);
}
sl.put(s.title+"-"+s.creator,s);
}
}
else{
System.out.println("Error, lista de reproduccion mal formateada: "
+ "No se encontró elemento playlist en el tope del árbol");
System.exit(1);
}
}
catch(Exception e){}
return sl;
}
/**
* Escribe un atributo de una canción almacenado en el archivo
* xspf en un objeto Song.
* @param attr Atributo XSPF.
* @param s Objeto Song.
*/
public static void get_xspf_attr(XMLElement attr, Song s){
String attr_name = attr.getName();
if (attr_name.compareTo("title") == 0){
s.title = attr.getContent().toLowerCase();
}
else if (attr_name.compareTo("creator") == 0){
s.creator = attr.getContent().toLowerCase();
}
}
}