sax2rdf
index
/Users/yosi/CVSROOT/WWW/2000/10/swap/sax2rdf.py

  A parser for RDF/XML built on the sax2 interface;
  derived from a parser for RDF/XML built on the xmllib XML parser.
 
  To do: Passing on namesapce bindings!
       (dwc: @@huh?)
       (tbl: The bind call in the RDF stream API is used to pass
        on the prefixes found, as hints only for generating readable output code)
 
  - Add parsing of RDF bags
 
 parses DAML_ONT_NS or List_NS lists, generates List_NS
 
 References:
 
 Python/XML HOWTO
                The Python/XML Special Interest Group
                                   xml-sig@python.org 
                               (edited by amk1@bigfoot.com)
 http://py-howto.sourceforge.net/xml-howto/xml-howto.html
 
 http://www.megginson.com/SAX/applications.html#python.parsers
 http://www.python.org/sigs/xml-sig/
 
 How to on xmllib:
 http://www.python.org/doc/howto/xml/node7.html
 
 
 RDF grammar http://www.w3.org/TR/rdf-syntax-grammar/ esp sections 6 and 7
    
##################################### SAX pointers
 First hit on Python SAX parser
 http://www.gca.org/papers/xmleurope2000/papers/s28-04.html#N84395
 
 Howto use SAX in python:
 http://www.python.org/doc/howto/xml/SAX.html

 
Modules
       
RDFSink
diag
isXML
string
sys
uripath
xml

 
Classes
       
exceptions.SyntaxError(exceptions.StandardError)
BadSyntax
xml.sax.handler.ContentHandler
RDFHandler
RDFXMLParser
XMLDOMParser

 
class BadSyntax(exceptions.SyntaxError)
    
Method resolution order:
BadSyntax
exceptions.SyntaxError
exceptions.StandardError
exceptions.Exception

Methods defined here:
__init__(self, info, message)
__str__(self)

Data and other attributes inherited from exceptions.SyntaxError:
filename = None
lineno = None
msg = ''
offset = None
print_file_and_line = None
text = None

Methods inherited from exceptions.Exception:
__getitem__(...)

 
class RDFHandler(xml.sax.handler.ContentHandler)
    RDF Parser using SAX API for XML parsing
 
  Methods defined here:
__init__(self, sink, openFormula, thisDoc, baseURI=None, flags='', why=None)
characters(self, data)
endDocument(self, f=None)
endElementNS(self, name, qname)
Handle end element event
endPrefixMapping(self, prefix)
flush(self)
idAboutAttr(self, attrs)
set up subject and maybe context from attributes
literal_element_end(self, name, qname)
literal_element_end_DOM(self, name, qname)
literal_element_start(self, name, qname, attrs)
literal_element_start_DOM(self, name, qname, attrs)
literal_element_start_DOM_OLD(self, name, qname, attrs)
newBlankNode(self)
processingInstruction(self, name, data)
startElementNS(self, name, qname, attrs)
Handle start tag.
startPrefixMapping(self, prefix, uri)
Performance note:
We make a new dictionary for every binding.
This makes lookup quick and easy, but
it takes extra space and more time to
set up a new binding.
uriref(self, str)
Generate uri from uriref in this document
unicode strings OK.

Methods inherited from xml.sax.handler.ContentHandler:
endElement(self, name)
Signals the end of an element in non-namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElement event.
ignorableWhitespace(self, whitespace)
Receive notification of ignorable whitespace in element content.
 
Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.
 
SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for
locating the origin of document events.
 
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.
 
The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.
 
Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.
skippedEntity(self, name)
Receive notification of a skipped entity.
 
The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.
startDocument(self)
Receive notification of the beginning of a document.
 
The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
 
The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

 
class RDFXMLParser(RDFHandler)
    XML/RDF parser based on sax XML interface
 
 
Method resolution order:
RDFXMLParser
RDFHandler
xml.sax.handler.ContentHandler

Methods defined here:
__init__(self, sink, openFormula, thisDoc=None, flags='', why=None)
close(self)
feed(self, data)
loadStream(self, stream)

Data and other attributes defined here:
flagDocumentation = '\n Flags to control RDF/XML INPUT (after --rdf...t of RDF inc. datatypes, xml:lang, and nodeIds.\n\n'

Methods inherited from RDFHandler:
characters(self, data)
endDocument(self, f=None)
endElementNS(self, name, qname)
Handle end element event
endPrefixMapping(self, prefix)
flush(self)
idAboutAttr(self, attrs)
set up subject and maybe context from attributes
literal_element_end(self, name, qname)
literal_element_end_DOM(self, name, qname)
literal_element_start(self, name, qname, attrs)
literal_element_start_DOM(self, name, qname, attrs)
literal_element_start_DOM_OLD(self, name, qname, attrs)
newBlankNode(self)
processingInstruction(self, name, data)
startElementNS(self, name, qname, attrs)
Handle start tag.
startPrefixMapping(self, prefix, uri)
Performance note:
We make a new dictionary for every binding.
This makes lookup quick and easy, but
it takes extra space and more time to
set up a new binding.
uriref(self, str)
Generate uri from uriref in this document
unicode strings OK.

