1 | <?php
|
---|
2 |
|
---|
3 |
|
---|
4 | // ----------------------------------------------------------------------------------
|
---|
5 | // Class: RdfSerializer
|
---|
6 | // ----------------------------------------------------------------------------------
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * An RDF seralizer.
|
---|
10 | * Seralizes models to RDF syntax. It supports the xml:base, xml:lang, rdf:datatype and
|
---|
11 | * rdf:nodeID directive.
|
---|
12 | * You can choose between different output syntaxes by using the configuration methods
|
---|
13 | * or changing the configuration default values in constants.php.
|
---|
14 | * This class is based on the java class edu.unika.aifb.rdf.api.syntax.RDFSerializer by Boris Motik.
|
---|
15 | *
|
---|
16 | * @version $Id: RdfSerializer.php 316 2006-08-24 07:55:29Z tgauss $
|
---|
17 | * @author Chris Bizer <chris@bizer.de>, Boris Motik <motik@fzi.de>, Daniel Westphal <dawe@gmx.de>, Leandro Mariano Lopez <llopez@xinergiaargentina.com>
|
---|
18 | *
|
---|
19 | * @package syntax
|
---|
20 | * @access public
|
---|
21 | *
|
---|
22 | */
|
---|
23 | class RdfSerializer extends Object {
|
---|
24 |
|
---|
25 | // configuration
|
---|
26 | var $use_entities;
|
---|
27 | var $use_attributes;
|
---|
28 | var $sort_model;
|
---|
29 | var $rdf_qnames;
|
---|
30 | var $use_xml_declaration;
|
---|
31 |
|
---|
32 | // properties
|
---|
33 | var $m_defaultNamespaces = array();
|
---|
34 | var $m_namespaces = array();
|
---|
35 | var $m_nextAutomaticPrefixIndex;
|
---|
36 | var $m_out;
|
---|
37 | var $m_baseURI;
|
---|
38 | var $m_statements = array();
|
---|
39 | var $m_currentSubject;
|
---|
40 | var $m_rdfIDElementText;
|
---|
41 | var $m_rdfAboutElementText;
|
---|
42 | var $m_rdfResourceElementText;
|
---|
43 | var $m_groupTypeStatement;
|
---|
44 | var $m_attributeStatements = array();
|
---|
45 | var $m_contentStatements = array();
|
---|
46 | var $rdf_qname_prefix;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructor
|
---|
50 | *
|
---|
51 | * @access public
|
---|
52 | */
|
---|
53 | function RdfSerializer() {
|
---|
54 |
|
---|
55 | // default serializer configuration
|
---|
56 | $this->use_entities = SER_USE_ENTITIES;
|
---|
57 | $this->use_attributes = SER_USE_ATTRIBUTES;
|
---|
58 | $this->sort_model = SER_SORT_MODEL;
|
---|
59 | $this->rdf_qnames = SER_RDF_QNAMES;
|
---|
60 | $this->use_xml_declaration = SER_XML_DECLARATION;
|
---|
61 |
|
---|
62 | global $default_prefixes;
|
---|
63 | foreach($default_prefixes as $key => $value){
|
---|
64 | $this->addNamespacePrefix($key,$value);
|
---|
65 | }
|
---|
66 |
|
---|
67 | require_once(RDFAPI_INCLUDE_DIR.PACKAGE_UTILITY);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Serializer congiguration: Sort Model
|
---|
72 | * Flag if the serializer should sort the model by subject before serializing.
|
---|
73 | * TRUE makes the RDF code more compact.
|
---|
74 | * TRUE is default. Default can be changed in constants.php.
|
---|
75 | *
|
---|
76 | * @param boolean
|
---|
77 | * @access public
|
---|
78 | */
|
---|
79 | function configSortModel($bool) {
|
---|
80 | $this->sort_model = $bool;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Serializer congiguration: Use Entities
|
---|
85 | * Flag if the serializer should use entities for URIs.
|
---|
86 | * TRUE makes the RDF code more compact.
|
---|
87 | * FALSE is default. Default can be changed in constants.php.
|
---|
88 | *
|
---|
89 | * @param boolean
|
---|
90 | * @access public
|
---|
91 | */
|
---|
92 | function configUseEntities($bool) {
|
---|
93 | $this->use_entities = $bool;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Serializer congiguration: Use Attributes
|
---|
98 | * Flag if the serializer should serialize triples as XML attributes where possible.
|
---|
99 | * TRUE makes the RDF code more compact.
|
---|
100 | * FALSE is default. Default can be changed in constants.php.
|
---|
101 | *
|
---|
102 | * @param boolean
|
---|
103 | * @access public
|
---|
104 | */
|
---|
105 | function configUseAttributes($bool) {
|
---|
106 | $this->use_attributes = $bool;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Serializer congiguration: Use Qnames
|
---|
111 | * Flag if the serializer should use qualified names for RDF reserved words.
|
---|
112 | * TRUE makes the RDF code more compact.
|
---|
113 | * TRUE is default. Default can be changed in constants.php.
|
---|
114 | *
|
---|
115 | * @param boolean
|
---|
116 | * @access public
|
---|
117 | */
|
---|
118 | function configUseQnames($bool) {
|
---|
119 | $this->rdf_qnames = $bool;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Serializer congiguration: Use XML Declaration
|
---|
124 | * Flag if the serializer should start documents with the xml declaration
|
---|
125 | * <?xml version="1.0" encoding="UTF-8" ?>.
|
---|
126 | * TRUE is default. Default can be changed in constants.php.
|
---|
127 | *
|
---|
128 | * @param boolean
|
---|
129 | * @access public
|
---|
130 | */
|
---|
131 | function configUseXmlDeclaration($bool) {
|
---|
132 | $this->use_xml_declaration = $bool;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Adds a new prefix/namespace combination.
|
---|
138 | *
|
---|
139 | * @param String $prefix
|
---|
140 | * @param String $namespace
|
---|
141 | * @access public
|
---|
142 | */
|
---|
143 | function addNamespacePrefix($prefix, $namespace) {
|
---|
144 | $this->m_defaultNamespaces[$prefix] = $namespace;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Serializes a model to RDF syntax.
|
---|
149 | * RDF syntax can be changed by config_use_attributes($boolean), config_use_entities($boolean),
|
---|
150 | * config_sort_model($boolean).
|
---|
151 | * NOTE: There is only one default namespace allowed within an XML document.
|
---|
152 | * Therefore if SER_RDF_QNAMES in constants.php is set to FALSE and you pass
|
---|
153 | * another $xml_default_namespace as parameter, the model will be serialized
|
---|
154 | * as if SER_RDF_QNAMES were set to TRUE.
|
---|
155 | *
|
---|
156 | * @param object MemModel $model
|
---|
157 | * @param String $encoding
|
---|
158 | * @return string
|
---|
159 | * @access public
|
---|
160 | */
|
---|
161 | function & serialize(&$model, $xml_default_namespace = NULL, $encoding = DEFAULT_ENCODING) {
|
---|
162 |
|
---|
163 | if ($xml_default_namespace) {
|
---|
164 |
|
---|
165 | if ($xml_default_namespace == RDF_NAMESPACE_URI) {
|
---|
166 | $this->rdf_qnames = FALSE;
|
---|
167 | unset($this->m_defaultNamespaces[RDF_NAMESPACE_PREFIX]);
|
---|
168 | }
|
---|
169 | elseif ($xml_default_namespace == RDF_SCHEMA_URI) {
|
---|
170 | unset($this->m_defaultNamespaces[RDF_SCHEMA_PREFIX]);
|
---|
171 | }
|
---|
172 | elseif (!SER_RDF_QNAMES)
|
---|
173 | $this->rdf_qnames = TRUE;
|
---|
174 |
|
---|
175 | $this->addNamespacePrefix(NULL, $xml_default_namespace);
|
---|
176 | }
|
---|
177 |
|
---|
178 | //copy parsed namespaces
|
---|
179 | $nsps = array();
|
---|
180 | $nsps = $model->getParsedNamespaces();
|
---|
181 | foreach($this->m_defaultNamespaces as $prefix => $namespace){
|
---|
182 | if(!isset ($nsps[$namespace]))
|
---|
183 | $nsps[$namespace] = $prefix;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // define rdf prefix (qname or not)
|
---|
187 | if ($this->rdf_qnames){
|
---|
188 | if(isset($nsps[RDF_NAMESPACE_URI])){
|
---|
189 | $this->rdf_qname_prefix = $nsps[RDF_NAMESPACE_URI].':';
|
---|
190 | }else{
|
---|
191 | $this->rdf_qname_prefix = RDF_NAMESPACE_PREFIX . ':';
|
---|
192 | }
|
---|
193 |
|
---|
194 | }else{
|
---|
195 | $this->rdf_qname_prefix = '';
|
---|
196 | }
|
---|
197 | // check if model is empty
|
---|
198 | if ($model->size() == 0) {
|
---|
199 | $elementStr = "<". $this->rdf_qname_prefix . RDF_RDF ." xmlns:rdf='".RDF_NAMESPACE_URI."' />";
|
---|
200 | return $elementStr;
|
---|
201 | }
|
---|
202 |
|
---|
203 | foreach($nsps as $ns => $pre){
|
---|
204 | $this->m_namespaces[$pre] = $ns;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | // set base URI
|
---|
209 | if ($model->getBaseURI()==NULL)
|
---|
210 | $this->m_baseURI="opaque:uri";
|
---|
211 | else
|
---|
212 | $this->m_baseURI=$model->getBaseURI();
|
---|
213 |
|
---|
214 |
|
---|
215 | if ($this->sort_model) {
|
---|
216 | // sort the array of statements
|
---|
217 |
|
---|
218 | foreach($model->triples as $key => $statement) {
|
---|
219 | $stmkey = $statement->subj->getURI() .
|
---|
220 | $statement->pred->getURI() .
|
---|
221 | (is_a($statement->obj,'Literal')?'"'.$statement->obj->getLabel().'"@'.$statement->obj->getLanguage().'^^'.$statement->obj->getDatatype():$statement->obj->getURI());
|
---|
222 | $this->m_statements[$stmkey] = $statement;
|
---|
223 | }
|
---|
224 | ksort($this->m_statements);
|
---|
225 |
|
---|
226 | /*
|
---|
227 | // Sort using the PHP usort() function. Slower :-(
|
---|
228 | $this->m_statements = $model->triples;
|
---|
229 | usort($this->m_statements, "statementsorter");
|
---|
230 | */
|
---|
231 | } else {
|
---|
232 | $this->m_statements = $model->triples;
|
---|
233 | }
|
---|
234 | // collects namespaces
|
---|
235 | $this->m_nextAutomaticPrefixIndex=0;
|
---|
236 | $this->collectNamespaces($model);
|
---|
237 |
|
---|
238 | // start writing the contents
|
---|
239 | $this->m_out="";
|
---|
240 | if ($this->use_xml_declaration)
|
---|
241 | $this->m_out .= '<?xml version="1.0" encoding="' . $encoding . '" ?>' . LINEFEED;
|
---|
242 | if (!HIDE_ADVERTISE)
|
---|
243 | $this->m_out.="<!-- Generated by RdfSerializer.php from RDF RAP.".LINEFEED."# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html !-->".LINEFEED.LINEFEED ;
|
---|
244 |
|
---|
245 | // write entitie declarations
|
---|
246 | if($this->use_entities) {
|
---|
247 | $this->m_out .= '<!DOCTYPE ' . $this->rdf_qname_prefix .
|
---|
248 | RDF_RDF.' [' . LINEFEED;
|
---|
249 | $this->writeEntityDeclarations();
|
---|
250 | $this->m_out .= LINEFEED . ']>' . LINEFEED;
|
---|
251 | }
|
---|
252 |
|
---|
253 | // start the RDF text
|
---|
254 | $this->m_out .= '<' . $this->rdf_qname_prefix . RDF_RDF;
|
---|
255 |
|
---|
256 | // write the xml:base
|
---|
257 | if ($model->getBaseURI() != NULL)
|
---|
258 | $this->m_out .= LINEFEED. INDENTATION .'xml:base="'.$model->getBaseURI().'"';
|
---|
259 |
|
---|
260 | // write namespaces declarations
|
---|
261 | $this->writeNamespaceDeclarations();
|
---|
262 | $this->m_out .='>'. LINEFEED;
|
---|
263 |
|
---|
264 | // write triples
|
---|
265 | $this->writeDescriptions();
|
---|
266 |
|
---|
267 | $this->m_out .= LINEFEED;
|
---|
268 | $this->m_out .='</' . $this->rdf_qname_prefix . RDF_RDF .'>';
|
---|
269 |
|
---|
270 | $this->m_namespaces=null;
|
---|
271 | $this->m_statements=null;
|
---|
272 | $this->m_currentSubject=null;
|
---|
273 | $this->m_groupTypeStatement=null;
|
---|
274 | $this->m_attributeStatements=null;
|
---|
275 | $this->m_contentStatements=null;
|
---|
276 | $this->m_rdfResourceElementText=null;
|
---|
277 |
|
---|
278 | return $this->m_out;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Serializes a model and saves it into a file.
|
---|
283 | * Returns FALSE if the model couldn't be saved to the file.
|
---|
284 | *
|
---|
285 | * @param object MemModel $model
|
---|
286 | * @param String $encoding
|
---|
287 | * @return boolean
|
---|
288 | * @access public
|
---|
289 | */
|
---|
290 | function saveAs(&$model, $filename, $encoding = DEFAULT_ENCODING) {
|
---|
291 | // serialize model
|
---|
292 | $RDF = $this->serialize($model, NULL, $encoding);
|
---|
293 |
|
---|
294 | //write serialized model to file
|
---|
295 | $file_handle = @fopen($filename, 'w');
|
---|
296 | if ($file_handle) {
|
---|
297 | fwrite($file_handle, $RDF);
|
---|
298 | fclose($file_handle);
|
---|
299 | return TRUE;
|
---|
300 | } else {
|
---|
301 | return FALSE;
|
---|
302 | };
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * @access private
|
---|
307 | */
|
---|
308 | function writeEntityDeclarations() {
|
---|
309 | foreach($this->m_namespaces as $prefix => $namespace) {
|
---|
310 | $this->m_out .= INDENTATION . '<!ENTITY '. $prefix . " '" . $namespace ."'>".LINEFEED;
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * @access private
|
---|
316 | */
|
---|
317 | function writeNamespaceDeclarations() {
|
---|
318 | foreach($this->m_namespaces as $prefix => $namespace) {
|
---|
319 |
|
---|
320 | if ($prefix == RDF_NAMESPACE_PREFIX && !$this->rdf_qnames) {
|
---|
321 |
|
---|
322 | if($this->use_entities) {
|
---|
323 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
|
---|
324 | '="&' . $prefix . ';"';
|
---|
325 | } else {
|
---|
326 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
|
---|
327 | '="' . $namespace . '"';
|
---|
328 | }
|
---|
329 | } else {
|
---|
330 | if ($prefix == NULL)
|
---|
331 | $colon_prefix = $prefix;
|
---|
332 | else
|
---|
333 | $colon_prefix = ":" .$prefix;
|
---|
334 |
|
---|
335 | if($this->use_entities) {
|
---|
336 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
|
---|
337 | .$colon_prefix .'="&' . $prefix . ';"';
|
---|
338 | } else {
|
---|
339 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
|
---|
340 | .$colon_prefix .'="' . $namespace . '"';
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * @access private
|
---|
348 | */
|
---|
349 | function writeDescriptions() {
|
---|
350 |
|
---|
351 | $this->m_groupTypeStatement = NULL;
|
---|
352 | $this->m_attributeStatements = array();
|
---|
353 | $this->m_contentStatements = array();
|
---|
354 | $this->m_currentSubject = NULL;
|
---|
355 |
|
---|
356 | foreach($this->m_statements as $key => $statement) {
|
---|
357 | $subject = $statement->getSubject();
|
---|
358 | $predicate = $statement->getPredicate();
|
---|
359 | $object = $statement->getobject();
|
---|
360 |
|
---|
361 | // write Group and update current subject if nessesary
|
---|
362 | if ($this->m_currentSubject==NULL || !$this->m_currentSubject->equals($subject)) {
|
---|
363 | $this->writeGroup();
|
---|
364 | $this->m_currentSubject=$subject;
|
---|
365 | }
|
---|
366 |
|
---|
367 | // classify the statement
|
---|
368 | if (($predicate->getURI() == RDF_NAMESPACE_URI.RDF_TYPE) && is_a($object, 'Resource') && !$this->m_groupTypeStatement) {
|
---|
369 | $this->writeGroup();
|
---|
370 | $this->m_groupTypeStatement = $statement;
|
---|
371 | }
|
---|
372 | elseif ($this->canAbbreviateValue($object) &&
|
---|
373 | $this->use_attributes &&
|
---|
374 | $this->checkForDoubleAttributes($predicate))
|
---|
375 | {
|
---|
376 | if (is_a($object, 'Literal')) {
|
---|
377 | if ($object->getDatatype() == NULL) {
|
---|
378 | $this->m_attributeStatements[] = $statement;
|
---|
379 | } else {
|
---|
380 | $this->m_contentStatements[] = $statement;
|
---|
381 | }
|
---|
382 | } else {
|
---|
383 | $this->m_attributeStatements[] = $statement;
|
---|
384 | }
|
---|
385 | } else {
|
---|
386 | $this->m_contentStatements[] = $statement;
|
---|
387 | }
|
---|
388 | }
|
---|
389 | $this->writeGroup();
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * @access private
|
---|
394 | */
|
---|
395 | function writeGroup() {
|
---|
396 | if ($this->m_currentSubject==NULL || ($this->m_groupTypeStatement==NULL && (count($this->m_attributeStatements)==0) && (count($this->m_contentStatements)==0)))
|
---|
397 | return;
|
---|
398 | if ($this->m_groupTypeStatement!=NULL)
|
---|
399 | $outerElementName=$this->getElementText($this->m_groupTypeStatement->obj->getURI());
|
---|
400 | else
|
---|
401 | $outerElementName = $this->rdf_qname_prefix . RDF_DESCRIPTION;
|
---|
402 | $this->m_out .= LINEFEED . '<';
|
---|
403 | $this->m_out .= $outerElementName;
|
---|
404 |
|
---|
405 | $this->m_out .= ' ';
|
---|
406 |
|
---|
407 |
|
---|
408 | $this->writeSubjectURI($this->m_currentSubject);
|
---|
409 |
|
---|
410 | // attribute Statements
|
---|
411 | if ($this->use_attributes)
|
---|
412 | $this->writeAttributeStatements();
|
---|
413 |
|
---|
414 | if (count($this->m_contentStatements)==0)
|
---|
415 | $this->m_out .= '/>' . LINEFEED;
|
---|
416 | else {
|
---|
417 | $this->m_out .= '>' . LINEFEED;
|
---|
418 |
|
---|
419 | // content statements
|
---|
420 | $this->writeContentStatements();
|
---|
421 |
|
---|
422 | $this->m_out .= '</';
|
---|
423 | $this->m_out .= $outerElementName;
|
---|
424 | $this->m_out .= '>'. LINEFEED;
|
---|
425 | }
|
---|
426 | $this->m_groupTypeStatement = NULL;
|
---|
427 | $this->m_attributeStatements = array();
|
---|
428 | $this->m_contentStatements = array();
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * @param object Node $predicate
|
---|
433 | * @access private
|
---|
434 | */
|
---|
435 | function checkForDoubleAttributes($predicate) {
|
---|
436 | foreach($this->m_attributeStatements as $key => $statement) {
|
---|
437 | if ($statement->pred->equals($predicate))
|
---|
438 | return FALSE;
|
---|
439 | }
|
---|
440 | return TRUE;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * @param STRING $uri
|
---|
445 | * @access private
|
---|
446 | */
|
---|
447 | function relativizeURI($uri) {
|
---|
448 | $rdfUtil=new RDFUtil();
|
---|
449 | $uri_namespace = $rdfUtil->guessNamespace($uri);
|
---|
450 | if ($uri_namespace == $this->m_baseURI) {
|
---|
451 | return $rdfUtil->guessName($uri);
|
---|
452 | } else {
|
---|
453 | return $uri;
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * @param object Node $subject_node
|
---|
459 | *
|
---|
460 | * @access private
|
---|
461 | */
|
---|
462 |
|
---|
463 | function writeSubjectURI($subject_node) {
|
---|
464 | $currentSubjectURI = $subject_node->getURI();
|
---|
465 | $relativizedURI = $this->relativizeURI($currentSubjectURI);
|
---|
466 |
|
---|
467 | // if submitted subject ist a blank node, use rdf:nodeID
|
---|
468 | if (is_a($this->m_currentSubject, 'BlankNode')) {
|
---|
469 | $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
|
---|
470 | $this->m_out .= '="';
|
---|
471 | $this->m_out .= $relativizedURI;
|
---|
472 | } else {
|
---|
473 |
|
---|
474 |
|
---|
475 | if (!($relativizedURI == $currentSubjectURI)) {
|
---|
476 | $this->m_out .= $this->rdf_qname_prefix . RDF_ID;
|
---|
477 | $this->m_out .= '="';
|
---|
478 | $this->m_out .= $relativizedURI;
|
---|
479 | } else {
|
---|
480 | $this->m_out .= $this->rdf_qname_prefix . RDF_ABOUT;
|
---|
481 | $this->m_out .= '="';
|
---|
482 | $this->writeAbsoluteResourceReference($relativizedURI);
|
---|
483 | };
|
---|
484 | };
|
---|
485 | $this->m_out .= '"';
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * @access private
|
---|
490 | */
|
---|
491 | function writeAttributeStatements() {
|
---|
492 | foreach($this->m_attributeStatements as $key => $statement) {
|
---|
493 | $this->m_out .= LINEFEED;
|
---|
494 | $this->m_out .= INDENTATION;
|
---|
495 | $this->m_out .= $this->getElementText($statement->pred->getURI());
|
---|
496 | $this->m_out .= '=';
|
---|
497 | $value=$statement->obj->getLabel();
|
---|
498 | $quote=$this->getValueQuoteType($value);
|
---|
499 | $this->m_out .= $quote;
|
---|
500 | $this->m_out .= $value;
|
---|
501 | $this->m_out .= $quote;
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * @access private
|
---|
507 | */
|
---|
508 | function writeContentStatements() {
|
---|
509 | foreach($this->m_contentStatements as $key => $statement) {
|
---|
510 | $this->m_out .= INDENTATION;
|
---|
511 | $this->m_out .= '<';
|
---|
512 | $predicateElementText=$this->getElementText($statement->pred->getURI());
|
---|
513 | $this->m_out .= $predicateElementText;
|
---|
514 |
|
---|
515 | if (is_a($statement->obj, 'Resource')) {
|
---|
516 | $this->writeResourceReference($statement->obj);
|
---|
517 | $this->m_out .= '/>' . LINEFEED;
|
---|
518 | } else {
|
---|
519 | if(is_a($statement->obj, 'Literal')) {
|
---|
520 | if ($statement->obj->getDatatype()!= NULL)
|
---|
521 | if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
|
---|
522 | $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_PARSE_TYPE . '="' . RDF_PARSE_TYPE_LITERAL . '"';
|
---|
523 | } else {
|
---|
524 | $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_DATATYPE . '="' . $statement->obj->getDatatype() . '"';
|
---|
525 | }
|
---|
526 | if ($statement->obj->getLanguage()!= NULL)
|
---|
527 | $this->m_out .= ' ' . XML_NAMESPACE_PREFIX . ':' . XML_LANG . '="' . $statement->obj->getLanguage() . '"';
|
---|
528 | };
|
---|
529 | $this->m_out .= '>';
|
---|
530 | if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
|
---|
531 | $this->m_out .= $statement->obj->getLabel();
|
---|
532 | } else {
|
---|
533 | $this->writeTextValue($statement->obj->getLabel());
|
---|
534 | }
|
---|
535 | $this->m_out .= '</';
|
---|
536 | $this->m_out .= $predicateElementText;
|
---|
537 | $this->m_out .= '>' . LINEFEED;
|
---|
538 | }
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * @param Object $object_node
|
---|
544 | * @access private
|
---|
545 | */
|
---|
546 | function writeResourceReference($object_node) {
|
---|
547 | $rebaseURI = $object_node->getURI();
|
---|
548 | $this->m_out .= ' ';
|
---|
549 | if (is_a($object_node, 'BlankNode')) {
|
---|
550 | $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
|
---|
551 | } else {
|
---|
552 | $this->m_out .= $this->rdf_qname_prefix . RDF_RESOURCE;
|
---|
553 | };
|
---|
554 |
|
---|
555 | $this->m_out .= '="';
|
---|
556 | $relativizedURI = $this->relativizeURI($rebaseURI);
|
---|
557 | if (!($relativizedURI == $rebaseURI))
|
---|
558 | if (!is_a($object_node, 'BlankNode'))
|
---|
559 | $this->m_out .= '#' . $relativizedURI;
|
---|
560 | else
|
---|
561 | $this->m_out .= $relativizedURI;
|
---|
562 | else
|
---|
563 | $this->writeAbsoluteResourceReference($rebaseURI);
|
---|
564 | $this->m_out .= '"';
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * @param String $rebaseURI
|
---|
570 | * @access private
|
---|
571 | */
|
---|
572 | function writeAbsoluteResourceReference($rebaseURI) {
|
---|
573 | $rdfUtil=new RDFUtil();
|
---|
574 | $namespace=$rdfUtil->guessNamespace($rebaseURI);
|
---|
575 | $localName=$rdfUtil->guessName($rebaseURI);
|
---|
576 | $text=$rebaseURI;
|
---|
577 | if ($namespace!='' and $this->use_entities) {
|
---|
578 | $prefix= array_search($namespace, $this->m_namespaces);
|
---|
579 | $text='&'.$prefix.';'.$localName;
|
---|
580 | } else $text = $rdfUtil->escapeValue($text);
|
---|
581 | $this->m_out .= $text;
|
---|
582 | }
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * @param STRING $textValue
|
---|
586 | * @access private
|
---|
587 | */
|
---|
588 | function writeTextValue($textValue) {
|
---|
589 | if ($this->getValueQuoteType($textValue)==USE_CDATA)
|
---|
590 | $this->writeEscapedCDATA($textValue);
|
---|
591 | else
|
---|
592 | $this->m_out .= $textValue;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /**
|
---|
596 | * @param STRING $textValue
|
---|
597 | * @access private
|
---|
598 | */
|
---|
599 | function writeEscapedCDATA($textValue) {
|
---|
600 | $this->m_out .= '<![CDATA[' . $textValue . ']]>';
|
---|
601 | }
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * @param STRING $textValue
|
---|
605 | * @access private
|
---|
606 | */
|
---|
607 | function getValueQuoteType($textValue) {
|
---|
608 | $quote=USE_ANY_QUOTE;
|
---|
609 | $hasBreaks=FALSE;
|
---|
610 | $whiteSpaceOnly=TRUE;
|
---|
611 | for ($i=0; $i<strlen($textValue); $i++) {
|
---|
612 | $c=$textValue{$i};
|
---|
613 | if ($c=='<' || $c=='>' || $c=='&')
|
---|
614 | return USE_CDATA;
|
---|
615 | if ($c==LINEFEED)
|
---|
616 | $hasBreaks=TRUE;
|
---|
617 | if ($c=='"' || $c=="\'") {
|
---|
618 | if ($quote==USE_ANY_QUOTE)
|
---|
619 | $quote=($c=='"') ? "\'" : "\"";
|
---|
620 | elseif ($c==$quote)
|
---|
621 | return USE_CDATA;
|
---|
622 | }
|
---|
623 | if (!($c == ' '))
|
---|
624 | $whiteSpaceOnly = FALSE;
|
---|
625 | }
|
---|
626 | if ($whiteSpaceOnly || $hasBreaks)
|
---|
627 | return USE_CDATA;
|
---|
628 | return $quote==USE_ANY_QUOTE ? '"' : $quote;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * @param object Node $node
|
---|
633 | * @access private
|
---|
634 | */
|
---|
635 | function canAbbreviateValue($node) {
|
---|
636 | if (is_a($node, 'Literal')) {
|
---|
637 | $value= $node->getLabel();
|
---|
638 | if (strlen($value)<MAX_ALLOWED_ABBREVIATED_LENGTH) {
|
---|
639 | $c=$this->getValueQuoteType($value);
|
---|
640 | return $c=='"' || $c=='\'';
|
---|
641 | }
|
---|
642 | }
|
---|
643 | return FALSE;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * @param STRING $elementName
|
---|
648 | * @access private
|
---|
649 | */
|
---|
650 | function getElementText($elementName) {
|
---|
651 | $rdfUtil=new RDFUtil();
|
---|
652 | $namespace=$rdfUtil->guessNamespace($elementName);
|
---|
653 | $localName=$rdfUtil->guessName($elementName);
|
---|
654 | if ($namespace=="")
|
---|
655 | return $localName;
|
---|
656 | $prefix=array_search($namespace, $this->m_namespaces);
|
---|
657 |
|
---|
658 | if ($prefix===FALSE) {
|
---|
659 | $errmsg = RDFAPI_ERROR . "(class: Serializer; method: getElementText): Prefix for element '" . $elementName . "' cannot be found.";
|
---|
660 | trigger_error($errmsg, E_USER_ERROR);
|
---|
661 | }
|
---|
662 | switch ($prefix) {
|
---|
663 | case RDF_NAMESPACE_PREFIX:
|
---|
664 | return $this->rdf_qname_prefix . $localName;
|
---|
665 | case NULL:
|
---|
666 | return $localName;
|
---|
667 | default:
|
---|
668 | return $prefix. ":" .$localName;
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * @param object MemModel $model
|
---|
674 | * @access private
|
---|
675 | */
|
---|
676 | function collectNamespaces($model) {
|
---|
677 | foreach($model->triples as $key => $value) {
|
---|
678 |
|
---|
679 | if ($this->use_entities) {
|
---|
680 | $this->collectNamespace($value->getSubject());
|
---|
681 | if(!is_a($value->getObject(), 'Literal'))
|
---|
682 | $this->collectNamespace($value->getObject());
|
---|
683 |
|
---|
684 | } else {
|
---|
685 |
|
---|
686 | if ($value->pred->getURI() == RDF_NAMESPACE_URI.RDF_TYPE)
|
---|
687 | $this->collectNamespace($value->getObject());
|
---|
688 | elseif
|
---|
689 | (($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBCLASSOF) ||
|
---|
690 | ($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBPROPERTYOF)) {
|
---|
691 | $this->collectNamespace($value->getSubject());
|
---|
692 | $this->collectNamespace($value->getObject());
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | $this->collectNamespace($value->getPredicate());
|
---|
697 |
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * @param object Resource $resource
|
---|
703 | * @access private
|
---|
704 | */
|
---|
705 | function collectNamespace($resource) {
|
---|
706 |
|
---|
707 | $rdfUtil=new RDFUtil();
|
---|
708 | $namespace=$rdfUtil->getNamespace($resource);
|
---|
709 | if (!in_array($namespace, $this->m_namespaces)&&$namespace!='') {
|
---|
710 | $prefix = array_search( $namespace, $this->m_defaultNamespaces);
|
---|
711 | if ($prefix===FALSE)
|
---|
712 | $prefix=$this->getNextNamespacePrefix();
|
---|
713 | $this->m_namespaces[$prefix] = $namespace;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * @access private
|
---|
719 | */
|
---|
720 | function getNextNamespacePrefix() {
|
---|
721 | $this->m_nextAutomaticPrefixIndex++;
|
---|
722 | return GENERAL_PREFIX_BASE . $this->m_nextAutomaticPrefixIndex;
|
---|
723 | }
|
---|
724 |
|
---|
725 | }
|
---|
726 | ?> |
---|