[12] | 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) return "<". $this->rdf_qname_prefix . RDF_RDF ." xmlns:rdf='".RDF_NAMESPACE_URI."' />";
|
---|
| 199 |
|
---|
| 200 | foreach($nsps as $ns => $pre){
|
---|
| 201 | $this->m_namespaces[$pre] = $ns;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | // set base URI
|
---|
| 206 | if ($model->getBaseURI()==NULL)
|
---|
| 207 | $this->m_baseURI="opaque:uri";
|
---|
| 208 | else
|
---|
| 209 | $this->m_baseURI=$model->getBaseURI();
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | if ($this->sort_model) {
|
---|
| 213 | // sort the array of statements
|
---|
| 214 |
|
---|
| 215 | foreach($model->triples as $key => $statement) {
|
---|
| 216 | $stmkey = $statement->subj->getURI() .
|
---|
| 217 | $statement->pred->getURI() .
|
---|
| 218 | (is_a($statement->obj,'Literal')?'"'.$statement->obj->getLabel().'"@'.$statement->obj->getLanguage().'^^'.$statement->obj->getDatatype():$statement->obj->getURI());
|
---|
| 219 | $this->m_statements[$stmkey] = $statement;
|
---|
| 220 | }
|
---|
| 221 | ksort($this->m_statements);
|
---|
| 222 |
|
---|
| 223 | /*
|
---|
| 224 | // Sort using the PHP usort() function. Slower :-(
|
---|
| 225 | $this->m_statements = $model->triples;
|
---|
| 226 | usort($this->m_statements, "statementsorter");
|
---|
| 227 | */
|
---|
| 228 | } else {
|
---|
| 229 | $this->m_statements = $model->triples;
|
---|
| 230 | }
|
---|
| 231 | // collects namespaces
|
---|
| 232 | $this->m_nextAutomaticPrefixIndex=0;
|
---|
| 233 | $this->collectNamespaces($model);
|
---|
| 234 |
|
---|
| 235 | // start writing the contents
|
---|
| 236 | $this->m_out="";
|
---|
| 237 | if ($this->use_xml_declaration)
|
---|
| 238 | $this->m_out .= '<?xml version="1.0" encoding="' . $encoding . '" ?>' . LINEFEED;
|
---|
| 239 | if (!HIDE_ADVERTISE)
|
---|
| 240 | $this->m_out.="<!-- Generated by RdfSerializer.php from RDF RAP.".LINEFEED."# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html !-->".LINEFEED.LINEFEED ;
|
---|
| 241 |
|
---|
| 242 | // write entitie declarations
|
---|
| 243 | if($this->use_entities) {
|
---|
| 244 | $this->m_out .= '<!DOCTYPE ' . $this->rdf_qname_prefix .
|
---|
| 245 | RDF_RDF.' [' . LINEFEED;
|
---|
| 246 | $this->writeEntityDeclarations();
|
---|
| 247 | $this->m_out .= LINEFEED . ']>' . LINEFEED;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | // start the RDF text
|
---|
| 251 | $this->m_out .= '<' . $this->rdf_qname_prefix . RDF_RDF;
|
---|
| 252 |
|
---|
| 253 | // write the xml:base
|
---|
| 254 | if ($model->getBaseURI() != NULL)
|
---|
| 255 | $this->m_out .= LINEFEED. INDENTATION .'xml:base="'.$model->getBaseURI().'"';
|
---|
| 256 |
|
---|
| 257 | // write namespaces declarations
|
---|
| 258 | $this->writeNamespaceDeclarations();
|
---|
| 259 | $this->m_out .='>'. LINEFEED;
|
---|
| 260 |
|
---|
| 261 | // write triples
|
---|
| 262 | $this->writeDescriptions();
|
---|
| 263 |
|
---|
| 264 | $this->m_out .= LINEFEED;
|
---|
| 265 | $this->m_out .='</' . $this->rdf_qname_prefix . RDF_RDF .'>';
|
---|
| 266 |
|
---|
| 267 | $this->m_namespaces=null;
|
---|
| 268 | $this->m_statements=null;
|
---|
| 269 | $this->m_currentSubject=null;
|
---|
| 270 | $this->m_groupTypeStatement=null;
|
---|
| 271 | $this->m_attributeStatements=null;
|
---|
| 272 | $this->m_contentStatements=null;
|
---|
| 273 | $this->m_rdfResourceElementText=null;
|
---|
| 274 |
|
---|
| 275 | return $this->m_out;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
| 279 | * Serializes a model and saves it into a file.
|
---|
| 280 | * Returns FALSE if the model couldn't be saved to the file.
|
---|
| 281 | *
|
---|
| 282 | * @param object MemModel $model
|
---|
| 283 | * @param String $encoding
|
---|
| 284 | * @return boolean
|
---|
| 285 | * @access public
|
---|
| 286 | */
|
---|
| 287 | function saveAs(&$model, $filename, $encoding = DEFAULT_ENCODING) {
|
---|
| 288 | // serialize model
|
---|
| 289 | $RDF = $this->serialize($model, NULL, $encoding);
|
---|
| 290 |
|
---|
| 291 | //write serialized model to file
|
---|
| 292 | $file_handle = @fopen($filename, 'w');
|
---|
| 293 | if ($file_handle) {
|
---|
| 294 | fwrite($file_handle, $RDF);
|
---|
| 295 | fclose($file_handle);
|
---|
| 296 | return TRUE;
|
---|
| 297 | } else {
|
---|
| 298 | return FALSE;
|
---|
| 299 | };
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /**
|
---|
| 303 | * @access private
|
---|
| 304 | */
|
---|
| 305 | function writeEntityDeclarations() {
|
---|
| 306 | foreach($this->m_namespaces as $prefix => $namespace) {
|
---|
| 307 | $this->m_out .= INDENTATION . '<!ENTITY '. $prefix . " '" . $namespace ."'>".LINEFEED;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /**
|
---|
| 312 | * @access private
|
---|
| 313 | */
|
---|
| 314 | function writeNamespaceDeclarations() {
|
---|
| 315 | foreach($this->m_namespaces as $prefix => $namespace) {
|
---|
| 316 |
|
---|
| 317 | if ($prefix == RDF_NAMESPACE_PREFIX && !$this->rdf_qnames) {
|
---|
| 318 |
|
---|
| 319 | if($this->use_entities) {
|
---|
| 320 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
|
---|
| 321 | '="&' . $prefix . ';"';
|
---|
| 322 | } else {
|
---|
| 323 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
|
---|
| 324 | '="' . $namespace . '"';
|
---|
| 325 | }
|
---|
| 326 | } else {
|
---|
| 327 | if ($prefix == NULL)
|
---|
| 328 | $colon_prefix = $prefix;
|
---|
| 329 | else
|
---|
| 330 | $colon_prefix = ":" .$prefix;
|
---|
| 331 |
|
---|
| 332 | if($this->use_entities) {
|
---|
| 333 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
|
---|
| 334 | .$colon_prefix .'="&' . $prefix . ';"';
|
---|
| 335 | } else {
|
---|
| 336 | $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
|
---|
| 337 | .$colon_prefix .'="' . $namespace . '"';
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * @access private
|
---|
| 345 | */
|
---|
| 346 | function writeDescriptions() {
|
---|
| 347 |
|
---|
| 348 | $this->m_groupTypeStatement = NULL;
|
---|
| 349 | $this->m_attributeStatements = array();
|
---|
| 350 | $this->m_contentStatements = array();
|
---|
| 351 | $this->m_currentSubject = NULL;
|
---|
| 352 |
|
---|
| 353 | foreach($this->m_statements as $key => $statement) {
|
---|
| 354 | $subject = $statement->getSubject();
|
---|
| 355 | $predicate = $statement->getPredicate();
|
---|
| 356 | $object = $statement->getobject();
|
---|
| 357 |
|
---|
| 358 | // write Group and update current subject if nessesary
|
---|
| 359 | if ($this->m_currentSubject==NULL || !$this->m_currentSubject->equals($subject)) {
|
---|
| 360 | $this->writeGroup();
|
---|
| 361 | $this->m_currentSubject=$subject;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | // classify the statement
|
---|
| 365 | if (($predicate->getURI() == RDF_NAMESPACE_URI.RDF_TYPE) && is_a($object, 'Resource') && !$this->m_groupTypeStatement) {
|
---|
| 366 | $this->writeGroup();
|
---|
| 367 | $this->m_groupTypeStatement = $statement;
|
---|
| 368 | }
|
---|
| 369 | elseif ($this->canAbbreviateValue($object) &&
|
---|
| 370 | $this->use_attributes &&
|
---|
| 371 | $this->checkForDoubleAttributes($predicate))
|
---|
| 372 | {
|
---|
| 373 | if (is_a($object, 'Literal')) {
|
---|
| 374 | if ($object->getDatatype() == NULL) {
|
---|
| 375 | $this->m_attributeStatements[] = $statement;
|
---|
| 376 | } else {
|
---|
| 377 | $this->m_contentStatements[] = $statement;
|
---|
| 378 | }
|
---|
| 379 | } else {
|
---|
| 380 | $this->m_attributeStatements[] = $statement;
|
---|
| 381 | }
|
---|
| 382 | } else {
|
---|
| 383 | $this->m_contentStatements[] = $statement;
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 | $this->writeGroup();
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /**
|
---|
| 390 | * @access private
|
---|
| 391 | */
|
---|
| 392 | function writeGroup() {
|
---|
| 393 | if ($this->m_currentSubject==NULL || ($this->m_groupTypeStatement==NULL && (count($this->m_attributeStatements)==0) && (count($this->m_contentStatements)==0)))
|
---|
| 394 | return;
|
---|
| 395 | if ($this->m_groupTypeStatement!=NULL)
|
---|
| 396 | $outerElementName=$this->getElementText($this->m_groupTypeStatement->obj->getURI());
|
---|
| 397 | else
|
---|
| 398 | $outerElementName = $this->rdf_qname_prefix . RDF_DESCRIPTION;
|
---|
| 399 | $this->m_out .= LINEFEED . '<';
|
---|
| 400 | $this->m_out .= $outerElementName;
|
---|
| 401 |
|
---|
| 402 | $this->m_out .= ' ';
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | $this->writeSubjectURI($this->m_currentSubject);
|
---|
| 406 |
|
---|
| 407 | // attribute Statements
|
---|
| 408 | if ($this->use_attributes)
|
---|
| 409 | $this->writeAttributeStatements();
|
---|
| 410 |
|
---|
| 411 | if (count($this->m_contentStatements)==0)
|
---|
| 412 | $this->m_out .= '/>' . LINEFEED;
|
---|
| 413 | else {
|
---|
| 414 | $this->m_out .= '>' . LINEFEED;
|
---|
| 415 |
|
---|
| 416 | // content statements
|
---|
| 417 | $this->writeContentStatements();
|
---|
| 418 |
|
---|
| 419 | $this->m_out .= '</';
|
---|
| 420 | $this->m_out .= $outerElementName;
|
---|
| 421 | $this->m_out .= '>'. LINEFEED;
|
---|
| 422 | }
|
---|
| 423 | $this->m_groupTypeStatement = NULL;
|
---|
| 424 | $this->m_attributeStatements = array();
|
---|
| 425 | $this->m_contentStatements = array();
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * @param object Node $predicate
|
---|
| 430 | * @access private
|
---|
| 431 | */
|
---|
| 432 | function checkForDoubleAttributes($predicate) {
|
---|
| 433 | foreach($this->m_attributeStatements as $key => $statement) {
|
---|
| 434 | if ($statement->pred->equals($predicate))
|
---|
| 435 | return FALSE;
|
---|
| 436 | }
|
---|
| 437 | return TRUE;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | /**
|
---|
| 441 | * @param STRING $uri
|
---|
| 442 | * @access private
|
---|
| 443 | */
|
---|
| 444 | function relativizeURI($uri) {
|
---|
| 445 | $rdfUtil=new RDFUtil();
|
---|
| 446 | $uri_namespace = $rdfUtil->guessNamespace($uri);
|
---|
| 447 | if ($uri_namespace == $this->m_baseURI) {
|
---|
| 448 | return $rdfUtil->guessName($uri);
|
---|
| 449 | } else {
|
---|
| 450 | return $uri;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | /**
|
---|
| 455 | * @param object Node $subject_node
|
---|
| 456 | *
|
---|
| 457 | * @access private
|
---|
| 458 | */
|
---|
| 459 |
|
---|
| 460 | function writeSubjectURI($subject_node) {
|
---|
| 461 | $currentSubjectURI = $subject_node->getURI();
|
---|
| 462 | $relativizedURI = $this->relativizeURI($currentSubjectURI);
|
---|
| 463 |
|
---|
| 464 | // if submitted subject ist a blank node, use rdf:nodeID
|
---|
| 465 | if (is_a($this->m_currentSubject, 'BlankNode')) {
|
---|
| 466 | $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
|
---|
| 467 | $this->m_out .= '="';
|
---|
| 468 | $this->m_out .= $relativizedURI;
|
---|
| 469 | } else {
|
---|
| 470 |
|
---|
| 471 |
|
---|
| 472 | if (!($relativizedURI == $currentSubjectURI)) {
|
---|
| 473 | $this->m_out .= $this->rdf_qname_prefix . RDF_ID;
|
---|
| 474 | $this->m_out .= '="';
|
---|
| 475 | $this->m_out .= $relativizedURI;
|
---|
| 476 | } else {
|
---|
| 477 | $this->m_out .= $this->rdf_qname_prefix . RDF_ABOUT;
|
---|
| 478 | $this->m_out .= '="';
|
---|
| 479 | $this->writeAbsoluteResourceReference($relativizedURI);
|
---|
| 480 | };
|
---|
| 481 | };
|
---|
| 482 | $this->m_out .= '"';
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /**
|
---|
| 486 | * @access private
|
---|
| 487 | */
|
---|
| 488 | function writeAttributeStatements() {
|
---|
| 489 | foreach($this->m_attributeStatements as $key => $statement) {
|
---|
| 490 | $this->m_out .= LINEFEED;
|
---|
| 491 | $this->m_out .= INDENTATION;
|
---|
| 492 | $this->m_out .= $this->getElementText($statement->pred->getURI());
|
---|
| 493 | $this->m_out .= '=';
|
---|
| 494 | $value=$statement->obj->getLabel();
|
---|
| 495 | $quote=$this->getValueQuoteType($value);
|
---|
| 496 | $this->m_out .= $quote;
|
---|
| 497 | $this->m_out .= $value;
|
---|
| 498 | $this->m_out .= $quote;
|
---|
| 499 | }
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | /**
|
---|
| 503 | * @access private
|
---|
| 504 | */
|
---|
| 505 | function writeContentStatements() {
|
---|
| 506 | foreach($this->m_contentStatements as $key => $statement) {
|
---|
| 507 | $this->m_out .= INDENTATION;
|
---|
| 508 | $this->m_out .= '<';
|
---|
| 509 | $predicateElementText=$this->getElementText($statement->pred->getURI());
|
---|
| 510 | $this->m_out .= $predicateElementText;
|
---|
| 511 |
|
---|
| 512 | if (is_a($statement->obj, 'Resource')) {
|
---|
| 513 | $this->writeResourceReference($statement->obj);
|
---|
| 514 | $this->m_out .= '/>' . LINEFEED;
|
---|
| 515 | } else {
|
---|
| 516 | if(is_a($statement->obj, 'Literal')) {
|
---|
| 517 | if ($statement->obj->getDatatype()!= NULL)
|
---|
| 518 | if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
|
---|
| 519 | $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_PARSE_TYPE . '="' . RDF_PARSE_TYPE_LITERAL . '"';
|
---|
| 520 | } else {
|
---|
| 521 | $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_DATATYPE . '="' . $statement->obj->getDatatype() . '"';
|
---|
| 522 | }
|
---|
| 523 | if ($statement->obj->getLanguage()!= NULL)
|
---|
| 524 | $this->m_out .= ' ' . XML_NAMESPACE_PREFIX . ':' . XML_LANG . '="' . $statement->obj->getLanguage() . '"';
|
---|
| 525 | };
|
---|
| 526 | $this->m_out .= '>';
|
---|
| 527 | if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
|
---|
| 528 | $this->m_out .= $statement->obj->getLabel();
|
---|
| 529 | } else {
|
---|
| 530 | $this->writeTextValue($statement->obj->getLabel());
|
---|
| 531 | }
|
---|
| 532 | $this->m_out .= '</';
|
---|
| 533 | $this->m_out .= $predicateElementText;
|
---|
| 534 | $this->m_out .= '>' . LINEFEED;
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | /**
|
---|
| 540 | * @param Object $object_node
|
---|
| 541 | * @access private
|
---|
| 542 | */
|
---|
| 543 | function writeResourceReference($object_node) {
|
---|
| 544 | $rebaseURI = $object_node->getURI();
|
---|
| 545 | $this->m_out .= ' ';
|
---|
| 546 | if (is_a($object_node, 'BlankNode')) {
|
---|
| 547 | $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
|
---|
| 548 | } else {
|
---|
| 549 | $this->m_out .= $this->rdf_qname_prefix . RDF_RESOURCE;
|
---|
| 550 | };
|
---|
| 551 |
|
---|
| 552 | $this->m_out .= '="';
|
---|
| 553 | $relativizedURI = $this->relativizeURI($rebaseURI);
|
---|
| 554 | if (!($relativizedURI == $rebaseURI))
|
---|
| 555 | if (!is_a($object_node, 'BlankNode'))
|
---|
| 556 | $this->m_out .= '#' . $relativizedURI;
|
---|
| 557 | else
|
---|
| 558 | $this->m_out .= $relativizedURI;
|
---|
| 559 | else
|
---|
| 560 | $this->writeAbsoluteResourceReference($rebaseURI);
|
---|
| 561 | $this->m_out .= '"';
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 |
|
---|
| 565 | /**
|
---|
| 566 | * @param String $rebaseURI
|
---|
| 567 | * @access private
|
---|
| 568 | */
|
---|
| 569 | function writeAbsoluteResourceReference($rebaseURI) {
|
---|
| 570 | $rdfUtil=new RDFUtil();
|
---|
| 571 | $namespace=$rdfUtil->guessNamespace($rebaseURI);
|
---|
| 572 | $localName=$rdfUtil->guessName($rebaseURI);
|
---|
| 573 | $text=$rebaseURI;
|
---|
| 574 | if ($namespace!='' and $this->use_entities) {
|
---|
| 575 | $prefix= array_search($namespace, $this->m_namespaces);
|
---|
| 576 | $text='&'.$prefix.';'.$localName;
|
---|
| 577 | } else $text = $rdfUtil->escapeValue($text);
|
---|
| 578 | $this->m_out .= $text;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | /**
|
---|
| 582 | * @param STRING $textValue
|
---|
| 583 | * @access private
|
---|
| 584 | */
|
---|
| 585 | function writeTextValue($textValue) {
|
---|
| 586 | if ($this->getValueQuoteType($textValue)==USE_CDATA)
|
---|
| 587 | $this->writeEscapedCDATA($textValue);
|
---|
| 588 | else
|
---|
| 589 | $this->m_out .= $textValue;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | /**
|
---|
| 593 | * @param STRING $textValue
|
---|
| 594 | * @access private
|
---|
| 595 | */
|
---|
| 596 | function writeEscapedCDATA($textValue) {
|
---|
| 597 | $this->m_out .= '<![CDATA[' . $textValue . ']]>';
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | /**
|
---|
| 601 | * @param STRING $textValue
|
---|
| 602 | * @access private
|
---|
| 603 | */
|
---|
| 604 | function getValueQuoteType($textValue) {
|
---|
| 605 | $quote=USE_ANY_QUOTE;
|
---|
| 606 | $hasBreaks=FALSE;
|
---|
| 607 | $whiteSpaceOnly=TRUE;
|
---|
| 608 | for ($i=0; $i<strlen($textValue); $i++) {
|
---|
| 609 | $c=$textValue{$i};
|
---|
| 610 | if ($c=='<' || $c=='>' || $c=='&')
|
---|
| 611 | return USE_CDATA;
|
---|
| 612 | if ($c==LINEFEED)
|
---|
| 613 | $hasBreaks=TRUE;
|
---|
| 614 | if ($c=='"' || $c=="\'") {
|
---|
| 615 | if ($quote==USE_ANY_QUOTE)
|
---|
| 616 | $quote=($c=='"') ? "\'" : "\"";
|
---|
| 617 | elseif ($c==$quote)
|
---|
| 618 | return USE_CDATA;
|
---|
| 619 | }
|
---|
| 620 | if (!($c == ' '))
|
---|
| 621 | $whiteSpaceOnly = FALSE;
|
---|
| 622 | }
|
---|
| 623 | if ($whiteSpaceOnly || $hasBreaks)
|
---|
| 624 | return USE_CDATA;
|
---|
| 625 | return $quote==USE_ANY_QUOTE ? '"' : $quote;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /**
|
---|
| 629 | * @param object Node $node
|
---|
| 630 | * @access private
|
---|
| 631 | */
|
---|
| 632 | function canAbbreviateValue($node) {
|
---|
| 633 | if (is_a($node, 'Literal')) {
|
---|
| 634 | $value= $node->getLabel();
|
---|
| 635 | if (strlen($value)<MAX_ALLOWED_ABBREVIATED_LENGTH) {
|
---|
| 636 | $c=$this->getValueQuoteType($value);
|
---|
| 637 | return $c=='"' || $c=='\'';
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 | return FALSE;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /**
|
---|
| 644 | * @param STRING $elementName
|
---|
| 645 | * @access private
|
---|
| 646 | */
|
---|
| 647 | function getElementText($elementName) {
|
---|
| 648 | $rdfUtil=new RDFUtil();
|
---|
| 649 | $namespace=$rdfUtil->guessNamespace($elementName);
|
---|
| 650 | $localName=$rdfUtil->guessName($elementName);
|
---|
| 651 | if ($namespace=="")
|
---|
| 652 | return $localName;
|
---|
| 653 | $prefix=array_search($namespace, $this->m_namespaces);
|
---|
| 654 |
|
---|
| 655 | if ($prefix===FALSE) {
|
---|
| 656 | $errmsg = RDFAPI_ERROR . "(class: Serializer; method: getElementText): Prefix for element '" . $elementName . "' cannot be found.";
|
---|
| 657 | trigger_error($errmsg, E_USER_ERROR);
|
---|
| 658 | }
|
---|
| 659 | switch ($prefix) {
|
---|
| 660 | case RDF_NAMESPACE_PREFIX:
|
---|
| 661 | return $this->rdf_qname_prefix . $localName;
|
---|
| 662 | case NULL:
|
---|
| 663 | return $localName;
|
---|
| 664 | default:
|
---|
| 665 | return $prefix. ":" .$localName;
|
---|
| 666 | }
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | /**
|
---|
| 670 | * @param object MemModel $model
|
---|
| 671 | * @access private
|
---|
| 672 | */
|
---|
| 673 | function collectNamespaces($model) {
|
---|
| 674 | foreach($model->triples as $key => $value) {
|
---|
| 675 |
|
---|
| 676 | if ($this->use_entities) {
|
---|
| 677 | $this->collectNamespace($value->getSubject());
|
---|
| 678 | if(!is_a($value->getObject(), 'Literal'))
|
---|
| 679 | $this->collectNamespace($value->getObject());
|
---|
| 680 |
|
---|
| 681 | } else {
|
---|
| 682 |
|
---|
| 683 | if ($value->pred->getURI() == RDF_NAMESPACE_URI.RDF_TYPE)
|
---|
| 684 | $this->collectNamespace($value->getObject());
|
---|
| 685 | elseif
|
---|
| 686 | (($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBCLASSOF) ||
|
---|
| 687 | ($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBPROPERTYOF)) {
|
---|
| 688 | $this->collectNamespace($value->getSubject());
|
---|
| 689 | $this->collectNamespace($value->getObject());
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | $this->collectNamespace($value->getPredicate());
|
---|
| 694 |
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | /**
|
---|
| 699 | * @param object Resource $resource
|
---|
| 700 | * @access private
|
---|
| 701 | */
|
---|
| 702 | function collectNamespace($resource) {
|
---|
| 703 |
|
---|
| 704 | $rdfUtil=new RDFUtil();
|
---|
| 705 | $namespace=$rdfUtil->getNamespace($resource);
|
---|
| 706 | if (!in_array($namespace, $this->m_namespaces)&&$namespace!='') {
|
---|
| 707 | $prefix = array_search( $namespace, $this->m_defaultNamespaces);
|
---|
| 708 | if ($prefix===FALSE)
|
---|
| 709 | $prefix=$this->getNextNamespacePrefix();
|
---|
| 710 | $this->m_namespaces[$prefix] = $namespace;
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | /**
|
---|
| 715 | * @access private
|
---|
| 716 | */
|
---|
| 717 | function getNextNamespacePrefix() {
|
---|
| 718 | $this->m_nextAutomaticPrefixIndex++;
|
---|
| 719 | return GENERAL_PREFIX_BASE . $this->m_nextAutomaticPrefixIndex;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | }
|
---|
| 723 | ?> |
---|