convert method

  1. @override
RdfGraph convert(
  1. String input, {
  2. String? documentUrl,
})

Decodes an RDF document and returns an RDF graph

This method transforms a textual RDF document into a structured RdfGraph object containing triples parsed from the input. It implements the convert method from the Converter interface.

Parameters:

  • input The RDF document to decode, as a string.
  • documentUrl The absolute URL of the document, used for resolving relative IRIs. If not provided, relative IRIs will be kept as-is or handled according to format-specific rules.

Returns:

  • An RdfGraph containing the triples parsed from the input.

The specific decoding behavior depends on the implementation of this interface, which will handle format-specific details like prefix resolution, blank node handling, etc.

May throw format-specific parsing exceptions if the input is malformed.

Implementation

@override
RdfGraph convert(String input, {String? documentUrl}) {
  final dataset = _decoder.convert(input, documentUrl: documentUrl);

  // If no named graphs, just return the default graph
  if (dataset.namedGraphs.isEmpty) {
    return dataset.defaultGraph;
  }

  // Handle named graphs according to the configured mode
  switch (_options.namedGraphHandling) {
    case NamedGraphHandling.strict:
      throw RdfDecoderException(
        "JSON-LD document contains ${dataset.namedGraphs.length} named graph(s), "
        "but JsonLdGraphDecoder only supports documents with a single default graph. "
        "Use JsonLdDecoder for full dataset support, or configure namedGraphHandling "
        "to ignoreNamedGraphs or mergeIntoDefault.",
        format: _format,
      );

    case NamedGraphHandling.ignoreNamedGraphs:
      _logNamedGraphHandling(
        NamedGraphLogLevel.fine,
        "Ignoring ${dataset.namedGraphs.length} named graph(s) and returning only the default graph "
        "with ${dataset.defaultGraph.triples.length} triple(s)",
      );
      return dataset.defaultGraph;

    case NamedGraphHandling.mergeIntoDefault:
      _logNamedGraphHandling(
        NamedGraphLogLevel.warning,
        "Merging ${dataset.namedGraphs.length} named graph(s) into the default graph. "
        "Graph name information will be lost.",
      );

      // Merge all triples from named graphs into the default graph
      final allTriples = <Triple>[
        ...dataset.defaultGraph.triples,
        for (final namedGraph in dataset.namedGraphs)
          ...namedGraph.graph.triples,
      ];

      return RdfGraph(triples: allTriples);
  }
}