<?php
/*

 Copyright (c) 2001 - 2007 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

class LastFmXml {

    
// Settings
    
private $_grabtags = array('artist','album','track');
    private 
$_subtags = array('name','reach','url','artist','chartposition','mbid');
    private 
$base_file ""


    public 
$results=array();  // Array of results
    
private $_parser;   // The XML parser
    
private $_subTag// Tracking the sub tag
    
private $_currentTag// Tracking the tag
    
private $_counter 0// Keep track of which element we are on 
   
       /**
     * Constructor, nothing to do here! 
     */
    
public function __construct() {

        
// Rien a faire
    
    
// Constructor
    
    /**
     * create_parser
     * this sets up an XML Parser that we can use to parse the XML
     * document we recieve
     */
    
public function create_parser() { 
                
$this->_parser xml_parser_create();

                
xml_parser_set_option($this->_parserXML_OPTION_CASE_FOLDINGfalse);
        
                
xml_set_object($this->_parser$this);

                
xml_set_element_handler($this->_parser'start_element''end_element');

                
xml_set_character_data_handler($this->_parser'cdata');

    } 
// create_parser
    
    /**
     * run_parse
     * This takes a filename optionally (otherwise uses base_file set at the top of this class)
     * and reads a LastFM xml document into a named array
     */
    
public function run_parse($file='') {

        
/* Create the Parser */
        
$this->create_parser();
        
$this->results = array(); 

        
// File we want to pull from
        
$file $file $file $this->$base_file
    
        
$contents file_get_contents($file); 
    
        
xml_parse($this->_parser$contents);
        
xml_parser_free($this->_parser);
    
    } 
// run_parse
    
    /**
     * start_element
     * Helper function for when we see an element
     */
    
function start_element($parser$tag$attributes) { 

        if (
in_array($tag,$this->_grabtags) AND !$this->_currentTag) { 
            
$this->_currentTag $tag;
        }
        if (
in_array($tag,$this->_subtags) AND $this->_currentTag) {
            
$this->_subTag $tag;
        } 

    } 
// start_element
    
    /**
     * cdata
     * Helper function for when we see the content of a tag
     */
    
function cdata($parser$cdata) {

    if (!
$this->_currentTag || !$this->_subTag || !trim($cdata)) { return false; } 

    
$tag     $this->_currentTag;
    
$subtag $this->_subTag;

    
$this->results[$this->_counter][$tag][$subtag] = trim($cdata);

    } 
// cdata
    
    /**
     * end_element
     * For when an element is closed
     */
    
function end_element($parser$tag) {

        
// If we're closing the current tag
        
if ($tag == $this->_currentTag) { 
            unset(
$this->_currentTag,$this->_subTag); 
            
$this->_counter++; 
        }
        
// If we're closing a subtag and have a currentTag
        
if (in_array($tag,$this->_subtags) AND $this->_currentTag) { 
            unset(
$this->_subTag); 
        } 

    } 
// end_element

// end LastFmXml

?>