1 | #!/usr/bin/python -u
|
---|
2 | import sys
|
---|
3 | import string
|
---|
4 | import StringIO
|
---|
5 | import libxml2
|
---|
6 | # Memory debug specific
|
---|
7 | libxml2.debugMemory(1)
|
---|
8 | import libxslt
|
---|
9 |
|
---|
10 | EXT_URL="http://example.com/foo"
|
---|
11 |
|
---|
12 | insertNodeName = None
|
---|
13 | transformNodeName = None
|
---|
14 |
|
---|
15 | def compile_test(style, inst, func):
|
---|
16 | pass
|
---|
17 |
|
---|
18 | def 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 |
|
---|
40 | styledoc = 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 |
|
---|
53 | style = libxslt.parseStylesheetDoc(styledoc)
|
---|
54 | libxslt.registerExtModuleElement("test", EXT_URL, compile_test, transform_test)
|
---|
55 | doc = libxml2.parseDoc("<doc/>")
|
---|
56 | result = style.applyStylesheet(doc, None)
|
---|
57 | style.freeStylesheet()
|
---|
58 | doc.freeDoc()
|
---|
59 |
|
---|
60 |
|
---|
61 | extensions = StringIO.StringIO()
|
---|
62 | libxslt.debugDumpExtensions(extensions)
|
---|
63 |
|
---|
64 | if 0 and extensions.buf.find(EXT_URL) < 0:
|
---|
65 | print "Element extension not registered (or dumping broken)"
|
---|
66 | sys.exit(1)
|
---|
67 |
|
---|
68 | root = result.children
|
---|
69 |
|
---|
70 | if root.name != "article":
|
---|
71 | print "Unexpected root node name"
|
---|
72 | sys.exit(1)
|
---|
73 | if root.content != "SUCCESS":
|
---|
74 | print "Unexpected root node content, extension function failed"
|
---|
75 | sys.exit(1)
|
---|
76 | if insertNodeName != 'article':
|
---|
77 | print "The function callback failed to access its context"
|
---|
78 | sys.exit(1)
|
---|
79 |
|
---|
80 | result.dump(sys.stdout)
|
---|
81 | result.freeDoc()
|
---|
82 |
|
---|
83 | # Memory debug specific
|
---|
84 | libxslt.cleanup()
|
---|
85 | if libxml2.debugMemory(1) == 0:
|
---|
86 | print "OK"
|
---|
87 | else:
|
---|
88 | print "Memory leak %d bytes" % (libxml2.debugMemory(1))
|
---|
89 | libxml2.dumpMemory()
|
---|