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,v 1.10 2006/06/26 12:34:19 tgauss Exp $
* @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){
require_once(RDFAPI_INCLUDE_DIR.
PACKAGE_UTILITY);
* 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) {
if ($xml_default_namespace ==
RDF_NAMESPACE_URI) {
elseif ($xml_default_namespace ==
RDF_SCHEMA_URI) {
$nsps =
$model->getParsedNamespaces();
if(!isset
($nsps[$namespace]))
$nsps[$namespace] =
$prefix;
// define rdf prefix (qname or not)
if(isset
($nsps[RDF_NAMESPACE_URI])){
// check if model is empty
if ($model->size() ==
0) return "<".
$this->rdf_qname_prefix .
RDF_RDF .
" xmlns:rdf='".
RDF_NAMESPACE_URI.
"' />";
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");
// 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->m_out .=
LINEFEED .
']>' .
LINEFEED;
if ($model->getBaseURI() !=
NULL)
$this->m_out .=
LINEFEED.
INDENTATION .
'xml:base="'.
$model->getBaseURI().
'"';
// write namespaces declarations
$this->m_out .=
'>'.
LINEFEED;
$this->m_out .=
LINEFEED;
* 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');
$this->m_out .=
INDENTATION .
'<!ENTITY '.
$prefix .
" '" .
$namespace .
"'>".
LINEFEED;
if ($prefix ==
RDF_NAMESPACE_PREFIX &&
!$this->rdf_qnames) {
$this->m_out .=
LINEFEED .
INDENTATION .
XML_NAMESPACE_DECLARATION_PREFIX .
$this->m_out .=
LINEFEED .
INDENTATION .
XML_NAMESPACE_DECLARATION_PREFIX .
$colon_prefix =
":" .
$prefix;
$this->m_out .=
LINEFEED .
INDENTATION .
XML_NAMESPACE_DECLARATION_PREFIX
.
$colon_prefix .
'="&' .
$prefix .
';"';
$this->m_out .=
LINEFEED .
INDENTATION .
XML_NAMESPACE_DECLARATION_PREFIX
.
$colon_prefix .
'="' .
$namespace .
'"';
$subject =
$statement->getSubject();
$predicate =
$statement->getPredicate();
$object =
$statement->getobject();
// write Group and update current subject if nessesary
// classify the statement
if (($predicate->getURI() ==
RDF_NAMESPACE_URI.
RDF_TYPE) &&
is_a($object, 'Resource')) {
if (is_a($object, 'Literal')) {
if ($object->getDatatype() ==
NULL) {
$this->m_out .=
LINEFEED .
'<';
$this->m_out .=
$outerElementName;
$this->m_out .=
'/>' .
LINEFEED;
$this->m_out .=
'>' .
LINEFEED;
$this->m_out .=
$outerElementName;
$this->m_out .=
'>'.
LINEFEED;
* @param object Node $predicate
if ($statement->pred->equals($predicate))
* @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;
$this->m_out .=
LINEFEED;
$this->m_out .=
INDENTATION;
$value=
$statement->obj->getLabel();
$this->m_out .=
INDENTATION;
$predicateElementText=
$this->getElementText($statement->pred->getURI());
$this->m_out .=
$predicateElementText;
if (is_a($statement->obj, 'Resource')) {
$this->m_out .=
'/>' .
LINEFEED;
if(is_a($statement->obj, 'Literal')) {
if ($statement->obj->getDatatype()!=
NULL)
if ($statement->obj->getDatatype()==
RDF_NAMESPACE_URI .
RDF_XMLLITERAL) {
$this->m_out .=
' ' .
RDF_NAMESPACE_PREFIX .
':' .
RDF_PARSE_TYPE .
'="' .
RDF_PARSE_TYPE_LITERAL .
'"';
$this->m_out .=
' ' .
RDF_NAMESPACE_PREFIX .
':' .
RDF_DATATYPE .
'="' .
$statement->obj->getDatatype() .
'"';
if ($statement->obj->getLanguage()!=
NULL)
$this->m_out .=
' ' .
XML_NAMESPACE_PREFIX .
':' .
XML_LANG .
'="' .
$statement->obj->getLanguage() .
'"';
if ($statement->obj->getDatatype()==
RDF_NAMESPACE_URI .
RDF_XMLLITERAL) {
$this->m_out .=
$statement->obj->getLabel();
$this->m_out .=
$predicateElementText;
$this->m_out .=
'>' .
LINEFEED;
* @param Object $object_node
$rebaseURI =
$object_node->getURI();
if (is_a($object_node, 'BlankNode')) {
if (!($relativizedURI ==
$rebaseURI))
if (!is_a($object_node, '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==
"\'") {
if ($quote==
USE_ANY_QUOTE)
$quote=
($c==
'"') ?
"\'" :
"\"";
if ($whiteSpaceOnly ||
$hasBreaks)
return $quote==
USE_ANY_QUOTE ?
'"' :
$quote;
* @param object Node $node
if (is_a($node, 'Literal')) {
$value=
$node->getLabel();
if (strlen($value)<
MAX_ALLOWED_ABBREVIATED_LENGTH) {
return $c==
'"' ||
$c==
'\'';
* @param STRING $elementName
$errmsg =
RDFAPI_ERROR .
"(class: Serializer; method: getElementText): Prefix for element '" .
$elementName .
"' cannot be found.";
case RDF_NAMESPACE_PREFIX:
return $prefix.
":" .
$localName;
* @param object MemModel $model
foreach($model->triples as $key =>
$value) {
if(!is_a($value->getObject(), 'Literal'))
if ($value->pred->getURI() ==
RDF_NAMESPACE_URI.
RDF_TYPE)
(($value->pred->getURI() ==
RDF_NAMESPACE_URI.
RDFS_SUBCLASSOF) ||
($value->pred->getURI() ==
RDF_NAMESPACE_URI.
RDFS_SUBPROPERTYOF)) {
* @param object Resource $resource
Documentation generated on Mon, 26 Jun 2006 14:25:56 +0200 by phpDocumentor 1.3.0RC6