8d505e53ca9615a209ebef1839d25ff1d3f91b7b
[platform/upstream/libxslt.git] / python / tests / extfunc.py
1 #!/usr/bin/python -u
2 import sys
3 import string
4 import libxml2
5 # Memory debug specific
6 libxml2.debugMemory(1)
7 import libxslt
8
9 nodeName = None
10
11 def f(ctx, str):
12     global nodeName
13
14     #
15     # Small check to verify the context is correcly accessed
16     #
17     try:
18         pctxt = libxslt.xpathParserContext(_obj=ctx)
19         ctxt = pctxt.context()
20         tctxt = ctxt.transformContext()
21         nodeName = tctxt.insertNode().name
22     except:
23         pass
24
25     return string.upper(str)
26
27 libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f)
28
29 styledoc = libxml2.parseDoc("""
30 <xsl:stylesheet version='1.0'
31   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
32   xmlns:foo='http://example.com/foo'
33   xsl:exclude-result-prefixes='foo'>
34
35   <xsl:param name='bar'>failure</xsl:param>
36   <xsl:template match='/'>
37     <article><xsl:value-of select='foo:foo($bar)'/></article>
38   </xsl:template>
39 </xsl:stylesheet>
40 """)
41 style = libxslt.parseStylesheetDoc(styledoc)
42 doc = libxml2.parseDoc("<doc/>")
43 result = style.applyStylesheet(doc, { "bar": "'success'" })
44 style.freeStylesheet()
45 doc.freeDoc()
46
47 root = result.children
48 if root.name != "article":
49     print "Unexpected root node name"
50     sys.exit(1)
51 if root.content != "SUCCESS":
52     print "Unexpected root node content, extension function failed"
53     sys.exit(1)
54 if nodeName != 'article':
55     print "The function callback failed to access its context"
56     sys.exit(1)
57
58 result.freeDoc()
59
60 # Memory debug specific
61 libxslt.cleanup()
62 if libxml2.debugMemory(1) == 0:
63     print "OK"
64 else:
65     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
66     libxml2.dumpMemory()