3 * ARC2 Turtle Serializer
5 * @author Benjamin Nowack
6 * @license http://arc.semsol.org/license
7 * @homepage <http://arc.semsol.org/>
13 ARC2::inc('RDFSerializer');
15 class ARC2_TurtleSerializer extends ARC2_RDFSerializer {
17 function __construct($a = '', &$caller) {
18 parent::__construct($a, $caller);
21 function ARC2_TurtleSerializer($a = '', &$caller) {
22 $this->__construct($a, $caller);
27 $this->content_header = 'application/x-turtle';
32 function getTerm($v, $term = '', $qualifier = '') {
34 if (preg_match('/^\_\:/', $v)) {
37 if (($term === 'p') && ($pn = $this->getPName($v))) {
42 in_array($qualifier, array('rdf:type', 'rdfs:domain', 'rdfs:range', 'rdfs:subClassOf')) &&
43 ($pn = $this->getPName($v))
47 if (preg_match('/^[a-z0-9]+\:[^\s]*$/is', $v)) {
50 return $this->getTerm(array('type' => 'literal', 'value' => $v), $term, $qualifier);
52 if (!isset($v['type']) || ($v['type'] != 'literal')) {
53 return $this->getTerm($v['value'], $term, $qualifier);
57 if (preg_match('/\"/', $v['value'])) {
59 if (preg_match('/\'/', $v['value'])) {
61 if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) {
63 $v['value'] = preg_replace("/'$/", "' ", $v['value']);
64 $v['value'] = preg_replace("/^'/", " '", $v['value']);
65 $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']);
69 if ((strlen($quot) == 1) && preg_match('/[\x0d\x0a]/', $v['value'])) {
70 $quot = $quot . $quot . $quot;
72 $suffix = isset($v['lang']) && $v['lang'] ? '@' . $v['lang'] : '';
73 $suffix = isset($v['datatype']) && $v['datatype'] ? '^^' . $this->getTerm($v['datatype'], 'dt') : $suffix;
74 return $quot . $v['value'] . $quot . $suffix;
80 foreach ($this->used_ns as $v) {
82 $r .= '@prefix ' . $this->nsp[$v] . ': <' .$v. '> .';
87 function getSerializedIndex($index, $raw = 0) {
90 foreach ($index as $s => $ps) {
91 $r .= $r ? ' .' . $nl . $nl : '';
92 $s = $this->getTerm($s, 's');
95 foreach ($ps as $p => $os) {
97 $p = $this->getTerm($p, 'p');
98 $r .= $first_p ? ' ' : ' ;' . $nl . str_pad('', strlen($s) + 1);
101 if (!is_array($os)) {/* single literal o */
102 $os = array(array('value' => $os, 'type' => 'literal'));
104 foreach ($os as $o) {
105 $r .= $first_o ? ' ' : ' ,' . $nl . str_pad('', strlen($s) + strlen($p) + 2);
106 $o = $this->getTerm($o, 'o', $p);
113 $r .= $r ? ' .' : '';
117 return $r ? $this->getHead() . $nl . $nl . $r : '';