Skip to content

Commit

Permalink
Format::guessFormat is a bit more precise from now on (for rdf/xml + …
Browse files Browse the repository at this point in the history
…turtle)
  • Loading branch information
k00ni committed Apr 3, 2024
1 parent a5c3c97 commit ac378cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ public static function guessFormat($data, $filename = null)
return self::getFormat('json');
} elseif (preg_match('/<rdf:/i', $short)) {
return self::getFormat('rdfxml');
} elseif (str_contains($short, '<Ontology xmlns=')) {
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)) {
Expand All @@ -278,7 +280,11 @@ public static function guessFormat($data, $filename = null)
return self::getFormat('rdfa');
} elseif (preg_match('/@prefix\s|@base\s/', $short)) {
return self::getFormat('turtle');
} elseif (preg_match('/prefix\s|base\s/i', $short)) {
} elseif (
preg_match('/prefix\s|base\s/i', $short)
// see FormatTest::testGuessFormatTurtleByPrefix for an example
&& false === str_contains($short, '<?xml')
) {
return self::getFormat('turtle');
} elseif (preg_match('/^\s*<.+> <.+>/m', $short)) {
return self::getFormat('ntriples');
Expand Down
18 changes: 18 additions & 0 deletions tests/EasyRdf/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,24 @@ public function testGuessFormatXml()
$this->assertStringEquals('', $format);
}

/**
* Case with regex
*
* /prefix\s|base\s/i
*
* was not precise enough and lead to (avoidable) wrong guesses.
*/
public function testGuessFormatTurtleByPrefix()
{
$data = '<?xml version="1.0"?>
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.semanticweb.org/hsiehjulien/ontologies/2016/3/untitled-ontology-4"
ontologyIRI="http://www.semanticweb.org/hsiehjulien/ontologies/2016/3/untitled-ontology-4">
<Prefix name="" IRI="http://www.semanticweb.org/hsiehjulien/ontologies/2016/3/untitled-ontology-4#"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>';
$this->assertStringEquals('rdfxml', Format::guessFormat($data));
}

public function testGuessFormatByFilenameTtl()
{
$format = Format::guessFormat(
Expand Down

0 comments on commit ac378cc

Please sign in to comment.