Beispiel #1
0
 /**
  * Method to serialise an EasyRdf\Graph to RDF/XML
  *
  * @param Graph  $graph  An EasyRdf\Graph object.
  * @param string $format The name of the format to convert to.
  * @param array  $options
  *
  * @return string The RDF in the new desired format.
  * @throws Exception
  */
 public function serialise(Graph $graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($format);
     if ($format != 'rdfxml') {
         throw new Exception("EasyRdf\\Serialiser\\RdfXml does not support: {$format}");
     }
     // store of namespaces to be appended to the rdf:RDF tag
     $this->prefixes = array('rdf' => true);
     // store of the resource URIs we have serialised
     $this->outputtedResources = array();
     $xml = '';
     // Serialise URIs first
     foreach ($graph->resources() as $resource) {
         if (!$resource->isBnode()) {
             $xml .= $this->rdfxmlResource($resource, true);
         }
     }
     // Serialise bnodes afterwards
     foreach ($graph->resources() as $resource) {
         if ($resource->isBnode()) {
             $xml .= $this->rdfxmlResource($resource, true);
         }
     }
     // iterate through namepsaces array prefix and output a string.
     $namespaceStr = '';
     foreach ($this->prefixes as $prefix => $count) {
         $url = RdfNamespace::get($prefix);
         if (strlen($namespaceStr)) {
             $namespaceStr .= "\n        ";
         }
         if (strlen($prefix) === 0) {
             $namespaceStr .= ' xmlns="' . htmlspecialchars($url) . '"';
         } else {
             $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"';
         }
     }
     return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n";
 }
Beispiel #2
0
 protected function expandCurie($node, &$context, $value)
 {
     if (preg_match('/^(\\w*?):(.*)$/', $value, $matches)) {
         list(, $prefix, $local) = $matches;
         $prefix = strtolower($prefix);
         if ($prefix === '_') {
             // It is a bnode
             return $this->remapBnode(substr($value, 2));
         } elseif (empty($prefix) and $context['vocab']) {
             // Empty prefix
             return $context['vocab'] . $local;
         } elseif (isset($context['prefixes'][$prefix])) {
             return $context['prefixes'][$prefix] . $local;
         } elseif ($uri = $node->lookupNamespaceURI($prefix)) {
             return $uri . $local;
         } elseif (!empty($prefix) and $uri = RdfNamespace::get($prefix)) {
             // Expand using well-known prefixes
             return $uri . $local;
         }
     }
 }
Beispiel #3
0
 /**
  * Parses qnames and boolean values, which have equivalent starting
  * characters.
  * @ignore
  */
 protected function parseQNameOrBoolean()
 {
     // First character should be a ':' or a letter
     $c = $this->read();
     if ($c == -1) {
         throw new Exception("Turtle Parse Error: unexpected end of file while readying value", $this->line, $this->column);
     }
     if ($c != ':' && !self::isPrefixStartChar($c)) {
         throw new Exception("Turtle Parse Error: expected a ':' or a letter, found '{$c}'", $this->line, $this->column);
     }
     $namespace = null;
     if ($c == ':') {
         // qname using default namespace
         if (isset($this->namespaces[''])) {
             $namespace = $this->namespaces[''];
         } else {
             throw new Exception("Turtle Parse Error: default namespace used but not defined", $this->line, $this->column);
         }
     } else {
         // $c is the first letter of the prefix
         $prefix = $c;
         $c = $this->read();
         while (self::isPrefixChar($c)) {
             $prefix .= $c;
             $c = $this->read();
         }
         if ($c != ':') {
             // prefix may actually be a boolean value
             $value = $prefix;
             if ($value == "true" || $value == "false") {
                 return array('type' => 'literal', 'value' => $value, 'datatype' => RdfNamespace::get('xsd') . 'boolean');
             }
         }
         $this->verifyCharacterOrFail($c, ":");
         if (isset($this->namespaces[$prefix])) {
             $namespace = $this->namespaces[$prefix];
         } else {
             throw new Exception("Turtle Parse Error: namespace prefix '{$prefix}' used but not defined", $this->line, $this->column);
         }
     }
     // $c == ':', read optional local name
     $localName = '';
     $c = $this->read();
     if (self::isNameStartChar($c)) {
         if ($c == '\\') {
             $localName .= $this->readLocalEscapedChar();
         } else {
             $localName .= $c;
         }
         $c = $this->read();
         while (self::isNameChar($c)) {
             if ($c == '\\') {
                 $localName .= $this->readLocalEscapedChar();
             } else {
                 $localName .= $c;
             }
             $c = $this->read();
         }
     }
     // Unread last character
     $this->unread($c);
     // Note: namespace has already been resolved
     return array('type' => 'uri', 'value' => $namespace . $localName);
 }
Beispiel #4
0
 /**
  * @ignore
  */
 protected function serialisePrefixes()
 {
     $turtle = '';
     foreach ($this->prefixes as $prefix => $count) {
         $url = RdfNamespace::get($prefix);
         $turtle .= "@prefix {$prefix}: <{$url}> .\n";
     }
     return $turtle;
 }
 /**
  * @param $prefix
  *
  * @return string
  */
 public function get($prefix)
 {
     return RdfNamespace::get($prefix);
 }