#!/usr/bin/python
"""
xml2html: Convert XML to HTML
 
*** SUPERSEDED by pyslt ***
 
Uses SAX (event-based parsing).
 
***LICENSE***
"""
 
_ID = '$Id: xml2html-1.01-1.py,v 1.1.1.1 2000/01/15 14:37:46 ecoaccess Exp $'
 
import sys, copy
from xml.sax.drivers.drv_xmlproc_val import *
 
class myParser(SAX_XPValParser):
    """XML parser."""
 
    psl = None
 
    def handle_doctype(self, rootname, pub_id, sys_id):
        self.dtd_handler.doctypeDecl(rootname, pub_id, sys_id, self.psl)
 
 
class docHandler:
    """XML Document events handler."""
 
    def characters(self, ch, start, length):
        """Handle a character data event.
        The data are contained in the substring of ch
        starting at position start and of length length."""
 
        print ch[start:start+length],
 
    def endDocument(self):
        """Handle an event for the end of a document."""
 
        pass
 
    def endElement(self, name):
        """Handle an event for the end of an element."""
 
        print self.translation_table[name].end()
 
    def ignorableWhitespace(self, ch, start, length):
        """Handle an event for ignorable whitespace in element content.
        The data are contained in the substring of ch
        starting at position start and of length length."""
         
        pass
 
    def processingInstruction(self, target, data):
        """Handle a processing instruction event."""
          
        pass
 
    def setDocumentLocator(self, locator):
        """Receive an object for locating the origin of SAX document events.
        locator is an object of type Locator."""
 
        pass
 
    def startDocument(self):
        """Handle an event for the beginning of a document."""
 
        pass
 
    def startElement(self, name, atts):
        """Handle an event for the beginning of an element.
        atts is an object of type AttributeList."""
 
        print self.translation_table[name].start(atts),
 
class dtdHandler:
    """Document Type Definition events handler."""
 
    def notationDecl(self, name, publicId, systemId):
        """Handle a notation declaration event."""
 
        pass
 
    def unparsedEntityDecl(self, name, publicId, systemId, ndata):
        """Handle an unparsed entity declaration event."""
 
        pass
 
    def doctypeDecl(self, rootname, pub_id, sys_id, psl=None):
        """Handle a Doctype Declaration event."""
 
        #print "Document type: %s" % rootname
 
        # Here is where I would need to load in the appropriate translation
        # table.
 
        if psl:
            x = {'kludge': {}}
            execfile(psl, globals(), locals())
            translation_table = x['kludge']
        else:
            if rootname == "Bylaws":
                from org.ecoaccess.dtd.Bylaws import translation_table
            elif rootname == "Funspec":
                from org.ecoaccess.dtd.Funspec import translation_table
            else:
                translation_table = {}
            
        docHandler.translation_table = translation_table
 
# --- Main prog
 
if __name__ == '__main__':
    p=myParser()
    if len(sys.argv) > 2:
        p.psl = sys.argv[2]
    p.setDocumentHandler(docHandler())
    p.setDTDHandler(dtdHandler())
    p.parse(sys.argv[1])