Source for file NTripleSerializer.php

Documentation is available at NTripleSerializer.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: NTripleSerializer
  5. // ----------------------------------------------------------------------------------
  6.  
  7.  
  8. /**
  9. * PHP N-Triple Serializer
  10. *
  11. * This class serialises models to N-Triple Syntax.
  12. *
  13. *
  14. * <b>History:</b>
  15. * <ul>
  16. * <li>11-15-2003 Initial version</li>
  17. * </ul>
  18. *
  19. *
  20. * @author Daniel Westphal <mail@d-westphal.de>
  21. * @version V0.9.3
  22. * @package syntax
  23. * @access public
  24. ***/
  25.  
  26.  
  27.  
  28. class NTripleSerializer extends Object {
  29.  
  30. var $debug;
  31. var $model;
  32. var $res;
  33.  
  34.  
  35. /**
  36. * Constructor
  37. *
  38. * @access public
  39. */
  40. function NTripleSerializer() {
  41. $this->debug=FALSE;
  42. }
  43. /**
  44. * Serializes a model to N Triple syntax.
  45. *
  46. * @param object Model $model
  47. * @return string
  48. * @access public
  49. */
  50. function & serialize(&$m) {
  51.  
  52. if (is_a($m, ''DbModel'')) $m = $m->getMemModel();
  53. $this->reset();
  54. if (!HIDE_ADVERTISE) {
  55. $this->res .= ''# Generated by NTripleSerializer.php from RDF RAP.'' .
  56. LINEFEED . ''# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html''.
  57. LINEFEED . LINEFEED;
  58. }
  59.  
  60. foreach ($m->triples as $t) {
  61.  
  62. $s=$t->getSubject();
  63. if (is_a($s, ''Blanknode'')) {
  64. $subject=''_:''.$s->getURI();
  65. } else {
  66. $subject = ''<'' . ereg_replace('' '', '''', $s->getURI()) . ''>'';
  67. }
  68.  
  69. $p=$t->getPredicate();
  70. $predicate=''<''.ereg_replace('' '', '''', $p->getURI()).''>'';
  71.  
  72. $o=$t->getObject();
  73. if (is_a($o, ''literal'')) {
  74. $object=''"''.$o->getLabel().''"'';
  75. if ($o->getLanguage()!='''') $object.=''@''.$o->getLanguage();
  76. if ($o->getDatatype()!='''') $object.=''^^<''.$o->getDatatype().">";
  77. } elseif (is_a($o, ''Blanknode'')) {
  78. $object=''_:''.$o->getURI();
  79. } else {$object=''<''.ereg_replace('' '', '''', $o->getURI()).''>'';};
  80. $this->res.=$subject.'' ''.$predicate.'' ''.$object.'' .'';
  81. $this->res.=LINEFEED.LINEFEED;
  82. }
  83. return $this->res;
  84. }
  85.  
  86. /**
  87. * Serializes a model and saves it into a file.
  88. * Returns FALSE if the model couldn''t be saved to the file.
  89. *
  90. * @access public
  91. * @param object MemModel $model
  92. * @param string $filename
  93. * @return boolean
  94. * @access public
  95. */
  96. function saveAs(&$model, $filename) {
  97.  
  98. // serialize model
  99. $n3 = $this->serialize($model);
  100.  
  101. // write serialized model to file
  102. $file_handle = @fopen($filename, ''w'');
  103. if ($file_handle) {
  104. fwrite($file_handle, $n3);
  105. fclose($file_handle);
  106. return TRUE;
  107. }else{
  108. return FALSE;
  109. };
  110. }
  111.  
  112. /* ==================== Private Methods from here ==================== */
  113.  
  114.  
  115. /**
  116. * Readies this object for serializing another model
  117. * @access private
  118. * @param void
  119. * @returns void
  120. ***/
  121. function reset() {
  122. $this->res="";
  123. $this->model=NULL;
  124. }
  125.  
  126. }
  127.  
  128.  
  129. ?>

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