|
37 | 37 | from rdflib.parser import StringInputSource |
38 | 38 | from rdflib.plugins.parsers.nquads import NQuadsParser |
39 | 39 | from rdflib.plugins.serializers.nquads import _nq_row |
40 | | -from rdflib.term import Identifier |
| 40 | +from rdflib.term import Identifier, _is_valid_uri |
41 | 41 |
|
42 | 42 | from c14n.Canonicalize import canonicalize |
43 | 43 | from pyld.__about__ import __copyright__, __license__, __version__ |
@@ -1032,7 +1032,7 @@ def to_rdf(self, input_, options): |
1032 | 1032 | dataset = Dataset() |
1033 | 1033 | for graph_name, graph in sorted(node_map.items()): |
1034 | 1034 | # skip relative IRIs |
1035 | | - if graph_name == '@default' or _is_absolute_iri(graph_name): |
| 1035 | + if graph_name == '@default' or _is_valid_absolute_iri(graph_name): |
1036 | 1036 | g = self._rdflib_term_from_id(graph_name) if graph_name != '@default' else dataset.default_graph |
1037 | 1037 | for t in self._graph_to_rdf(graph, issuer, options): |
1038 | 1038 | s, p, o = t |
@@ -3861,8 +3861,8 @@ def _graph_to_rdf(self, graph, issuer, options): |
3861 | 3861 |
|
3862 | 3862 | for item in items: |
3863 | 3863 | # skip relative IRI subjects and predicates |
3864 | | - if not _is_absolute_iri(id_) or ( |
3865 | | - property != '@type' and not _is_absolute_iri(property) |
| 3864 | + if not _is_valid_absolute_iri(id_) or ( |
| 3865 | + property != '@type' and not _is_valid_absolute_iri(property) |
3866 | 3866 | ): |
3867 | 3867 | continue |
3868 | 3868 |
|
@@ -3900,21 +3900,23 @@ def _list_to_rdf(self, list_: list, issuer: IdentifierIssuer, triples, options) |
3900 | 3900 |
|
3901 | 3901 | last = list_.pop() if list_ else None |
3902 | 3902 | # result is the head of the list |
3903 | | - result = BNode(issuer.get_id()) if last else RDF.nil |
| 3903 | + result = self._rdflib_term_from_id(issuer.get_id()) if last else RDF.nil |
3904 | 3904 | subject = result |
3905 | 3905 |
|
3906 | 3906 | for item in list_: |
3907 | 3907 | object = self._object_to_rdf(item, issuer, triples, options) |
3908 | | - next = BNode(issuer.get_id()) |
3909 | | - triples.append((subject, RDF.first, object)) |
| 3908 | + next = self._rdflib_term_from_id(issuer.get_id()) |
| 3909 | + if object is not None: |
| 3910 | + triples.append((subject, RDF.first, object)) |
3910 | 3911 | triples.append((subject, RDF.rest, next)) |
3911 | 3912 |
|
3912 | 3913 | subject = next |
3913 | 3914 |
|
3914 | 3915 | # tail of list |
3915 | 3916 | if last: |
3916 | 3917 | object = self._object_to_rdf(last, issuer, triples, options) |
3917 | | - triples.append((subject, RDF.first, object)) |
| 3918 | + if object is not None: |
| 3919 | + triples.append((subject, RDF.first, object)) |
3918 | 3920 | triples.append((subject, RDF.rest, RDF.nil)) |
3919 | 3921 |
|
3920 | 3922 | return result |
@@ -4060,7 +4062,7 @@ def _object_to_rdf(self, item, issuer, triples, options): |
4060 | 4062 | id_ = item['@id'] if _is_object(item) else item |
4061 | 4063 |
|
4062 | 4064 | # skip relative IRIs |
4063 | | - if not id_.startswith('_:') and not _is_absolute_iri(id_): |
| 4065 | + if not id_.startswith('_:') and not _is_valid_absolute_iri(id_): |
4064 | 4066 | return None |
4065 | 4067 |
|
4066 | 4068 | return self._rdflib_term_from_id(id_) |
@@ -6558,6 +6560,18 @@ def _is_absolute_iri(v): |
6558 | 6560 | return _is_string(v) and re.match(r'^([A-Za-z][A-Za-z0-9+-.]*|_):[^\s]*$', v) |
6559 | 6561 |
|
6560 | 6562 |
|
| 6563 | +def _is_valid_absolute_iri(v): |
| 6564 | + """ |
| 6565 | + Returns True if the given value is an absolute IRI that RDFLib can |
| 6566 | + serialize, False if not. |
| 6567 | +
|
| 6568 | + :param v: the value to check. |
| 6569 | +
|
| 6570 | + :return: True if the value is a valid absolute IRI, False if not. |
| 6571 | + """ |
| 6572 | + return _is_absolute_iri(v) and _is_valid_uri(v) |
| 6573 | + |
| 6574 | + |
6561 | 6575 | def _is_relative_iri(v): |
6562 | 6576 | """ |
6563 | 6577 | Returns true if the given value is a relative IRI, false if not. |
|
0 commit comments