Devs, I've taken a first pass at implementing a simple TriX serializer. I should have attached the new file (which goes in `.../rdflib/syntax/serializers/TriXSerializer.py`) to this email, along with a crazy little test script. The test script exercises round tripping, but it turned out parsing was broken, so I fixed it; the patch for that (and adding the new serializer to the plugin list) should be attached as well. Finally, does anyone mind if I clean up the TriX parser a bit?
Take care, John L. Clark -- PLEASE NOTE that this message is not digitally signed. As a result, you have no strong evidence that this message was actually sent by me. Upon request I can provide a digitally signed receipt for this message or other evidence validating its contents if you need such evidence.
Index: rdflib/plugin.py =================================================================== --- rdflib/plugin.py (revision 1239) +++ rdflib/plugin.py (working copy) @@ -49,6 +49,9 @@ register('pretty-xml', serializers.Serializer, 'rdflib.syntax.serializers.PrettyXMLSerializer', 'PrettyXMLSerializer') +register('TriX', serializers.Serializer, + 'rdflib.syntax.serializers.TriXSerializer', 'TriXSerializer') + register('nt', serializers.Serializer, 'rdflib.syntax.serializers.NTSerializer', 'NTSerializer') Index: rdflib/syntax/parsers/TriXHandler.py =================================================================== --- rdflib/syntax/parsers/TriXHandler.py (revision 1239) +++ rdflib/syntax/parsers/TriXHandler.py (working copy) @@ -33,6 +33,7 @@ """ from rdflib import RDF, RDFS, Namespace from rdflib import URIRef, BNode, Literal +from rdflib.Namespace import Namespace from rdflib.Graph import Graph from rdflib.exceptions import ParserError, Error from rdflib.syntax.xml_names import is_ncname @@ -42,7 +43,7 @@ RDFNS = RDF.RDFNS -TRIXNS=Namespace.Namespace("http://www.w3.org/2004/03/trix/trix-1/") +TRIXNS=u"http://www.w3.org/2004/03/trix/trix-1/" class TriXHandler(handler.ContentHandler): @@ -200,6 +201,7 @@ self.error("This should never happen if the SAX parser ensures XML syntax correctness") if name[1]=="graph": + self.graph = Graph(store = self.store.store) self.state=1 if name[1]=="TriX":
from rdflib.syntax.serializers import Serializer from rdflib.URIRef import URIRef from rdflib.Literal import Literal from rdflib.BNode import BNode from rdflib.Graph import Graph, ConjunctiveGraph from Ft.Xml import MarkupWriter, XML_NAMESPACE TRIX_NS = u"http://www.w3.org/2004/03/trix/trix-1/" class TriXSerializer(Serializer): def __init__(self, store): super(TriXSerializer, self).__init__(store) def serialize(self, stream, base=None, encoding=None, **args): self.writer = MarkupWriter(stream=stream, encoding=encoding, indent="yes") self.writer.startDocument() self.writer.startElement(u"TriX", TRIX_NS) if isinstance(self.store, ConjunctiveGraph): for subgraph in self.store.contexts(): self._writeGraph(subgraph) elif isinstance(self.store, Graph): self._writeGraph(self.store) else: pass self.writer.endElement(u"TriX", TRIX_NS) self.writer.endDocument() def _writeGraph(self, graph): self.writer.startElement(u"graph", TRIX_NS) if isinstance(graph.identifier, URIRef): self.writer.simpleElement(u"uri", TRIX_NS, content=unicode(graph.identifier)) for triple in graph.triples((None,None,None)): self._writeTriple(triple) self.writer.endElement(u"graph", TRIX_NS) def _writeTriple(self, triple): self.writer.startElement(u"triple", TRIX_NS) for component in triple: if isinstance(component, URIRef): self.writer.simpleElement(u"uri", TRIX_NS, content=unicode(component)) elif isinstance(component, BNode): self.writer.simpleElement(u"id", TRIX_NS, content=unicode(component)) elif isinstance(component, Literal): if component.datatype: self.writer.simpleElement(u"typedLiteral", TRIX_NS, content=unicode(component), attributes={u"datatype": unicode(component.datatype)}) elif component.language: self.writer.simpleElement(u"plainLiteral", TRIX_NS, content=unicode(component), attributes={ (u"xml:lang", XML_NAMESPACE): unicode(component.language)}) else: self.writer.simpleElement(u"plainLiteral", TRIX_NS, content=unicode(component)) self.writer.endElement(u"triple", TRIX_NS)
from rdflib.Graph import ConjunctiveGraph from rdflib import URIRef, Literal from rdflib.Graph import Graph s1 = URIRef('store:1') r1 = URIRef('resource:1') r2 = URIRef('resource:2') label = URIRef('predicate:label') g1 = Graph(identifier = s1) g1.add((r1, label, Literal("label 1", lang="en"))) g1.add((r1, label, Literal("label 2"))) s2 = URIRef('store:2') g2 = Graph(identifier = s2) g2.add((r2, label, Literal("label 3"))) g = ConjunctiveGraph() for s,p,o in g1.triples((None, None, None)): g.addN([(s,p,o,g1)]) for s,p,o in g2.triples((None, None, None)): g.addN([(s,p,o,g2)]) r3 = URIRef('resource:3') g.add((r3, label, Literal(4))) #g.addN([(r1, label, Literal("label 1"), s1), # (r1, label, Literal("label 2"), s1), # (r2, label, Literal("label 3"), s2)]) for c in g.contexts(): pass #s = Graph(g.store, identifier=c) #print c, c.identifier.__class__, c.identifier, len(c) r = g.serialize(format='TriX') print r g3 = ConjunctiveGraph() from rdflib.StringInputSource import StringInputSource g3.parse(StringInputSource(r, None), format='trix') for c in g3.contexts(): print c, c.identifier.__class__, c.identifier, len(c)
_______________________________________________ Dev mailing list Dev@rdflib.net http://rdflib.net/mailman/listinfo/dev