1#!/usr/bin/python -u
2import sys
3import string
4import StringIO
5import libxml2
6# Memory debug specific
7libxml2.debugMemory(1)
8import libxslt
9
10EXT_URL="http://example.com/foo"
11
12insertNodeName = None
13transformNodeName = None
14
15def compile_test(style, inst, func):
16    pass
17
18def transform_test(ctx, node, inst, comp):
19    global insertNodeName
20
21    #
22    # Small check to verify the context is correcly accessed
23    #
24    try:
25        #
26        # FIXME add some more sanity checks
27        #
28        tctxt = libxslt.transformCtxt(_obj=ctx)
29        insertNodeName = tctxt.insertNode().name
30
31        # FIXME find and confirm the note being replaced is called 'test'
32        # transformNodeName = libxml2.xmlNode(inst).name
33    except:
34        pass
35
36    tctxt.insertNode().addContent('SUCCESS')
37
38
39
40styledoc = libxml2.parseDoc("""
41<xsl:stylesheet version='1.0'
42  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
43  xmlns:foo='%s'
44  extension-element-prefixes='foo'>
45
46  <xsl:template match='/'>
47    <article><foo:test>FAILURE</foo:test></article>
48    <deeper><article><foo:test>something<foo:test>nested</foo:test>even</foo:test></article></deeper>
49  </xsl:template>
50</xsl:stylesheet>
51""" % EXT_URL)
52
53style = libxslt.parseStylesheetDoc(styledoc)
54libxslt.registerExtModuleElement("test", EXT_URL, compile_test, transform_test)
55doc = libxml2.parseDoc("<doc/>")
56result = style.applyStylesheet(doc, None)
57style.freeStylesheet()
58doc.freeDoc()
59
60
61extensions = StringIO.StringIO()
62libxslt.debugDumpExtensions(extensions)
63
64if 0 and extensions.buf.find(EXT_URL) < 0:
65    print "Element extension not registered (or dumping broken)"
66    sys.exit(1)
67
68root = result.children
69
70if root.name != "article":
71    print "Unexpected root node name"
72    sys.exit(1)
73if root.content != "SUCCESS":
74    print "Unexpected root node content, extension function failed"
75    sys.exit(1)
76if insertNodeName != 'article':
77    print "The function callback failed to access its context"
78    sys.exit(1)
79
80result.dump(sys.stdout)
81result.freeDoc()
82
83# Memory debug specific
84libxslt.cleanup()
85if libxml2.debugMemory(1) == 0:
86    print "OK"
87else:
88    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
89    libxml2.dumpMemory()
90