Mercurial > hg > rr-repo
comparison sites/all/libraries/ARC2/arc/serializers/ARC2_TurtleSerializer.php @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 /** | |
3 * ARC2 Turtle Serializer | |
4 * | |
5 * @author Benjamin Nowack | |
6 * @license http://arc.semsol.org/license | |
7 * @homepage <http://arc.semsol.org/> | |
8 * @package ARC2 | |
9 * @version 2010-11-16 | |
10 */ | |
11 | |
12 ARC2::inc('RDFSerializer'); | |
13 | |
14 class ARC2_TurtleSerializer extends ARC2_RDFSerializer { | |
15 | |
16 function __construct($a, &$caller) { | |
17 parent::__construct($a, $caller); | |
18 } | |
19 | |
20 function __init() { | |
21 parent::__init(); | |
22 $this->content_header = 'application/x-turtle'; | |
23 } | |
24 | |
25 /* */ | |
26 | |
27 function getTerm($v, $term = '', $qualifier = '') { | |
28 if (!is_array($v)) { | |
29 if (preg_match('/^\_\:/', $v)) { | |
30 return $v; | |
31 } | |
32 if (($term === 'p') && ($pn = $this->getPName($v))) { | |
33 return $pn; | |
34 } | |
35 if ( | |
36 ($term === 'o') && | |
37 in_array($qualifier, array('rdf:type', 'rdfs:domain', 'rdfs:range', 'rdfs:subClassOf')) && | |
38 ($pn = $this->getPName($v)) | |
39 ) { | |
40 return $pn; | |
41 } | |
42 if (preg_match('/^[a-z0-9]+\:[^\s]*$/is' . ($this->has_pcre_unicode ? 'u' : ''), $v)) { | |
43 return '<' .$v. '>'; | |
44 } | |
45 return $this->getTerm(array('type' => 'literal', 'value' => $v), $term, $qualifier); | |
46 } | |
47 if (!isset($v['type']) || ($v['type'] != 'literal')) { | |
48 return $this->getTerm($v['value'], $term, $qualifier); | |
49 } | |
50 /* literal */ | |
51 $quot = '"'; | |
52 if (preg_match('/\"/', $v['value'])) { | |
53 $quot = "'"; | |
54 if (preg_match('/\'/', $v['value']) || preg_match('/[\x0d\x0a]/', $v['value'])) { | |
55 $quot = '"""'; | |
56 if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) { | |
57 $quot = "'''"; | |
58 $v['value'] = preg_replace("/'$/", "' ", $v['value']); | |
59 $v['value'] = preg_replace("/^'/", " '", $v['value']); | |
60 $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']); | |
61 } | |
62 } | |
63 } | |
64 if ((strlen($quot) == 1) && preg_match('/[\x0d\x0a]/', $v['value'])) { | |
65 $quot = $quot . $quot . $quot; | |
66 } | |
67 $suffix = isset($v['lang']) && $v['lang'] ? '@' . $v['lang'] : ''; | |
68 $suffix = isset($v['datatype']) && $v['datatype'] ? '^^' . $this->getTerm($v['datatype'], 'dt') : $suffix; | |
69 return $quot . $v['value'] . $quot . $suffix; | |
70 } | |
71 | |
72 function getHead() { | |
73 $r = ''; | |
74 $nl = "\n"; | |
75 foreach ($this->used_ns as $v) { | |
76 $r .= $r ? $nl : ''; | |
77 foreach ($this->ns as $prefix => $ns) { | |
78 if ($ns != $v) continue; | |
79 $r .= '@prefix ' . $prefix . ': <' .$v. '> .'; | |
80 break; | |
81 } | |
82 } | |
83 return $r; | |
84 } | |
85 | |
86 function getSerializedIndex($index, $raw = 0) { | |
87 $r = ''; | |
88 $nl = "\n"; | |
89 foreach ($index as $s => $ps) { | |
90 $r .= $r ? ' .' . $nl . $nl : ''; | |
91 $s = $this->getTerm($s, 's'); | |
92 $r .= $s; | |
93 $first_p = 1; | |
94 foreach ($ps as $p => $os) { | |
95 if (!$os) continue; | |
96 $p = $this->getTerm($p, 'p'); | |
97 $r .= $first_p ? ' ' : ' ;' . $nl . str_pad('', strlen($s) + 1); | |
98 $r .= $p; | |
99 $first_o = 1; | |
100 if (!is_array($os)) {/* single literal o */ | |
101 $os = array(array('value' => $os, 'type' => 'literal')); | |
102 } | |
103 foreach ($os as $o) { | |
104 $r .= $first_o ? ' ' : ' ,' . $nl . str_pad('', strlen($s) + strlen($p) + 2); | |
105 $o = $this->getTerm($o, 'o', $p); | |
106 $r .= $o; | |
107 $first_o = 0; | |
108 } | |
109 $first_p = 0; | |
110 } | |
111 } | |
112 $r .= $r ? ' .' : ''; | |
113 if ($raw) { | |
114 return $r; | |
115 } | |
116 return $r ? $this->getHead() . $nl . $nl . $r : ''; | |
117 } | |
118 | |
119 /* */ | |
120 | |
121 } |