You are here

public static function EasyRdf_Format::guessFormat in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Format.php \EasyRdf_Format::guessFormat()

Attempt to guess the document format from some content.

If $filename is given, then the suffix is first used to guess the format.

If the document format is not recognised, null is returned.

Parameters

string $data The document data:

string $filename Optional filename:

Return value

object EasyRdf_Format The format object

1 call to EasyRdf_Format::guessFormat()
EasyRdf_Graph::parse in vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php
Parse some RDF data into the graph object.

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Format.php, line 233

Class

EasyRdf_Format
Class the represents an RDF file format.

Code

public static function guessFormat($data, $filename = null) {
  if (is_array($data)) {

    # Data has already been parsed into RDF/PHP
    return self::getFormat('php');
  }

  // First try and identify by the filename
  if ($filename and preg_match('/\\.(\\w+)$/', $filename, $matches)) {
    foreach (self::$formats as $format) {
      if (in_array($matches[1], $format->extensions)) {
        return $format;
      }
    }
  }

  // Then try and guess by the first 1024 bytes of content
  $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)) {

    # We don't support any other microformats embedded in HTML
    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;
  }
}