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
  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. foreach ($xmlModel->graph as $graph)
  72. {
  73. if (isset($graph->uri))
  74. {
  75. $graphName=(string)$graph->uri;
  76. } else
  77. {
  78. $graphName="urn:" . (string)uniqid();
  79. }
  80. $namedGraph=&$this->graphSet->createGraph($graphName);
  81. foreach ($graph->triple as $triple)
  82. {
  83. $tripleCount=0;
  84. $tripleArray=array();
  85. foreach ($triple->children() as $tag => $value)
  86. {
  87. $tripleArray[$tripleCount++]=$this->_element2Resource((string)$tag,$value);
  88. };
  89. $namedGraph->add(new Statement($tripleArray[0],$tripleArray[1],$tripleArray[2]));
  90. };
  91. };
  92. }
  93. /**
  94. * return a mathing resource tyoe
  95. *
  96. * @param string
  97. * @param object simpleXMLNode $value
  98. * @access private
  99. */
  100. function _element2Resource($tag,$value)
  101. {
  102. switch ($tag)
  103. {
  104. case 'uri':
  105. return new Resource((string)$value);
  106. break;
  107. case 'id':
  108. return new BlankNode((string)$value);
  109. break;
  110. case 'typedLiteral':
  111. $literal=new Literal((string)$value);
  112. $literal->setDatatype((string)$value['datatype']);
  113. return $literal;
  114. break;
  115. case 'plainLiteral':
  116. $literal=new Literal((string)$value);
  117. if(isset($value['xml:lang']))
  118. $literal->setLanguage((string)$value['xml:lang']);
  119. return $literal;
  120. break;
  121. }
  122. }
  123. }
  124. ?>

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