public function testGetEmptyNamespace() { EasyRdf_Namespace::set('', 'http://xmlns.com/foaf/0.1/name'); $url = EasyRdf_Namespace::get(''); $this->assertSame('http://xmlns.com/foaf/0.1/name', $url); }
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 = EasyRdf_Namespace::get($prefix)) { // Expand using well-known prefixes return $uri . $local; } } }
/** * 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 EasyRdf_Exception("Turtle Parse Error: unexpected end of file while readying value"); } if ($c != ':' && !self::isPrefixStartChar($c)) { throw new EasyRdf_Exception("Turtle Parse Error: expected a ':' or a letter, found '{$c}'"); } $namespace = NULL; if ($c == ':') { // qname using default namespace $namespace = $this->_namespaces[""]; if ($namespace == NULL) { throw new EasyRdf_Exception("Turtle Parse Error: default namespace used but not defined"); } } 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' => EasyRdf_Namespace::get('xsd') . 'boolean'); } } $this->verifyCharacter($c, ":"); if (isset($this->_namespaces[$prefix])) { $namespace = $this->_namespaces[$prefix]; } else { throw new EasyRdf_Exception("Turtle Parse Error: namespace prefix '{$prefix}' used but not defined"); } } // $c == ':', read optional local name $localName = ''; $c = $this->read(); if (self::isNameStartChar($c)) { $localName .= $c; $c = $this->read(); while (self::isNameChar($c)) { $localName .= $c; $c = $this->read(); } } // Unread last character $this->unread($c); // Note: namespace has already been resolved return array('type' => 'uri', 'value' => $namespace . $localName); }
/** * @ignore */ protected function serialisePrefixes() { $turtle = ''; foreach ($this->_prefixes as $prefix => $count) { $url = EasyRdf_Namespace::get($prefix); $turtle .= "@prefix {$prefix}: <{$url}> .\n"; } return $turtle; }
/** * Returns the cached vocabularies. * @return array of Vocabulary dataobjects */ public function getVocabularies() { if ($this->all_vocabularies === null) { // initialize cache $vocs = $this->graph->allOfType('skosmos:Vocabulary'); $this->all_vocabularies = $this->createDataObjects("Vocabulary", $vocs); foreach ($this->all_vocabularies as $voc) { // register vocabulary ids as RDF namespace prefixes $prefix = preg_replace('/\\W+/', '', $voc->getId()); // strip non-word characters try { if ($prefix != '' && EasyRdf_Namespace::get($prefix) === null) { EasyRdf_Namespace::set($prefix, $voc->getUriSpace()); } } catch (Exception $e) { // not valid as namespace identifier, ignore } } } return $this->all_vocabularies; }
public function testDeleteNamespace() { EasyRdf_Namespace::set('po', 'http://purl.org/ontology/po/'); $this->assertNotNull(EasyRdf_Namespace::get('po')); EasyRdf_Namespace::delete('po'); $this->assertNull(EasyRdf_Namespace::get('po')); }
/** * Method to serialise an EasyRdf_Graph to RDF/XML * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'rdfxml') { throw new EasyRdf_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 = ''; foreach ($graph->resources() as $resource) { $xml .= $this->rdfxmlResource($resource, true, 1); } // iterate through namepsaces array prefix and output a string. $namespaceStr = ''; foreach ($this->_prefixes as $prefix => $count) { $url = EasyRdf_Namespace::get($prefix); if (strlen($namespaceStr)) { $namespaceStr .= "\n "; } $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"'; } return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n"; }
/** * Method to serialise an EasyRdf_Graph to RDF/XML * * @param string $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); if ($format != 'rdfxml') { throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfXml does not support: {$format}"); } // store of namespaces to be appended to the rdf:RDF tag $this->_prefixes = array('rdf' => true); $xml = ''; foreach ($graph->resources() as $resource) { $properties = $resource->propertyUris(); if (count($properties) == 0) { continue; } $xml .= "\n " . $this->rdfxmlResource($resource) . "\n"; foreach ($properties as $property) { $short = EasyRdf_Namespace::shorten($property, true); if ($short) { $this->addPrefix($short); $objects = $resource->all($property); foreach ($objects as $object) { $xml .= $this->rdfxmlObject($short, $object); } } else { throw new EasyRdf_Exception("It is not possible to serialse the property " . "'{$property}' to RDF/XML."); } } $xml .= " </rdf:Description>\n"; } // iterate through namepsaces array prefix and output a string. $namespaceStr = ''; foreach ($this->_prefixes as $prefix => $count) { $url = EasyRdf_Namespace::get($prefix); if (strlen($namespaceStr)) { $namespaceStr .= "\n "; } $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"'; } return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n"; }