Source for file TriXSerializer.php

Documentation is available at TriXSerializer.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: TriXSerializer
  4. // ----------------------------------------------------------------------------------
  5.  
  6. /**
  7. * Temporary implementation of a TriX-Serializer
  8. @version  $Id: fsource_dataset__syntaxTriXSerializer.php.html 442 2007-06-01 16:19:26Z cax $
  9. @author Daniel Westphal (http://www.d-westphal.de)
  10. *
  11. @package     dataset
  12. @access    public
  13. ***/
  14. class TriXSerializer  
  15. {
  16.     
  17.     /**
  18.     * Reference to the graphSet
  19.     *
  20.     * @var        object GraphSet 
  21.     * @access    private
  22.     */
  23.     var $graphSet;
  24.     
  25.  
  26.     /**
  27.     * Constructor
  28.     * Needs a reference to a graphSet
  29.     *
  30.     * @param    GraphSet 
  31.     * @access    public
  32.     */        
  33.     function TriXSerializer(&$graphSet)
  34.     {
  35.         $this->graphSet=&$graphSet;
  36.     }
  37.  
  38.     /**
  39.     * Serialize the dataset to a TriX string
  40.     *
  41.     * @return   string 
  42.     * @access    public
  43.     */
  44.     function serializeToString()
  45.     {
  46.         return $this->_serialize();
  47.     }
  48.     
  49.     /**
  50.     * Serialize the dataset to a TriX string and save in file
  51.     *
  52.     * @param   string 
  53.     * @access    public
  54.     */
  55.     function serializeToFile($fileName)
  56.     {
  57.         $serializedString=&$this->_serialize();
  58.         $handle fopen($fileName'w');
  59.            fwrite($handle$serializedString);
  60.         fclose($handle);
  61.     }
  62.     
  63.     
  64.     /**
  65.     * Serialize the dataset to a TriX string
  66.     *
  67.     * @return   string 
  68.     * @access    private
  69.     */
  70.     function _serialize()
  71.     {
  72.         //Trix header
  73.         $serializedString=
  74.             '<?xml version="1.0" encoding="utf-8"?>'.
  75.             '<TriX xmlns="http://www.w3.org/2004/03/trix/trix-1/">';
  76.         
  77.         //serialize defaultGraph if it is not empty
  78.         $defaultGraph=$this->graphSet->getDefaultGraph();
  79.         if ($defaultGraph->isEmpty()===false)
  80.         {
  81.             $serializedString.='<graph>';
  82.             for($iterator $this->graphSet->findInDefaultGraph(null,null,null)$iterator->valid()$iterator->next()) 
  83.             {
  84.                 $serializedString.='<triple>';
  85.                 
  86.                 $statement=$iterator->current();
  87.                 
  88.                 $serializedString.=$this->_node2string($statement->getSubject());
  89.                 $serializedString.=$this->_node2string($statement->getPredicate());
  90.                 $serializedString.=$this->_node2string($statement->getObject());
  91.                 
  92.                 $serializedString.='</triple>';
  93.             };
  94.             $serializedString.='</graph>';
  95.         }        
  96.             
  97.         //serialize namedGraphs    
  98.         foreach ($this->graphSet->listGraphNames(as $graphName)
  99.         {
  100.             $serializedString.='<graph>';
  101.             $serializedString.='<uri>'.$graphName.'</uri>';
  102.             for($iterator $this->graphSet->findInNamedGraphs(new Resource($graphName),null,null,null)$iterator->valid()$iterator->next()) 
  103.             {
  104.                 $serializedString.='<triple>';
  105.                 
  106.                 $statement=$iterator->current();
  107.                 
  108.                 $serializedString.=$this->_node2string($statement->getSubject());
  109.                 $serializedString.=$this->_node2string($statement->getPredicate());
  110.                 $serializedString.=$this->_node2string($statement->getObject());
  111.                 
  112.                 $serializedString.='</triple>';
  113.             };
  114.             $serializedString.='</graph>';
  115.         };        
  116.         //TriX footer    
  117.         $serializedString.='</TriX>';    
  118.         return $serializedString;
  119.     }
  120.     
  121.     /**
  122.     * Serialize node to a TriX string
  123.     *
  124.     * @param Node 
  125.     * @return   string 
  126.     * @access    private
  127.     */
  128.     function _node2string($node)
  129.     {
  130.         switch ($node)
  131.         {
  132.             case (is_a($node,'BlankNode')):
  133.                 return ('<id>'.$node->getLabel().'</id>');
  134.             
  135.             case (is_a($node,'Resource')):
  136.                 return ('<uri>'.$node->getLabel().'</uri>');
  137.             
  138.             case (is_a($node,'Literal')):
  139.             
  140.                 if ($node->dtype!=null)
  141.                     return ('<typedLiteral datatype="'.htmlentities($node->dtype).'">'.$node->getLabel().'</typedLiteral>');
  142.  
  143.                 if ($node->lang!=null)
  144.                     return ('<plainLiteral xml:lang="'.htmlentities($node->lang).'">'.$node->getLabel().'</plainLiteral>');
  145.                     
  146.                 return ('<plainLiteral>'.htmlentities($node->getLabel()).'</plainLiteral>');
  147.         }
  148.     }
  149. }
  150. ?>

Documentation generated on Fri, 1 Jun 2007 16:52:33 +0200 by phpDocumentor 1.3.2