Location: PHPKode > projects > ATUIN > atuin/app/models/rdfengine/RDFParser.php
<?php

require_once 'RDFDocumentModel.php';
require_once 'Translator.php';

/**
 * this parser handles basic rss1.0 documents.
 * the parser creates a RDF document model corresponding
 * to a given RDF file (string)
 *
 * @author Michael Lessnau
 * @version 0.1
 */
class RDFParser {

   /**
    * @var $dom DOMDocument DOM document of the RDF file
    */
   private $dom;

   /**
    * @var $rdfModel RDFDocumentModel document model 
    */
   private $rdfModel;
   
   /**
    * @var $allItems RDFNodeItem[] all items within $dom 
    */
   private $allItems;

   /**
    * @var $allImages RDFNodeImages[] all images within $dom 
    */
   private $allImages;

   /**
    * constructor
    */
   public function __construct( ) {
      unset( $this->dom);
      unset( $this->rdfModel);
      unset( $this->allItems);
      unset( $this->allImages);
   }
   
   /**
    * destructor
    */
   public function __destruct( ) {
      // nop
   }

   /**
    * setting the class' attributes
    *
    * @param $name string attribute name
    * @param $value mixed attribute value
    */
   public function __set( $name, $value) {
      switch( $name) {
         case 'dom':
            $this->dom = $value;
            break;
         case 'rdfModel':
            $this->rdfModel = $value;
            break;
         case 'allItems':
            $this->allItems = $value;
            break;
         case 'allImages':
            $this->allImages = $value;
            break;
      }
   }
   
   /**
    * access the class' attributes
    *
    * @param $name string attribute name
    * @return string attribute value
    */
   public function __get( $name) {
      switch( $name) {
         case 'dom':
            return $this->dom;
         case 'rdfModel':
            return $this->rdfModel;
         case 'allItems':
            return $this->allItems;
         case 'allImages':
            return $this->allImages;
      }
   }

   /**
    * parsing the RDF file given by a string.
    *
    * @param $rdfString string RDF file content
    * @return RDFDocumentModek document model
    */
   public function parse( $rdfString) {
      $this->dom = DOMDocument::loadXML( $rdfString);
      $this->rdfModel = new RDFDocumentModel( );
      $this->allItems = array( );
      $this->allImages = array( );
      $this->processNodes( );
      return $this->rdfModel;         
   }

   /**
    * creating the whole RDF document model and
    * store it to $this->rdfModel.
    */
   private function processNodes( ) {
      // collect all items within the document
      $items = $this->dom->getElementsByTagName( 'item');
      foreach( $items as $item) {
         $newItem = new RDFNodeItem( );
         foreach( $item->attributes as $attr) {
            $newItem->rdfAbout = Translator::decode( $attr->value);
         }
         foreach( $item->childNodes as $child) {
            switch( $child->nodeName) {
               case 'dc:creator':
                  $newItem->dcCreator = Translator::decode( $child->nodeValue);
                  break;
               case 'dc:date':
                  $newItem->dcDate = Translator::decode( $child->nodeValue);
                  break;
               case 'title':
                  $newItem->title = Translator::decode( $child->nodeValue);
                  break;
               case 'link':
                  $newItem->link = Translator::decode( $child->nodeValue);
                  break;
               case 'description':
                  $newItem->description = Translator::decode( $child->nodeValue);
                  break;
            }
         }
         $this->allItems[$newItem->rdfAbout] = $newItem;
      }
      // collect all images within the document
      $images = $this->dom->getElementsByTagName( 'image');
      foreach( $images as $image) {
         foreach( $image->attributes as $attr) {
            if( strcmp( $attr->name, 'about')==0) {
               $newImage = new RDFNodeImage( );
               $newImage->rdfAbout = Translator::decode( $attr->value);
               foreach( $image->childNodes as $child) {
                  switch( $child->nodeName) {
                     case 'title':
                        $newImage->title = Translator::decode( $child->nodeValue);
                        break;
                     case 'url':
                        $newImage->url = Translator::decode( $child->nodeValue);
                        break;
                     case 'link':
                        $newImage->link = Translator::decode( $child->nodeValue);
                        break;
                  }
               }
               $this->allImages[$newImage->rdfAbout] = $newImage;
            }
         }
      }
      // create channels
      $channels = $this->dom->getElementsByTagName( 'channel');
      foreach( $channels as $c) {
         // create a new channel
         $newChannel = $this->createChannel( $c);
         // save the new channel
         $this->rdfModel->addChannel( $newChannel);
      }
   }

   /**
    * process a channel element by extracting its contents.
    *
    * @param $node DOMElementNode channel element reference
    * @return RDFNodeChannel channel object
    */
   private function createChannel( $node) {
      $channel = new RDFNodeChannel( );
      // read out "rdf:about" attribute
      foreach( $node->attributes as $attr) {
         if( $attr->name=='about' && $attr->prefix=='rdf')
            $channel->rdfAbout = Translator::decode( $attr->value);
      }
      // read element nodes
      foreach( $node->childNodes as $child) {
         switch( $child->nodeName) {
            case 'dc:source':
               $channel->dcSource = Translator::decode( $child->nodeValue);
               break;
            case 'dc:publisher':
               $channel->dcPublisher = Translator::decode( $child->nodeValue);
               break;
            case 'dc:creator':
               $channel->dcCreator = Translator::decode( $child->nodeValue);
               break;
            case 'dc:date':
               $channel->dcDate = Translator::decode( $child->nodeValue);
               break;
            case 'title':
               $channel->title = Translator::decode( $child->nodeValue);
               break;
            case 'link':
               $channel->link = Translator::decode( $child->nodeValue);
               break;
            case 'description':
               $channel->description = Translator::decode( $child->nodeValue);
               break;
            case 'image':
               foreach( $child->attributes as $attr) {
                  $id = Translator::decode( $attr->value);
                  $channel->image = $this->allImages[$id];
                  unset( $this->allImages[$id]);
               }
               break;
            case 'items':
               foreach( $child->childNodes as $c) {
                  if( strcmp( $c->nodeName, 'rdf:Seq')==0) {
                     foreach( $c->childNodes as $item) {
                        if( strcmp( $item->nodeName, 'rdf:li')==0) {
                           foreach( $item->attributes as $attr) {
                              $id = Translator::decode( $attr->value);
                              $channel->addItem( $this->allItems[$id]);
                              unset( $this->allItems[$id]);
                           }
                        }
                     }
                  }
               }
               break;
         }
      }
      return $channel;
   }   
};

?>
Return current item: ATUIN