source: Dev/branches/rest-dojo-ui/jQueryUI/server/rdfapi/syntax/NTripleSerializer.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 2.9 KB
Line 
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 *
15 * @author Daniel Westphal <mail@d-westphal.de>
16 * @version $Id: NTripleSerializer.php 268 2006-05-15 05:28:09Z tgauss $
17 * @package syntax
18 * @access public
19 **/
20
21
22
23class NTripleSerializer extends Object {
24
25  var $debug;
26  var $model;
27  var $res;
28
29 
30 
31 
32
33   /**
34    * Constructor
35    *
36    * @access   public
37    */
38  function NTripleSerializer() {
39    $this->debug=FALSE;
40  }
41 
42  /**
43   * Serializes a model to N Triple syntax.
44   *
45   * @param     object Model $model
46   * @return    string
47   * @access    public
48   */
49  function & serialize(&$m) {
50
51        if (is_a($m, 'DbModel')) $m = $m->getMemModel();
52   
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 = '<' . preg_replace("' '", "''", $s->getURI()) . '>';
67      }
68
69          $p=$t->getPredicate(); 
70          $predicate='<'.preg_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='<'.preg_replace("' '", "''", $o->getURI()).'>';};   
80               
81      $this->res.=$subject.' '.$predicate.' '.$object.' .';
82      $this->res.=LINEFEED.LINEFEED;
83    }
84   
85    return $this->res;
86  }
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
116  /* ==================== Private Methods from here ==================== */
117
118
119/**
120 * Readies this object for serializing another model
121 * @access private
122 * @param void
123 * @returns void
124 **/
125  function reset() {
126    $this->res="";
127    $this->model=NULL;
128  }
129
130}
131
132
133?>
Note: See TracBrowser for help on using the repository browser.