Пример #1
0
 /**
  * Parse RDF/PHP into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return boolean             true if parsing was successful
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'php') {
         throw new EasyRdf_Exception("EasyRdf_Parser_RdfPhp does not support: {$format}");
     }
     // Reset the bnode mapping
     $this->resetBnodeMap();
     foreach ($data as $subject => $properties) {
         if (substr($subject, 0, 2) === '_:') {
             $subject = $this->remapBnode($graph, $subject);
         } elseif (preg_match('/^\\w+$/', $subject)) {
             # Cope with invalid RDF/JSON serialisations that
             # put the node name in, without the _: prefix
             # (such as net.fortytwo.sesametools.rdfjson)
             $subject = $this->remapBnode($graph, $subject);
         }
         foreach ($properties as $property => $objects) {
             foreach ($objects as $object) {
                 if ($object['type'] === 'bnode') {
                     $object['value'] = $this->remapBnode($graph, $object['value']);
                 }
                 $graph->add($subject, $property, $object);
             }
         }
     }
     return true;
 }
Пример #2
0
 /**
  * Parse a JSON-LD document into an EasyRdf_Graph
  *
  * Attention: Since JSON-LD supports datasets, a document may contain
  * multiple graphs and not just one. This parser returns only the
  * default graph. An alternative would be to merge all graphs.
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'jsonld') {
         throw new EasyRdf_Exception("EasyRdf_Parser_JsonLd does not support {$format}");
     }
     try {
         $quads = \ML\JsonLD\JsonLD::toRdf($data, array('base' => $baseUri));
     } catch (\ML\JsonLD\Exception\JsonLdException $e) {
         throw new EasyRdf_Parser_Exception($e->getMessage());
     }
     foreach ($quads as $quad) {
         // Ignore named graphs
         if (null !== $quad->getGraph()) {
             continue;
         }
         $subject = (string) $quad->getSubject();
         if ('_:' === substr($subject, 0, 2)) {
             $subject = $this->remapBnode($subject);
         }
         $predicate = (string) $quad->getProperty();
         if ($quad->getObject() instanceof \ML\IRI\IRI) {
             $object = array('type' => 'uri', 'value' => (string) $quad->getObject());
             if ('_:' === substr($object['value'], 0, 2)) {
                 $object = array('type' => 'bnode', 'value' => $this->remapBnode($object['value']));
             }
         } else {
             $object = array('type' => 'literal', 'value' => $quad->getObject()->getValue());
             if ($quad->getObject() instanceof \ML\JsonLD\LanguageTaggedString) {
                 $object['lang'] = $quad->getObject()->getLanguage();
             } else {
                 $object['datatype'] = $quad->getObject()->getType();
             }
         }
         $this->addTriple($subject, $predicate, $object);
     }
     return $this->tripleCount;
 }
Пример #3
0
 /**
  * Parse RDF/PHP into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return boolean             true if parsing was successful
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'php') {
         throw new EasyRdf_Exception("EasyRdf_Parser_RdfPhp does not support: {$format}");
     }
     // Reset the bnode mapping
     $this->resetBnodeMap();
     # FIXME: validate the data (?)
     foreach ($data as $subject => $properties) {
         if (substr($subject, 0, 2) === '_:') {
             $subject = $this->remapBnode($graph, $subject);
         }
         foreach ($properties as $property => $objects) {
             foreach ($objects as $object) {
                 if ($object['type'] === 'bnode') {
                     $object['value'] = $this->remapBnode($graph, $object['value']);
                 }
                 $graph->add($subject, $property, $object);
             }
         }
     }
     return true;
 }
 /**
  * Parse an RDF document into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return boolean             true if parsing was successful
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     $parser = librdf_new_parser($this->_world, $format, null, null);
     if (!$parser) {
         throw new EasyRdf_Exception("Failed to create librdf_parser of type: {$format}");
     }
     $rdfUri = librdf_new_uri($this->_world, $baseUri);
     if (!$rdfUri) {
         throw new EasyRdf_Exception("Failed to create librdf_uri from: {$baseUri}");
     }
     $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri);
     if (!$stream) {
         throw new EasyRdf_Exception("Failed to parse RDF stream for: {$rdfUri}");
     }
     do {
         $statement = librdf_stream_get_object($stream);
         if ($statement) {
             $subject = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_subject($statement));
             $predicate = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_predicate($statement));
             $object = EasyRdf_Parser_Redland::nodeToArray(librdf_statement_get_object($statement));
             $graph->add($subject, $predicate, $object);
         }
     } while (!librdf_stream_next($stream));
     $errorCount = $this->parserErrorCount($parser);
     if ($errorCount) {
         throw new EasyRdf_Exception("{$errorCount} errors while parsing.");
     }
     librdf_free_uri($rdfUri);
     librdf_free_stream($stream);
     librdf_free_parser($parser);
     // Success
     return true;
 }
 /**
  * Parse an N-Triples document into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'ntriples') {
         throw new EasyRdf_Exception("EasyRdf_Parser_Ntriples does not support: {$format}");
     }
     $lines = preg_split("/[\r\n]+/", strval($data));
     foreach ($lines as $line) {
         if (preg_match("/^\\s*#/", $line)) {
             continue;
         } else {
             if (preg_match("/(.+)\\s+<([^<>]+)>\\s+(.+)\\s*\\./", $line, $matches)) {
                 $this->addTriple($this->parseNtriplesSubject($matches[1]), $this->unescapeString($matches[2]), $this->parseNtriplesObject($matches[3]));
             }
         }
     }
     return $this->_tripleCount;
 }
 /**
  * Parse an RDF/XML document into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'rdfxml') {
         throw new EasyRdf_Exception("EasyRdf_Parser_RdfXml does not support: {$format}");
     }
     $this->init($graph, $baseUri);
     $this->resetBnodeMap();
     /* xml parser */
     $this->initXMLParser();
     /* parse */
     if (!xml_parse($this->_xmlParser, $data, false)) {
         throw new EasyRdf_Exception('XML error: "' . xml_error_string(xml_get_error_code($this->_xmlParser)) . '" at line ' . xml_get_current_line_number($this->_xmlParser));
     }
     xml_parser_free($this->_xmlParser);
     return $this->_tripleCount;
 }
