Source for file TriXParser.php

Documentation is available at TriXParser.php

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

Documentation generated on Fri, 13 Jan 2006 07:49:51 +0100 by phpDocumentor 1.3.0RC4