Source for file TriXSerializer.php

Documentation is available at TriXSerializer.php

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

Documentation generated on Mon, 4 Jul 2005 17:14:54 +0200 by phpDocumentor 1.3.0RC3