package itunes;
import common.TrackMetaData;
import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class ItunesXMLParser {
private static String [] monthname = {"dummy","January","February", "March", "April", "May", "July", "August", "September", "October", "November", "December"};
public synchronized static TrackMetaData parseTrackMetaData(String xml){
String title;
String album;
String duration; // not necessary?
String id; // id
String parentId; //parentID
String resource;
String streamContent;//<r:streamContent>
String albumArtUri;///upnp:albumArtURI>
String upnpClass;
String creator;
String albumArtist;//<r:albumArtist
String playDate;
int i, k;
i = xml.indexOf("<key>Total Time</key><integer>");
k = xml.indexOf("</integer>\t\t\t<key>Track Number");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
duration = xml.substring(i+30, k);
else
duration="";
i = xml.indexOf("<key>Track ID</key><integer>");
k = xml.indexOf("</integer>\t\t\t<key>Name");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
id = xml.substring(i+28, k);
else
id="";
parentId=id;
//System.out.println(parentId);
i = xml.indexOf("<key>Name</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Artist");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
title = xml.substring(i+23, k);
else
title="";
//System.out.println(title);
i= xml.indexOf("<key>Artist</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Album");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
creator = xml.substring(i+25,k);
else
creator="";
//System.out.println(creator);
i= xml.indexOf("<key>Album</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Genre");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
album = xml.substring(i+24,k);
else
album="";
//System.out.println(album);
i= xml.indexOf("<key>Track Type</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Location");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
upnpClass = xml.substring(i+29,k);
else
upnpClass="";
//System.out.println(upnpClass);
i= xml.indexOf("<key>Artist</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Album");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
albumArtist = xml.substring(i+25,k);
else
albumArtist="";
i= xml.indexOf("<key>Genre</key><string>");
k = xml.indexOf("</string>\t\t\t<key>Kind");
// System.out.println(i+" "+k);
if (i>-1 && k>-1)
resource = xml.substring(i+24,k);
else
resource ="";
//System.out.println(albumArtist);
// i= xml.indexOf("<upnp:albumArtURI>");
// k = xml.indexOf("</upnp:albumArtURI>");
// System.out.println(i+" "+k);
// if (i!=-1 && k!=-1)
// albumArtUri = xml.substring(i+18,k);
// else
albumArtUri="";
//System.out.println(albumArtUri);
/* //working code but not needed for now
i= xml.indexOf("<key>Location</key><string>");
k = xml.indexOf("</string>\t\t\t<key>File Folder Count");
System.out.println(i+" "+k);
if (i>-1 && k>-1)
streamContent = xml.substring(i+27,k);
else
*/
streamContent="";
//System.out.println(streamContent);
//java.util.Calendar;
/*
i= xml.indexOf("<key>Play Date UTC</key><date>");
System.out.println(i);
if (i>-1)
playDate = xml.substring(i+30,i+49).replace('T', ' ').replace('-', ' ');
else
playDate="";
System.out.println(playDate);
String []temp = playDate.split(" ");
System.out.println("########");
System.out.println(temp[2]+ " " + monthname[Integer.parseInt(temp[1])]+ " " + temp[0]+ " " + temp[3]);
System.out.println(playDate);
System.out.println("########");
*/
i= xml.indexOf("<key>Play Date</key><integer>");
if (i>-1)
{
playDate = xml.substring(i+29,i+39);
long offset = 2082819600;
long t = Long.parseLong(playDate) - offset;
Date d = new Date(t*1000);
//Calendar c = DateFormat.getDateTimeInstance().getCalendar();
//c.setTime(d);
// System.out.println("########");
// System.out.println(t);
// System.out.println(d);
//System.out.println(c.);//+ " " + monthname[df.MONTH_FIELD+1]+ " " + df.DATE_FIELD+ " " + (df.AM_PM_FIELD>0) != null?df.HOUR0_FIELD+12:(df.HOUR0_FIELD));
String tempy = (DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM).format(d)).replace(",", "").replace(':', ' ');
String [] temps = tempy.split(" ");
int hour = Integer.parseInt(temps[3]);
if (tempy.contains("AM"))
{
if (hour == 12)
hour = 0;
}
else if (hour >0 && hour <12)
hour += 12;
// System.out.println(tempy);
String finaltime = temps[1]+ " " + temps[0]+ " "+ temps[2]+ " "+ hour+ ":"+temps[4]+":"+temps[5];
playDate = finaltime;
System.out.println("TimeParse: "+finaltime);
//if 12 am make 0
// if 1 to 11 pm add 12
// 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
}
else
playDate = null;
TrackMetaData a = new TrackMetaData(id,parentId,resource,streamContent,albumArtUri,title,upnpClass,creator,album,albumArtist, playDate);
return a;
}
}