Пример #7
0
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     // Parsing goes here
     return true;
 }
Пример #8
0
 /**
  * Parse RDFa 1.1 into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 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;
 }
Пример #9
0
 public function testParseUndefined()
 {
     $this->setExpectedException('EasyRdf_Exception', 'This method should be overridden by sub-classes.');
     $parser = new EasyRdf_Parser();
     $parser->parse($this->graph, 'data', 'format', 'baseUri');
 }
Пример #10
0
 /**
  * Parse RDF/PHP into an EasyRdf_Graph
  *
  * @param string $graph    the graph to load the data into
  * @param string $data     the RDF document data
  * @param string $format   the format of the input data
  * @param string $baseUri  the base URI of the data being parsed
  * @return boolean         true if parsing was successful
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'php') {
         throw new EasyRdf_Exception("EasyRdf_Parser_RdfPhp does not support: {$format}");
     }
     # Convert into an object graph
     foreach ($data as $subject => $touple) {
         $type = $this->getResourceType($data, $subject);
         # Is this a bnode?
         if (substr($subject, 0, 2) == '_:') {
             if (!isset($this->_bnodeMap[$subject])) {
                 $this->_bnodeMap[$subject] = $graph->newBNode($type);
             }
             $res = $this->_bnodeMap[$subject];
         } else {
             $res = $graph->resource($subject, $type);
         }
         foreach ($touple as $property => $objects) {
             $this->addProperty($graph, $data, $res, $property, $objects);
         }
     }
     return true;
 }
Пример #11
0
 /**
  * Parse an N-Triples document into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'ntriples') {
         throw new EasyRdf_Exception("EasyRdf_Parser_Ntriples does not support: {$format}");
     }
     $lines = preg_split('/\\x0D?\\x0A/', strval($data));
     foreach ($lines as $index => $line) {
         $lineNum = $index + 1;
         if (preg_match('/^\\s*#/', $line)) {
             # Comment
             continue;
         } elseif (preg_match('/^\\s*(.+?)\\s+<([^<>]+?)>\\s+(.+?)\\s*\\.\\s*$/', $line, $matches)) {
             $this->addTriple($this->parseNtriplesSubject($matches[1], $lineNum), $this->unescapeString($matches[2]), $this->parseNtriplesObject($matches[3], $lineNum));
         } elseif (preg_match('/^\\s*$/', $line)) {
             # Blank line
             continue;
         } else {
             throw new EasyRdf_Parser_Exception("Failed to parse statement", $lineNum);
         }
     }
     return $this->tripleCount;
 }