[XML-SIG] RE: Using xmlproc to validate

James Kew jkew@mediabright.co.uk
Wed, 26 Mar 2003 10:44:33 -0000


This is a multi-part message in MIME format.

------_=_NextPart_000_01C2F384.B447B0A8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset="ISO-8859-1"

I wrote:

> I don't have a formal patch, but I do have some code at work that =
fixes up
> drv_xmlproc.XmlprocDriver at runtime so that it does correctly call
> EntityResolver. I'll dig it out tomorrow.

Attached: importing this makes a runtime fix to current PyXML versions. =
More
in the docstring.

SF patch to follow.

--
James Kew
james.kew@btinternet.com




--
This message and any attachments (the "message") is intended solely for =
the
addressees and is confidential. If you receive this message in error, =
please
delete it and immediately notify the sender.
Any use not in accordance with its purpose, any dissemination or =
disclosure,
either whole or partial, is prohibed except formal appoval.
The E-Mail transmission can not guarantee the integrity of this message.
CANAL+TECHNOLOGIES will not therefore be liable for the message if =
modified.



------_=_NextPart_000_01C2F384.B447B0A8
Content-Type: application/octet-stream;
	name="XMLProcFixup.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="XMLProcFixup.py"

#
# BEGIN COPYRIGHT NOTICE
# PROJECT MLI
# (c) Canal+Technologies 2002
# END COPYRIGHT NOTICE
#=20
# BEGIN SOURCE CODE CONTROL INFORMATION
# %name:     XMLProcFixup.py %
# %version:  1 %
# %instance: 1 %
# END SOURCE CODE CONTROL INFORMATION
#
# XMLProc parser fixup.
#

__version__ =3D "%full_filespec: XMLProcFixup.py~1:python:1 %"
__doc__ =3D """Fixup the XMLProc SAX2 parser to support the =
EntityResolver interface.

The current version (PyXML 0.8, 0.8.1, 0.8.2, file version 1.15) of
xml.sax.driver2.drv_xmlproc.XmlprocDriver does not fully support
setEntityResolver: although an EntityResolver can be set, it is not
called during parsing.

This module applies runtime patches to the class such that it:
* exposes the xmlproc.xmlapp.PubIdResolver methods
* registers itself as a pubid_resolver on the underlying xmlval parser
* delegates PubId resolution to the current EntityResolver.

Note that this does not fully fulfil the required semantics of the
EntityResolver interface: the user's EntityResolver.resolveEntity may
not return an InputSource.

Usage: import this module before using validating SAX2 parsers.
"""

# We will add and replace methods in =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver.=20
import xml.sax.drivers2.drv_xmlproc

# Has the source already been patched?
if not hasattr(xml.sax.drivers2.drv_xmlproc.XmlprocDriver, =
"resolve_doctype_pubid"):

    def funcToMethod(func, clas, method_name=3DNone):
        """
        Bind a function as a class method.
        See Python Cookbook (Martelli/Ascher) recipe 5.12.

        func:
            Function to bind.
        clas:
            Class to bind to.
        method_name:
            Name of bound method. Defaults to name of function.
        """
        setattr(clas, method_name or func.__name__, func)

    # resolve_pubid:=20
    def resolve_pubid(self, pubid, sysid):
        """
        Resolve a pubid. This function is bound as the =
resolve_pe_pubid,
        resolve_doctype_pubid, resolve_entity_pubid methods on =
XmlprocDriver.
        """
        # Delegate out to the instance's EntityResolver.
        return self._ent_handler.resolveEntity(pubid, sysid)

    def prepareParser(self, source):
        """
        Prepare the parser. This function is bound to replace the =
existing
        prepareParser method on XmlprocDriver. We assume the existing
        prepareParser method is rebound as old_prepareParser.
        """
        # Delegate out to the existing method.
        self.old_prepareParser(source)
        # Set the pubid_resolver on the underlying xmlval parser.
        self._parser.set_pubid_resolver(self)

    # Add the xmlproc PubIdResolver methods to XmlprocDriver.=20
    funcToMethod(resolve_pubid, =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver, "resolve_pe_pubid")
    funcToMethod(resolve_pubid, =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver, "resolve_doctype_pubid")
    funcToMethod(resolve_pubid, =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver, "resolve_entity_pubid")

    # Rebind prepareParser to old_prepareParser.
    =
funcToMethod(xml.sax.drivers2.drv_xmlproc.XmlprocDriver.prepareParser, =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver, "old_prepareParser")
    # Replace prepareParser on XmlprocDriver.=20
    funcToMethod(prepareParser, =
xml.sax.drivers2.drv_xmlproc.XmlprocDriver)


    
------_=_NextPart_000_01C2F384.B447B0A8--