You are here

public function EasyRdf_Parser_Rdfa::parse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Rdfa.php \EasyRdf_Parser_Rdfa::parse()

Parse RDFa 1.1 into an EasyRdf_Graph

Parameters

object EasyRdf_Graph $graph the graph to load the data into:

string $data the RDF document data:

string $format the format of the input data:

string $baseUri the base URI of the data being parsed:

Return value

integer The number of triples added to the graph

Overrides EasyRdf_Parser::parse

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Rdfa.php, line 673

Class

EasyRdf_Parser_Rdfa
Class to parse RDFa 1.1 with no external dependancies.

Code

public function parse($graph, $data, $format, $baseUri) {
  parent::checkParseParams($graph, $data, $format, $baseUri);
  if ($format != 'rdfa') {
    throw new EasyRdf_Exception("EasyRdf_Parser_Rdfa does not support: {$format}");
  }

  // Initialise evaluation context.
  $context = $this
    ->initialContext();
  libxml_use_internal_errors(true);

  // Parse the document into DOM
  $doc = new DOMDocument();

  // Attempt to parse the document as strict XML, and fall back to HTML
  // if XML parsing fails.
  if ($doc
    ->loadXML($data, LIBXML_NONET)) {
    if ($this->debug) {
      print "Document was parsed as XML.";
    }

    // Collect all xmlns namespaces defined throughout the document.
    $sxe = simplexml_import_dom($doc);
    $context['xmlns'] = $sxe
      ->getDocNamespaces(true);
    unset($context['xmlns']['']);
  }
  else {
    $doc
      ->loadHTML($data);
    if ($this->debug) {
      print "Document was parsed as HTML.";
    }
  }

  // Establish the base for both XHTML and HTML documents.
  $xpath = new DOMXPath($doc);
  $xpath
    ->registerNamespace('xh', "http://www.w3.org/1999/xhtml");
  $nodeList = $xpath
    ->query('/xh:html/xh:head/xh:base');
  if ($node = $nodeList
    ->item(0) and $href = $node
    ->getAttribute('href')) {
    $this->baseUri = new EasyRdf_ParsedUri($href);
  }
  $nodeList = $xpath
    ->query('/html/head/base');
  if ($node = $nodeList
    ->item(0) and $href = $node
    ->getAttribute('href')) {
    $this->baseUri = new EasyRdf_ParsedUri($href);
  }

  // Remove the fragment from the base URI
  $this->baseUri
    ->setFragment(null);

  // Recursively process XML nodes
  $this
    ->processNode($doc, $context);
  return $this->tripleCount;
}