<?
// 
// $Id: eznewsxml.php,v 1.2 2000/09/28 12:48:35 pkej-cvs Exp $
//
// Definition of eZNewsXML class and support functions.
//
// Paul K Egell-Johnsen <pkej@ez.no>
// Created on: <28-Sep-2000 13:42:10 pkej>
//

//!! eZNews
//! eZNewsXML handles XML callbacks.
/*!
    Create a child class extending this class. Create two functions in the
    child class, one called startElement the other endElement. Check out
    the global callbacks startElement and checkElement for example of how
    you use the functions in your object.
    
    Your object file also needs to have opening tags and closing tags in
    order to be usable.
    
    The startElement and endElement in this base class are abstract. The
    code inside it would have worked with the XML in example 1.
    
    This code is based on this <A href="http://www.phpbuilder.com/columns/joe20000907.php3">
    article</A> by Joe Stump.
    
    Example:
    \code
    
    // Example 1 - XML
    
    <?xml version="1.0"?>
    <ezflower>
    <product>
        <name>
            The name of the product.
        </name>
        <description>
            Information about this product.
        </description>
        <price>
            Any text over several lines.
        </price>
        <pictureid value="1"/>
        <categoryid value="4"/>        
    </product>
    </ezflower>
    \endcode
 */
 
class eZNewsXML
{
    
    /*!
        Constructs a new eZNewsCommand object. This must be called from
        base classes. ( eZNewsXML::eZNewsXML() in childs constructor )
     */
    function eZNewsXML()
    {
        global $theXMLParser;
        $theXMLParser = $this;
        
    }
    
    //!! eZNews
    //! startElement takes care of the start tags in an XML document.
    /*
        This function is abstract, but you'll find example code inside
        for how you could implement this in your own class.
     */
    function startElement( $parser, $inName, $attrs='' )
    {
        die("Take care of startElement() in a subclass of eZNewsXML,
         and make sure that you override the functions
         startElement and endElement. ");

        $this->currentTag = $inName;

        if ( $format == $openTags[$inName] )
        {
            switch( $inName )
            {
                case 'pictureid':
                    $this->parsedXMLAttributes[ $this->currentTag ]["value"] = $attrs["value"];
                    break;
                case 'categoryid':
                    $this->parsedXMLAttributes[ $this->currentTag ]["value"] = $attrs["value"];
                    break;
                default:
                    break;
            }
        }
    }

    //!! eZNews
    //! endElement takes care of the end tags in an XML document.
    /*
        This function is abstract, but you'll find example code inside
        for how you could implement this in your own class.
     */
    function endElement( $parser, $inName, $attrs='' )
    {
        die("Take care of endElement() in a subclass of eZNewsXML,
         and make sure that you override the functions
         startElement and endElement. ");

        if( $format == $closeTags[ $inName ] )
        {
            switch( $inName )
            {
                case 'ezflower':
                    $this->printPage( );
                    $this->parsedXML = '';
                    break;
                case 'product':
                    $this->printItem( );
                    $this->parsedXML = '';
                    break;
                default:
                    break;
            }
        }
    }
    
    
    //!! eZNews
    //! addToOldTag adds data from the stream to the last tag parsed.
    /*
     This function does the magic of ensuring that all character data
     is stored in the correct data element of the $temp array.
     */
    function addToOldTag( $data )
    {
        $data = utf8_decode( $data );

        $this->parsedXML[ $this->lastTag ] = $this->parsedXML[ $this->lastTag ] . $data;
        if( $data == "\n" || $data == "\r" )
        {
                $this->parsedXML[ $this->lastTag ] = $this->parsedXML[ $this->lastTag ] . "<br>\n";
        }
        if( $data == "\t"  )
        {
                $this->parsedXML[ $this->lastTag ] = $this->parsedXML[ $this->lastTag ] . "\t";
        }
    }
    

    //!! eZNews
    //! characterData stores data in the value array.
    /*
     This function takes character data and stores it in an array where
     the key is the tag name.
     */
    function characterData( $parser, $data )
    {
        if( empty( $this->currentTag ) )
        {
            $this->addToOldTag( $data );
        }
        else
        {
            $this->parsedXML[ $this->currentTag ] = utf8_decode( $data );
            $this->lastTag = $this->currentTag;
            $this->currentTag = '';
        }
    }

    /// This is used for storing XML data. The data for each tag can be found in this associative array.
    var $parsedXML = array();

    /// This is used for storing XML attributes. The attribute for each  tag can be found in this associative array.
    var $parsedXMLAttributes = array();
    
    var $currentTag;
    var $lastTag;

}
?>
