Source for file Serializer.php
Documentation is available at Serializer.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.
* @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
// add default namespaces
* 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 RDF_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 RDF_SER_RDF_QNAMES were set to TRUE.
* @param object Model_Memory $model
* @param String $encoding
function &serialize(&$model, $xml_default_namespace = null ,
$encoding = RDF_DEFAULT_ENCODING )
if ($xml_default_namespace) {
// define rdf prefix (qname or not)
// check if model is empty
if ($model->size () == 0 ) {
// copy default namespaces
if ($model->getBaseURI () == null ) {
// sort the array of statements
foreach($model->triples as $statement) {
$stmkey = $statement->subj ->getURI () . $statement->pred ->getURI () . $statement->obj ->getLabel ();
// Sort using the PHP usort() function. Slower :-(
$this->m_statements = $model->triples;
usort($this->m_statements, "statementsorter");
// start writing the contents
$this->m_out .= '<?xml version="1.0" encoding="' . $encoding . '" ?>' . RDF_LINEFEED;
// write entitie declarations
if ($model->getBaseURI () != null ) {
// write namespaces declarations
* Serializes a model and saves it into a file.
* Returns FALSE if the model couldn't be saved to the file.
* @param object Model_Memory $model
* @param String $encoding
function saveAs(&$model, $filename, $encoding = RDF_DEFAULT_ENCODING )
$RDF = $this->serialize($model, null , $encoding);
// write serialized model to file
$file_handle = @fopen($filename, 'w');
$colon_prefix = ":" . $prefix;
$colon_prefix . '="&' . $prefix . ';"';
$colon_prefix . '="' . $namespace . '"';
$subject = $statement->getSubject ();
$predicate = $statement->getPredicate ();
$object = $statement->getobject ();
// write Group and update current subject if nessesary
if (PEAR ::isError ($result)) {
if (PEAR ::isError ($result)) {
// classify the statement
&& is_a($object, 'RDF_Resource')
if (is_a($object, 'RDF_Literal')) {
if ($object->getDatatype () == null ) {
if (PEAR ::isError ($outerElementName)) {
return $outerElementName;
$this->m_out .= $outerElementName;
$this->m_out .= $outerElementName;
* @param object Node $predicate
$result = $statement->pred ->equals ($predicate);
if (PEAR ::isError ($result)) {
* @param object Node $subject_node
$currentSubjectURI = $subject_node->getURI ();
// if submitted subject ist a blank node, use rdf:nodeID
$this->m_out .= $relativizedURI;
if (!($relativizedURI == $currentSubjectURI)) {
$this->m_out .= $relativizedURI;
if (PEAR ::isError ($result)) {
$value = $statement->obj ->getLabel ();
$predicateElementText = $this->getElementText($statement->pred ->getURI ());
if (PEAR ::isError ($predicateElementText)) {
return $predicateElementText;
$this->m_out .= $predicateElementText;
if (is_a($statement->obj , 'RDF_Resource')) {
if (is_a($statement->obj , 'RDF_Literal')) {
if ($statement->obj ->getDatatype () != null ) {
if ($statement->obj ->getLanguage () != null ) {
$this->m_out .= $statement->obj ->getLabel ();
$this->m_out .= $predicateElementText;
* @param Object $object_node
$rebaseURI = $object_node->getURI ();
if (is_a($object_node, 'RDF_BlankNode')) {
if (!($relativizedURI == $rebaseURI)) {
if (!is_a($object_node, 'RDF_BlankNode')) {
$this->m_out .= '#' . $relativizedURI;
$this->m_out .= $relativizedURI;
* @param String $rebaseURI
$text = '&' . $prefix . ';' . $localName;
* @param STRING $textValue
$this->m_out .= $textValue;
* @param STRING $textValue
$this->m_out .= '<![CDATA[' . $textValue . ']]>';
* @param STRING $textValue
for ($i = 0; $i < strlen($textValue); $i++ ) {
if ($c == '<' || $c == '>' || $c == '&') {
if ($c == '"' || $c == "\'") {
$quote = ($c == '"') ? "\'" : '\"';
} elseif ($c == $quote) {
if ($whiteSpaceOnly || $hasBreaks) {
* @param object Node $node
if (is_a($node, 'RDF_Literal')) {
$value = $node->getLabel ();
return $c == '"' || $c == '\'';
* @param STRING $elementName
$errmsg = " Prefix for element '$elementName' cannot be found.";
return $prefix . ':' . $localName;
* @param object Model_Memory $model
if (isset ($model->triples )) {
foreach($model->triples as $key => $value) {
if (!is_a($value->getObject (), 'RDF_Literal')) {
* @param object Resource $resource
Documentation generated on Mon, 11 Mar 2019 15:39:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|