import sys import string import re import util import rdfstore import sax2rdf import StringIO import xml.sax import notation3 import nt # A class for parsing an XML RDF document class XRDF(nt.NTriples): """Parses an XML RDF document using notation3.py""" def __init__(self): rdfstore.RDFStore.__init__(self, data=[]) def parsen(self, filename): file = open(filename, 'r') self.parse(file, filename) file.close() def parse(self, file, fn): """Takes file, sparts it, parses, and adds to store""" t, rh = StringIO.StringIO(), sax2rdf.RDFHandler p = xml.sax.make_parser() p.setFeature(xml.sax.handler.feature_namespaces, 1) p.setContentHandler(rh(notation3.ToN3(t.write, flags='spart'), fn)) p.parse(file) # Parse the file, dump as -spart into temp t.pos = 0 # Set back to the beginning of t x = n3filetodoc(t) # RegExp temp as normal t.close() self.data = x # The entire file started out as just this hack to parse incoming # XML RDF documents into a store... it's incredible how quickly it # became a fully fledged RDF parser/tool! def parseString(self, s, fn): """Takes a string, sparts it, parses, and adds to store""" t, rh = StringIO.StringIO(), sax2rdf.RDFHandler p = xml.sax.make_parser() p.setFeature(xml.sax.handler.feature_namespaces, 1) p.setContentHandler(rh(notation3.ToN3(t.write, flags='spart'), fn)) p.parseString(s) # Parse the string, dump as -spart into temp t.pos = 0 # Set back to the beginning of t x = self.parsent(t) # RegExp temp as normal t.close() self.data = x