View source
<?php
class EasyRdf_Format {
private static $formats = array();
private $name = array();
private $label = null;
private $uri = null;
private $mimeTypes = array();
private $extensions = array();
private $parserClass = null;
private $serialiserClass = null;
public static function getNames() {
return array_keys(self::$formats);
}
public static function getFormats() {
return self::$formats;
}
public static function getHttpAcceptHeader($extraTypes = array()) {
$accept = $extraTypes;
foreach (self::$formats as $format) {
if ($format->parserClass and count($format->mimeTypes) > 0) {
$accept = array_merge($accept, $format->mimeTypes);
}
}
arsort($accept, SORT_NUMERIC);
$acceptStr = '';
foreach ($accept as $type => $q) {
if ($acceptStr) {
$acceptStr .= ',';
}
if ($q == 1.0) {
$acceptStr .= $type;
}
else {
$acceptStr .= sprintf("%s;q=%1.1F", $type, $q);
}
}
return $acceptStr;
}
public static function formatExists($name) {
return array_key_exists($name, self::$formats);
}
public static function getFormat($query) {
if (!is_string($query) or $query == null or $query == '') {
throw new InvalidArgumentException("\$query should be a string and cannot be null or empty");
}
foreach (self::$formats as $format) {
if ($query == $format->name or $query == $format->uri or array_key_exists($query, $format->mimeTypes) or in_array($query, $format->extensions)) {
return $format;
}
}
throw new EasyRdf_Exception("Format is not recognised: {$query}");
}
public static function register($name, $label = null, $uri = null, $mimeTypes = array(), $extensions = array()) {
if (!is_string($name) or $name == null or $name == '') {
throw new InvalidArgumentException("\$name should be a string and cannot be null or empty");
}
if (!array_key_exists($name, self::$formats)) {
self::$formats[$name] = new EasyRdf_Format($name);
}
self::$formats[$name]
->setLabel($label);
self::$formats[$name]
->setUri($uri);
self::$formats[$name]
->setMimeTypes($mimeTypes);
self::$formats[$name]
->setExtensions($extensions);
return self::$formats[$name];
}
public static function unregister($name) {
unset(self::$formats[$name]);
}
public static function registerParser($name, $class) {
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)
->setParserClass($class);
}
public static function registerSerialiser($name, $class) {
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)
->setSerialiserClass($class);
}
public static function guessFormat($data, $filename = null) {
if (is_array($data)) {
return self::getFormat('php');
}
if ($filename and preg_match('/\\.(\\w+)$/', $filename, $matches)) {
foreach (self::$formats as $format) {
if (in_array($matches[1], $format->extensions)) {
return $format;
}
}
}
$short = substr($data, 0, 1024);
if (preg_match('/^\\s*\\{/', $short)) {
return self::getFormat('json');
}
elseif (preg_match('/<rdf:/i', $short)) {
return self::getFormat('rdfxml');
}
elseif (preg_match('|http://www.w3.org/2005/sparql-results|', $short)) {
return self::getFormat('sparql-xml');
}
elseif (preg_match('/\\WRDFa\\W/i', $short)) {
return self::getFormat('rdfa');
}
elseif (preg_match('/<!DOCTYPE html|<html/i', $short)) {
return self::getFormat('rdfa');
}
elseif (preg_match('/@prefix\\s|@base\\s/', $short)) {
return self::getFormat('turtle');
}
elseif (preg_match('/^\\s*<.+> <.+>/m', $short)) {
return self::getFormat('ntriples');
}
else {
return null;
}
}
public function __construct($name) {
$this->name = $name;
$this->label = $name;
}
public function getName() {
return $this->name;
}
public function getLabel() {
return $this->label;
}
public function setLabel($label) {
if ($label) {
if (!is_string($label)) {
throw new InvalidArgumentException("\$label should be a string");
}
return $this->label = $label;
}
else {
return $this->label = null;
}
}
public function getUri() {
return $this->uri;
}
public function setUri($uri) {
if ($uri) {
if (!is_string($uri)) {
throw new InvalidArgumentException("\$uri should be a string");
}
return $this->uri = $uri;
}
else {
return $this->uri = null;
}
}
public function getDefaultMimeType() {
$types = array_keys($this->mimeTypes);
if (isset($types[0])) {
return $types[0];
}
}
public function getMimeTypes() {
return $this->mimeTypes;
}
public function setMimeTypes($mimeTypes) {
if ($mimeTypes) {
if (!is_array($mimeTypes)) {
$mimeTypes = array(
$mimeTypes,
);
}
$this->mimeTypes = $mimeTypes;
}
else {
$this->mimeTypes = array();
}
}
public function getDefaultExtension() {
if (isset($this->extensions[0])) {
return $this->extensions[0];
}
}
public function getExtensions() {
return $this->extensions;
}
public function setExtensions($extensions) {
if ($extensions) {
if (!is_array($extensions)) {
$extensions = array(
$extensions,
);
}
$this->extensions = $extensions;
}
else {
$this->extensions = array();
}
}
public function setParserClass($class) {
if ($class) {
if (!is_string($class)) {
throw new InvalidArgumentException("\$class should be a string");
}
$this->parserClass = $class;
}
else {
$this->parserClass = null;
}
}
public function getParserClass() {
return $this->parserClass;
}
public function newParser() {
$parserClass = $this->parserClass;
if (!$parserClass) {
throw new EasyRdf_Exception("No parser class available for format: " . $this
->getName());
}
return new $parserClass();
}
public function setSerialiserClass($class) {
if ($class) {
if (!is_string($class)) {
throw new InvalidArgumentException("\$class should be a string");
}
$this->serialiserClass = $class;
}
else {
$this->serialiserClass = null;
}
}
public function getSerialiserClass() {
return $this->serialiserClass;
}
public function newSerialiser() {
$serialiserClass = $this->serialiserClass;
if (!$serialiserClass) {
throw new EasyRdf_Exception("No serialiser class available for format: " . $this
->getName());
}
return new $serialiserClass();
}
public function __toString() {
return $this->name;
}
}
EasyRdf_Format::register('php', 'RDF/PHP', 'http://n2.talis.com/wiki/RDF_PHP_Specification', array(
'application/x-httpd-php-source' => 1.0,
), array(
'phps',
));
EasyRdf_Format::register('json', 'RDF/JSON Resource-Centric', 'http://n2.talis.com/wiki/RDF_JSON_Specification', array(
'application/json' => 1.0,
'text/json' => 0.9,
'application/rdf+json' => 0.9,
), array(
'json',
));
EasyRdf_Format::register('jsonld', 'JSON-LD', 'http://www.w3.org/TR/json-ld/', array(
'application/ld+json' => 1.0,
), array(
'jsonld',
));
EasyRdf_Format::register('ntriples', 'N-Triples', 'http://www.w3.org/TR/n-triples/', array(
'application/n-triples' => 1.0,
'text/plain' => 0.9,
'text/ntriples' => 0.9,
'application/ntriples' => 0.9,
'application/x-ntriples' => 0.9,
), array(
'nt',
));
EasyRdf_Format::register('turtle', 'Turtle Terse RDF Triple Language', 'http://www.dajobe.org/2004/01/turtle', array(
'text/turtle' => 0.8,
'application/turtle' => 0.7,
'application/x-turtle' => 0.7,
), array(
'ttl',
));
EasyRdf_Format::register('rdfxml', 'RDF/XML', 'http://www.w3.org/TR/rdf-syntax-grammar', array(
'application/rdf+xml' => 0.8,
), array(
'rdf',
'xrdf',
));
EasyRdf_Format::register('dot', 'Graphviz', 'http://www.graphviz.org/doc/info/lang.html', array(
'text/vnd.graphviz' => 0.8,
), array(
'gv',
'dot',
));
EasyRdf_Format::register('json-triples', 'RDF/JSON Triples');
EasyRdf_Format::register('n3', 'Notation3', 'http://www.w3.org/2000/10/swap/grammar/n3#', array(
'text/n3' => 0.5,
'text/rdf+n3' => 0.5,
), array(
'n3',
));
EasyRdf_Format::register('rdfa', 'RDFa', 'http://www.w3.org/TR/rdfa-core/', array(
'text/html' => 0.4,
'application/xhtml+xml' => 0.4,
), array(
'html',
));
EasyRdf_Format::register('sparql-xml', 'SPARQL XML Query Results', 'http://www.w3.org/TR/rdf-sparql-XMLres/', array(
'application/sparql-results+xml' => 1.0,
));
EasyRdf_Format::register('sparql-json', 'SPARQL JSON Query Results', 'http://www.w3.org/TR/rdf-sparql-json-res/', array(
'application/sparql-results+json' => 1.0,
));
EasyRdf_Format::register('png', 'Portable Network Graphics (PNG)', 'http://www.w3.org/TR/PNG/', array(
'image/png' => 0.3,
), array(
'png',
));
EasyRdf_Format::register('gif', 'Graphics Interchange Format (GIF)', 'http://www.w3.org/Graphics/GIF/spec-gif89a.txt', array(
'image/gif' => 0.2,
), array(
'gif',
));
EasyRdf_Format::register('svg', 'Scalable Vector Graphics (SVG)', 'http://www.w3.org/TR/SVG/', array(
'image/svg+xml' => 0.3,
), array(
'svg',
));
EasyRdf_Format::registerParser('json', 'EasyRdf_Parser_Json');
EasyRdf_Format::registerParser('jsonld', 'EasyRdf_Parser_JsonLd');
EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Ntriples');
EasyRdf_Format::registerParser('php', 'EasyRdf_Parser_RdfPhp');
EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_RdfXml');
EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Turtle');
EasyRdf_Format::registerParser('rdfa', 'EasyRdf_Parser_Rdfa');
EasyRdf_Format::registerSerialiser('json', 'EasyRdf_Serialiser_Json');
EasyRdf_Format::registerSerialiser('jsonld', 'EasyRdf_Serialiser_JsonLd');
EasyRdf_Format::registerSerialiser('n3', 'EasyRdf_Serialiser_Turtle');
EasyRdf_Format::registerSerialiser('ntriples', 'EasyRdf_Serialiser_Ntriples');
EasyRdf_Format::registerSerialiser('php', 'EasyRdf_Serialiser_RdfPhp');
EasyRdf_Format::registerSerialiser('rdfxml', 'EasyRdf_Serialiser_RdfXml');
EasyRdf_Format::registerSerialiser('turtle', 'EasyRdf_Serialiser_Turtle');
EasyRdf_Format::registerSerialiser('dot', 'EasyRdf_Serialiser_GraphViz');
EasyRdf_Format::registerSerialiser('gif', 'EasyRdf_Serialiser_GraphViz');
EasyRdf_Format::registerSerialiser('png', 'EasyRdf_Serialiser_GraphViz');
EasyRdf_Format::registerSerialiser('svg', 'EasyRdf_Serialiser_GraphViz');