Source for file TriXParser.php

Documentation is available at TriXParser.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: TriXParser
  4. // ----------------------------------------------------------------------------------
  5.  
  6.  
  7.  
  8. /**
  9. * Temporary implementation of a TriX-Parser (Usable only with PHP > V5)
  10. * Currently, it doesn't support any namespaces and has problems with typed literals.
  11. * So this parser only works with TRIX documents where the default namespace is the TRIX namespace.
  12. *
  13. * <BR><BR>History:<UL>
  14. * <LI>05-07-2005 : First version of this class.</LI>
  15. *
  16. * @version V0.9
  17. * @author Daniel Westphal (http://www.d-westphal.de)
  18. *
  19. * @package dataset
  20. * @access public
  21. ***/
  22. class TriXParser
  23. {
  24. /**
  25. * Reference to the graphSet
  26. *
  27. * @var GraphSet
  28. * @access private
  29. */
  30. var $graphSet;
  31. /**
  32. * Constructor
  33. * Needs a reference to a graphSet
  34. *
  35. * @param GraphSet
  36. * @access public
  37. */
  38. function TriXParser(&$graphSet)
  39. {
  40. $this->graphSet=&$graphSet;
  41. }
  42.  
  43. /**
  44. * Parse an XML string
  45. *
  46. * @param string
  47. * @access public
  48. */
  49. function parseString($string)
  50. {
  51. $this->_populateGraphSet(simplexml_load_string($string));
  52. }
  53. /**
  54. * Parse from a file
  55. *
  56. * @param string
  57. * @access public
  58. */
  59. function parseFile($pathToFile)
  60. {
  61. $this->_populateGraphSet(simplexml_load_file($pathToFile));
  62. }
  63. /**
  64. * Populates the graphSet with namedGraphs and triples.
  65. *
  66. * @param object simpleXMLModel $xmlModel
  67. * @access private
  68. */
  69. function _populateGraphSet(&$xmlModel)
  70. {
  71. $defaultGraphOccurred=false;
  72. foreach ($xmlModel->graph as $graph)
  73. {
  74. if (isset($graph->uri))
  75. {
  76. $graphName=(string)$graph->uri;
  77. $namedGraph=& $this->graphSet->getNamedGraph($graphName);
  78. if ($namedGraph ==null)
  79. $namedGraph=& $this->graphSet->createGraph($graphName);
  80. } else
  81. {
  82. if ($defaultGraphOccurred)
  83. trigger_error('Only one unnamed Graph per file allowed', E_USER_ERROR);
  84. $namedGraph=& $this->graphSet->getDefaultGraph();
  85. $defaultGraphOccurred=true;
  86. }
  87. foreach ($graph->triple as $triple)
  88. {
  89. $tripleCount=0;
  90. $tripleArray=array();
  91. foreach ($triple->children() as $tag => $value)
  92. {
  93. $tripleArray[$tripleCount++]=$this->_element2Resource((string)$tag,$value);
  94. };
  95. $namedGraph->add(new Statement($tripleArray[0],$tripleArray[1],$tripleArray[2]));
  96. };
  97. };
  98. }
  99. /**
  100. * return a mathing resource tyoe
  101. *
  102. * @param string
  103. * @param object simpleXMLNode $value
  104. * @access private
  105. */
  106. function _element2Resource($tag,$value)
  107. {
  108. switch ($tag)
  109. {
  110. case 'uri':
  111. return new Resource((string)$value);
  112. break;
  113. case 'id':
  114. return new BlankNode((string)$value);
  115. break;
  116. case 'typedLiteral':
  117. $literal=new Literal((string)$value);
  118. $literal->setDatatype((string)$value['datatype']);
  119. return $literal;
  120. break;
  121. case 'plainLiteral':
  122. $literal=new Literal((string)$value);
  123. if(isset($value['xml:lang']))
  124. $literal->setLanguage((string)$value['xml:lang']);
  125. return $literal;
  126. break;
  127. }
  128. }
  129. }
  130. ?>

Documentation generated on Thu, 7 Jul 2005 13:43:59 +0200 by phpDocumentor 1.3.0RC3