convert method

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

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}) {
  _logger.fine(
    'Parsing N-Triples document${documentUrl != null ? " with base URL: $documentUrl" : ""}',
  );

  final List<Triple> triples = [];
  final List<String> lines = input.split('\n');
  int lineNumber = 0;

  for (final line in lines) {
    lineNumber++;
    final trimmed = line.trim();

    // Skip empty lines and comments
    if (trimmed.isEmpty || trimmed.startsWith('#')) {
      continue;
    }

    try {
      final triple = _parseLine(trimmed, lineNumber);
      triples.add(triple);
    } catch (e) {
      throw RdfDecoderException(
        'Error parsing N-Triples at line $lineNumber: ${e.toString()}',
        format: _formatName,
        source: SourceLocation(
          line: lineNumber - 1, // Convert to 0-based line number
          column: 0,
          context: trimmed,
        ),
      );
    }
  }

  return RdfGraph.fromTriples(triples);
}