Methods inherited from xml.sax.handler.ContentHandler:
endElement(self, name)
Signals the end of an element in non-namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElement event.
ignorableWhitespace(self, whitespace)
Receive notification of ignorable whitespace in element content.
 
Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.
 
SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for
locating the origin of document events.
 
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.
 
The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.
 
Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.
skippedEntity(self, name)
Receive notification of a skipped entity.
 
The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.
startDocument(self)
Receive notification of the beginning of a document.
 
The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
 
The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

 
class XMLDOMParser(RDFXMLParser)
    XML format to RDF Graph parser based on sax XML interface
 
 
Method resolution order:
XMLDOMParser
RDFXMLParser
RDFHandler
xml.sax.handler.ContentHandler

Methods defined here:
__init__(self, thisDoc=None, flags='', why=None)

Methods inherited from RDFXMLParser:
close(self)
feed(self, data)
loadStream(self, stream)

Data and other attributes inherited from RDFXMLParser:
flagDocumentation = '\n Flags to control RDF/XML INPUT (after --rdf...t of RDF inc. datatypes, xml:lang, and nodeIds.\n\n'

Methods inherited from RDFHandler:
characters(self, data)
endDocument(self, f=None)
endElementNS(self, name, qname)
Handle end element event
endPrefixMapping(self, prefix)
flush(self)
idAboutAttr(self, attrs)
set up subject and maybe context from attributes
literal_element_end(self, name, qname)
literal_element_end_DOM(self, name, qname)
literal_element_start(self, name, qname, attrs)
literal_element_start_DOM(self, name, qname, attrs)
literal_element_start_DOM_OLD(self, name, qname, attrs)
newBlankNode(self)
processingInstruction(self, name, data)
startElementNS(self, name, qname, attrs)
Handle start tag.
startPrefixMapping(self, prefix, uri)
Performance note:
We make a new dictionary for every binding.
This makes lookup quick and easy, but
it takes extra space and more time to
set up a new binding.
uriref(self, str)
Generate uri from uriref in this document
unicode strings OK.

Methods inherited from xml.sax.handler.ContentHandler:
endElement(self, name)
Signals the end of an element in non-namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElement event.
ignorableWhitespace(self, whitespace)
Receive notification of ignorable whitespace in element content.
 
Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.
 
SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for
locating the origin of document events.
 
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.
 
The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.
 
Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.
skippedEntity(self, name)
Receive notification of a skipped entity.
 
The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.
startDocument(self)
Receive notification of the beginning of a document.
 
The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
 
The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

 
Functions
       
XMLtoDOM(str)
test(args=None)

 
Data
        ANONYMOUS = 3
DAML_ONT_NS = 'http://www.daml.org/2000/10/daml-ont#'
DPO_NS = 'http://www.daml.org/2001/03/daml+oil#'
FORMULA = 1
List_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
NODE_MERGE_URI = 'http://www.w3.org/2000/10/swap/log#is'
OWL_NS = 'http://www.w3.org/2002/07/owl#'
RDF_NS_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
RDF_Specification = 'http://www.w3.org/TR/REC-rdf-syntax/'
STATE_DESCRIPTION = 'Description (have subject)'
STATE_LIST = 'within list'
STATE_LITERAL = 'within XML literal'
STATE_NOT_RDF = 'not RDF'
STATE_NOVALUE = 'no value'
STATE_NO_SUBJECT = 'no context'
STATE_OUTERMOST = 'outermost level'
STATE_VALUE = 'plain value'
XMLLiteralsAsDomTrees = 1
XML_NS_URI = 'http://www.w3.org/XML/1998/namespace'
a = 'bagID'
coreSyntaxTerms = ['RDF', 'ID', 'about', 'parseType', 'resource', 'nodeID', 'datatype']
feature_namespaces = 'http://xml.org/sax/features/namespaces'
nodeElementExceptions = {'http://www.w3.org/1999/02/22-rdf-syntax-ns#ID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#about': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEach': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEachPrefix': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#bagID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#li': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nodeID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#parseType': True, ...}
oldTerms = ['aboutEach', 'aboutEachPrefix', 'bagID']
propertyAttributeExceptions = {'http://www.w3.org/1999/02/22-rdf-syntax-ns#Description': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#ID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#about': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEach': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEachPrefix': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#bagID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#li': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nodeID': True, ...}
propertyElementExceptions = {'http://www.w3.org/1999/02/22-rdf-syntax-ns#Description': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#ID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#about': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEach': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEachPrefix': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#bagID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nodeID': True, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#parseType': True, ...}
syntaxTerms = ['RDF', 'ID', 'about', 'parseType', 'resource', 'nodeID', 'datatype', 'Description', 'li']
tracking = 0