diff options
Diffstat (limited to 'rdflib/plugins/serializers')
-rw-r--r-- | rdflib/plugins/serializers/rdfxml.py | 16 | ||||
-rw-r--r-- | rdflib/plugins/serializers/trig.py | 8 | ||||
-rw-r--r-- | rdflib/plugins/serializers/trix.py | 7 | ||||
-rw-r--r-- | rdflib/plugins/serializers/turtle.py | 11 |
4 files changed, 37 insertions, 5 deletions
diff --git a/rdflib/plugins/serializers/rdfxml.py b/rdflib/plugins/serializers/rdfxml.py index 631c8fe0..4242fc05 100644 --- a/rdflib/plugins/serializers/rdfxml.py +++ b/rdflib/plugins/serializers/rdfxml.py @@ -46,7 +46,11 @@ class XMLSerializer(Serializer): yield prefix, namespace def serialize(self, stream, base=None, encoding=None, **args): - self.base = base + # if base is given here, use that, if not and a base is set for the graph use that + if base is not None: + self.base = base + elif self.store.base is not None: + self.base = self.store.base self.__stream = stream self.__serialized = {} encoding = self.encoding @@ -62,6 +66,8 @@ class XMLSerializer(Serializer): # If provided, write xml:base attribute for the RDF if "xml_base" in args: write(' xml:base="%s"\n' % args['xml_base']) + elif self.base: + write(' xml:base="%s"\n' % self.base) # TODO: # assert( # namespaces["http://www.w3.org/1999/02/22-rdf-syntax-ns#"]=='rdf') @@ -163,7 +169,11 @@ class PrettyXMLSerializer(Serializer): def serialize(self, stream, base=None, encoding=None, **args): self.__serialized = {} store = self.store - self.base = base + # if base is given here, use that, if not and a base is set for the graph use that + if base is not None: + self.base = base + elif store.base is not None: + self.base = store.base self.max_depth = args.get("max_depth", 3) assert self.max_depth > 0, "max_depth must be greater than 0" @@ -184,6 +194,8 @@ class PrettyXMLSerializer(Serializer): if "xml_base" in args: writer.attribute(XMLBASE, args["xml_base"]) + elif self.base: + writer.attribute(XMLBASE, self.base) writer.namespaces(namespaces.items()) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index 6c05ad75..250e1c09 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -39,7 +39,7 @@ class TrigSerializer(TurtleSerializer): for triple in context: self.preprocessTriple(triple) - self._contexts[context]=(self.orderSubjects(), self._subjects, self._references) + self._contexts[context] = (self.orderSubjects(), self._subjects, self._references) def reset(self): super(TrigSerializer, self).reset() @@ -49,7 +49,11 @@ class TrigSerializer(TurtleSerializer): spacious=None, **args): self.reset() self.stream = stream - self.base = base + # if base is given here, use that, if not and a base is set for the graph use that + if base is not None: + self.base = base + elif self.store.base is not None: + self.base = self.store.base if spacious is not None: self._spacious = spacious diff --git a/rdflib/plugins/serializers/trix.py b/rdflib/plugins/serializers/trix.py index fceec6bd..0a574493 100644 --- a/rdflib/plugins/serializers/trix.py +++ b/rdflib/plugins/serializers/trix.py @@ -29,6 +29,11 @@ class TriXSerializer(Serializer): self.writer = XMLWriter(stream, nm, encoding, extra_ns={"": TRIXNS}) self.writer.push(TRIXNS[u"TriX"]) + # if base is given here, use that, if not and a base is set for the graph use that + if base is None and self.store.base is not None: + base = self.store.base + if base is not None: + self.writer.attribute("http://www.w3.org/XML/1998/namespacebase", base) self.writer.namespaces() if isinstance(self.store, ConjunctiveGraph): @@ -44,6 +49,8 @@ class TriXSerializer(Serializer): def _writeGraph(self, graph): self.writer.push(TRIXNS[u"graph"]) + if graph.base: + self.writer.attribute("http://www.w3.org/XML/1998/namespacebase", graph.base) if isinstance(graph.identifier, URIRef): self.writer.element( TRIXNS[u"uri"], content=text_type(graph.identifier)) diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index 1c58ba1b..8a41587c 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -224,7 +224,11 @@ class TurtleSerializer(RecursiveSerializer): spacious=None, **args): self.reset() self.stream = stream - self.base = base + # if base is given here, use that, if not and a base is set for the graph use that + if base is not None: + self.base = base + elif self.store.base is not None: + self.base = self.store.base if spacious is not None: self._spacious = spacious @@ -246,6 +250,8 @@ class TurtleSerializer(RecursiveSerializer): self.endDocument() stream.write(b("\n")) + self.base = None + def preprocessTriple(self, triple): super(TurtleSerializer, self).preprocessTriple(triple) for i, node in enumerate(triple): @@ -291,6 +297,9 @@ class TurtleSerializer(RecursiveSerializer): def startDocument(self): self._started = True ns_list = sorted(self.namespaces.items()) + + if self.base: + self.write(self.indent() + '@base <%s> .\n' % self.base) for prefix, uri in ns_list: self.write(self.indent() + '@prefix %s: <%s> .\n' % (prefix, uri)) if ns_list and self._spacious: |