Source for file NTripleSerializer.php

Documentation is available at NTripleSerializer.php

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

Documentation generated on Fri, 17 Dec 2004 16:16:23 +0100 by phpDocumentor 1.3.0RC3