Source for file RdfSerializer.php
Documentation is available at RdfSerializer.php
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* Seralizes models to RDF syntax. It supports the xml:base, xml:lang, rdf:datatype and
* You can choose between different output syntaxes by using the configuration methods
* or changing the configuration default values in constants.php.
* This class is based on the java class edu.unika.aifb.rdf.api.syntax.RDFSerializer by Boris Motik.
* @version $Id: fsource_syntax__syntaxRdfSerializer.php.html 443 2007-06-01 16:25:38Z cax $
* @author Chris Bizer <chris@bizer.de>, Boris Motik <motik@fzi.de>, Daniel Westphal <dawe@gmx.de>, Leandro Mariano Lopez <llopez@xinergiaargentina.com>
// default serializer configuration
global $default_prefixes;
foreach($default_prefixes as $key =>
$value){
* Serializer congiguration: Sort Model
* Flag if the serializer should sort the model by subject before serializing.
* TRUE makes the RDF code more compact.
* TRUE is default. Default can be changed in constants.php.
* Serializer congiguration: Use Entities
* Flag if the serializer should use entities for URIs.
* TRUE makes the RDF code more compact.
* FALSE is default. Default can be changed in constants.php.
* Serializer congiguration: Use Attributes
* Flag if the serializer should serialize triples as XML attributes where possible.
* TRUE makes the RDF code more compact.
* FALSE is default. Default can be changed in constants.php.
* Serializer congiguration: Use Qnames
* Flag if the serializer should use qualified names for RDF reserved words.
* TRUE makes the RDF code more compact.
* TRUE is default. Default can be changed in constants.php.
* Serializer congiguration: Use XML Declaration
* Flag if the serializer should start documents with the xml declaration
* <?xml version="1.0" encoding="UTF-8" ?>.
* TRUE is default. Default can be changed in constants.php.
* Adds a new prefix/namespace combination.
* @param String $namespace
* Serializes a model to RDF syntax.
* RDF syntax can be changed by config_use_attributes($boolean), config_use_entities($boolean),
* config_sort_model($boolean).
* NOTE: There is only one default namespace allowed within an XML document.
* Therefore if SER_RDF_QNAMES in constants.php is set to FALSE and you pass
* another $xml_default_namespace as parameter, the model will be serialized
* as if SER_RDF_QNAMES were set to TRUE.
* @param object MemModel $model
* @param String $encoding
function & serialize(&$model, $xml_default_namespace =
NULL, $encoding =
DEFAULT_ENCODING) {
if ($xml_default_namespace) {
$nsps =
$model->getParsedNamespaces();
if(!isset
($nsps[$namespace]))
$nsps[$namespace] =
$prefix;
// define rdf prefix (qname or not)
// check if model is empty
foreach($nsps as $ns =>
$pre){
if ($model->getBaseURI()==
NULL)
// sort the array of statements
foreach($model->triples as $key =>
$statement) {
$stmkey =
$statement->subj->getURI() .
$statement->pred->getURI() .
(is_a($statement->obj,'Literal')?
'"'.
$statement->obj->getLabel().
'"@'.
$statement->obj->getLanguage().
'^^'.
$statement->obj->getDatatype():
$statement->obj->getURI());
// Sort using the PHP usort() function. Slower :-(
$this->m_statements = $model->triples;
usort($this->m_statements, "statementsorter");
$this->collectNamespaces($model);
// start writing the contents
$this->m_out .=
'<?xml version="1.0" encoding="' .
$encoding .
'" ?>' .
LINEFEED;
$this->m_out.=
"<!-- Generated by RdfSerializer.php from RDF RAP.".
LINEFEED.
"# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html !-->".
LINEFEED.
LINEFEED ;
// write entitie declarations
$this->writeEntityDeclarations();
if ($model->getBaseURI() !=
NULL)
// write namespaces declarations
$this->writeNamespaceDeclarations();
$this->writeDescriptions();
* Serializes a model and saves it into a file.
* Returns FALSE if the model couldn't be saved to the file.
* @param object MemModel $model
* @param String $encoding
function saveAs(&$model, $filename, $encoding =
DEFAULT_ENCODING) {
$RDF =
$this->serialize($model, NULL, $encoding);
//write serialized model to file
$file_handle =
@fopen($filename, 'w');
function writeEntityDeclarations() {
function writeNamespaceDeclarations() {
$colon_prefix =
":" .
$prefix;
.
$colon_prefix .
'="&' .
$prefix .
';"';
.
$colon_prefix .
'="' .
$namespace .
'"';
function writeDescriptions() {
$subject =
$statement->getSubject();
$predicate =
$statement->getPredicate();
$object =
$statement->getobject();
// write Group and update current subject if nessesary
// classify the statement
elseif ($this->canAbbreviateValue($object) &&
$this->checkForDoubleAttributes($predicate))
if (is_a($object, 'Literal')) {
if ($object->getDatatype() ==
NULL) {
$this->m_out .=
$outerElementName;
$this->writeAttributeStatements();
$this->writeContentStatements();
$this->m_out .=
$outerElementName;
* @param object Node $predicate
function checkForDoubleAttributes($predicate) {
if ($statement->pred->equals($predicate))
function relativizeURI($uri) {
* @param object Node $subject_node
function writeSubjectURI($subject_node) {
$currentSubjectURI =
$subject_node->getURI();
$relativizedURI =
$this->relativizeURI($currentSubjectURI);
// if submitted subject ist a blank node, use rdf:nodeID
$this->m_out .=
$relativizedURI;
if (!($relativizedURI ==
$currentSubjectURI)) {
$this->m_out .=
$relativizedURI;
$this->writeAbsoluteResourceReference($relativizedURI);
function writeAttributeStatements() {
$this->m_out .=
$this->getElementText($statement->pred->getURI());
$value=
$statement->obj->getLabel();
$quote=
$this->getValueQuoteType($value);
function writeContentStatements() {
$predicateElementText=
$this->getElementText($statement->pred->getURI());
$this->m_out .=
$predicateElementText;
if (is_a($statement->obj, 'Resource')) {
$this->writeResourceReference($statement->obj);
if(is_a($statement->obj, 'Literal')) {
if ($statement->obj->getDatatype()!=
NULL)
if ($statement->obj->getLanguage()!=
NULL)
$this->m_out .=
$statement->obj->getLabel();
$this->writeTextValue($statement->obj->getLabel());
$this->m_out .=
$predicateElementText;
* @param Object $object_node
function writeResourceReference($object_node) {
$rebaseURI =
$object_node->getURI();
if (is_a($object_node, 'BlankNode')) {
$relativizedURI =
$this->relativizeURI($rebaseURI);
if (!($relativizedURI ==
$rebaseURI))
if (!is_a($object_node, 'BlankNode'))
$this->m_out .=
'#' .
$relativizedURI;
$this->m_out .=
$relativizedURI;
$this->writeAbsoluteResourceReference($rebaseURI);
* @param String $rebaseURI
function writeAbsoluteResourceReference($rebaseURI) {
$text=
'&'.
$prefix.
';'.
$localName;
* @param STRING $textValue
function writeTextValue($textValue) {
if ($this->getValueQuoteType($textValue)==
USE_CDATA)
$this->writeEscapedCDATA($textValue);
$this->m_out .=
$textValue;
* @param STRING $textValue
function writeEscapedCDATA($textValue) {
$this->m_out .=
'<![CDATA[' .
$textValue .
']]>';
* @param STRING $textValue
function getValueQuoteType($textValue) {
for ($i=
0; $i<
strlen($textValue); $i++
) {
if ($c==
'<' ||
$c==
'>' ||
$c==
'&')
if ($c==
'"' ||
$c==
"\'") {
$quote=
($c==
'"') ?
"\'" :
"\"";
if ($whiteSpaceOnly ||
$hasBreaks)
* @param object Node $node
function canAbbreviateValue($node) {
if (is_a($node, 'Literal')) {
$value=
$node->getLabel();
$c=
$this->getValueQuoteType($value);
return $c==
'"' ||
$c==
'\'';
* @param STRING $elementName
function getElementText($elementName) {
$errmsg =
RDFAPI_ERROR .
"(class: Serializer; method: getElementText): Prefix for element '" .
$elementName .
"' cannot be found.";
return $prefix.
":" .
$localName;
* @param object MemModel $model
function collectNamespaces($model) {
foreach($model->triples as $key =>
$value) {
$this->collectNamespace($value->getSubject());
if(!is_a($value->getObject(), 'Literal'))
$this->collectNamespace($value->getObject());
$this->collectNamespace($value->getObject());
$this->collectNamespace($value->getSubject());
$this->collectNamespace($value->getObject());
$this->collectNamespace($value->getPredicate());
* @param object Resource $resource
function collectNamespace($resource) {
$prefix=
$this->getNextNamespacePrefix();
function getNextNamespacePrefix() {
Documentation generated on Fri, 1 Jun 2007 16:50:50 +0200 by phpDocumentor 1.3.2