Package net.jlastfm

Source Code of net.jlastfm.PlaylistParser

package net.jlastfm;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.jlastfm.model.LastFmPlaylist;
import net.jlastfm.model.LastFmTrack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PlaylistParser extends DefaultHandler {

  private LastFmPlaylist playlist;
  private String currentAttribute;
  private LastFmTrack currentTrack;
  private String data;
 
  private boolean playlistData;
  private boolean tracklistData;
 
 
  public LastFmPlaylist fetchPlaylist(String url) {
    this.playlist = new LastFmPlaylist();
    this.playlistData = false;
    this.tracklistData = false;
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser saxParser = factory.newSAXParser();
      saxParser.parse(url, this);
    } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return this.playlist;
  }
 

  @Override
  public void characters(char[] ch, int start, int length)
      throws SAXException {
    this.data = new String(ch, start, length);
  }

  @Override
  public void endElement(String uri, String localName, String name)
      throws SAXException {
    try {
      if (this.playlistData) {
        if (name.equals("title")) {
          this.playlist.setTitle(URLDecoder.decode(this.data, "UTF-8"));
        }
      } else if (this.tracklistData) {
        if (name.equals("track")) {
          this.playlist.addTrack(this.currentTrack);
        } else if (name.equals("location")) {
          this.currentTrack.setLocation(URLDecoder.decode(this.data, "UTF-8"));
        } else if (name.equals("title")) {         
            this.currentTrack.setTitle(URLDecoder.decode(this.data, "UTF-8"));         
        } else if (name.equals("id")) {
          this.currentTrack.setId(Integer.parseInt(this.data));
        } else if (name.equals("album")) {
          this.currentTrack.setAlbum(URLDecoder.decode(this.data, "UTF-8"));
        } else if (name.equals("creator")) {
          this.currentTrack.setCreator(URLDecoder.decode(this.data, "UTF-8"));
        } else if (name.equals("duration")) {
          this.currentTrack.setDuration(Integer.parseInt(this.data));
        } else if (name.equals("image")) {
          this.currentTrack.setImage(URLDecoder.decode(this.data, "UTF-8"));
        } else if (name.equals("lastfm:trackauth")) {
          this.currentTrack.setLastFmTrackAuth(this.data);
        } else if (name.equals("lastfm:albumId")) {
          this.currentTrack.setLastFmAlbumId(this.data);
        } else if (name.equals("lastfm:artistId")) {
          this.currentTrack.setLastFmartistId(this.data);
        } else if (name.equals("link")) {
          if (this.currentAttribute.equals("http://www.last.fm/buyAlbumURL")) {
            this.currentTrack.setLastFmBuyAlbumURL(URLDecoder.decode(this.data, "UTF-8"));
          } else if (this.currentAttribute.equals("http://www.last.fm/buyTrackURL")) {
            this.currentTrack.setLastFmBuyTrackURL(URLDecoder.decode(this.data, "UTF-8"));
          } else if (this.currentAttribute.equals("http://www.last.fm/trackpage")) {
            this.currentTrack.setLastFmTrackPage(URLDecoder.decode(this.data, "UTF-8"));
          } else if (this.currentAttribute.equals("http://www.last.fm/albumpage")) {
            this.currentTrack.setLastFmAlbumPage(URLDecoder.decode(this.data, "UTF-8"));
          } else if (this.currentAttribute.equals("http://www.last.fm/artistpage")) {
            this.currentTrack.setLastFmArtistPage(URLDecoder.decode(this.data, "UTF-8"));
          }
         
        }
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }   
  }

  @Override
  public void startElement(String uri, String localName, String name,
      Attributes attributes) throws SAXException {

    if (name.equals("playlist")) {
      this.playlistData = true;
    }
    else if (name.equals("trackList")) {
      this.tracklistData = true;
      this.playlistData = false;
    }
    else if (this.tracklistData && name.equals("track")) {
      this.currentTrack = new LastFmTrack();
    }
    else if (this.tracklistData && name.equals("link")) {
      this.currentAttribute = attributes.getValue("rel");
    }
  } 
 
}
TOP

Related Classes of net.jlastfm.PlaylistParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.