3 # This tests custom input callbacks
9 str_io = StringIO.StringIO
14 # We implement a new scheme, py://strings/ that will reference this dictionary
16 'catalogs/catalog.xml' :
17 '''<?xml version="1.0" encoding="utf-8"?>
18 <!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
19 <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
20 <rewriteSystem systemIdStartString="http://example.com/dtds/" rewritePrefix="../dtds/"/>
24 '''<?xml version="1.0" encoding="utf-8"?>
25 <!DOCTYPE root SYSTEM "http://example.com/dtds/sample.dtd">
26 <root>&sample.entity;</root>''',
30 <!ELEMENT root (#PCDATA)>
31 <!ENTITY sample.entity "replacement text">'''
34 prefix = "py://strings/"
35 startURL = prefix + "xml/sample.xml"
36 catURL = prefix + "catalogs/catalog.xml"
39 if not(URI.startswith(prefix)):
41 path = URI[len(prefix):]
42 if path not in pystrings:
44 return str_io(pystrings[path])
47 def run_test(desc, docpath, catalog, exp_status="verified", exp_err=[], test_callback=None,
48 root_name="root", root_content="replacement text"):
49 opts = libxml2.XML_PARSE_DTDLOAD | libxml2.XML_PARSE_NONET | libxml2.XML_PARSE_COMPACT
52 def my_global_error_cb(ctx, msg):
53 actual_err.append((-1, msg))
54 def my_ctx_error_cb(arg, msg, severity, reserved):
55 actual_err.append((severity, msg))
57 libxml2.registerErrorHandler(my_global_error_cb, None)
59 parser = libxml2.createURLParserCtxt(docpath, opts)
60 parser.setErrorHandler(my_ctx_error_cb, None)
61 if catalog is not None:
62 parser.addLocalCatalog(catalog)
63 if test_callback is not None:
65 parser.parseDocument()
67 actual_status = "loaded"
68 e = doc.getRootElement()
69 if e.name == root_name and e.content == root_content:
70 actual_status = "verified"
72 except libxml2.parserError:
73 actual_status = "not loaded"
75 if actual_status != exp_status:
76 print("Test '%s' failed: expect status '%s', actual '%s'" % (desc, exp_status, actual_status))
78 elif actual_err != exp_err:
79 print("Test '%s' failed" % desc)
80 print("Expect errors:")
81 for s,m in exp_err: print(" [%2d] '%s'" % (s,m))
82 print("Actual errors:")
83 for s,m in actual_err: print(" [%2d] '%s'" % (s,m))
87 # Check that we cannot read custom schema without custom callback
88 run_test(desc="Loading entity without custom callback",
89 docpath=startURL, catalog=None,
90 exp_status="not loaded", exp_err=[
93 (-1, "failed to load external entity \"py://strings/xml/sample.xml\"\n")
96 # Register handler and try to load the same entity
97 libxml2.registerInputCallback(my_input_cb)
98 run_test(desc="Loading entity with custom callback",
99 docpath=startURL, catalog=None,
100 exp_status="loaded", exp_err=[
101 (-1, "Attempt to load network entity http://example.com/dtds/sample.dtd"),
102 ( 4, "Entity 'sample.entity' not defined\n")
105 # Register a catalog (also accessible via pystr://) and retry
106 run_test(desc="Loading entity with custom callback and catalog",
107 docpath=startURL, catalog=catURL)
109 # Unregister custom callback when parser is already created
110 run_test(desc="Loading entity and unregistering callback",
111 docpath=startURL, catalog=catURL,
112 test_callback=lambda: libxml2.popInputCallbacks(),
113 exp_status="loaded", exp_err=[
114 ( 3, "failed to load external entity \"py://strings/dtds/sample.dtd\"\n"),
115 ( 4, "Entity 'sample.entity' not defined\n")
118 # Try to load the document again
119 run_test(desc="Retry loading document after unregistering callback",
120 docpath=startURL, catalog=catURL,
121 exp_status="not loaded", exp_err=[
124 (-1, "failed to load external entity \"py://strings/xml/sample.xml\"\n")
127 # But should be able to read standard I/O yet...
128 run_test(desc="Loading using standard i/o after unregistering callback",
129 docpath="tst.xml", catalog=None,
130 root_name='doc', root_content='bar')
132 # Now pop ALL input callbacks, should fail to load even standard I/O
135 libxml2.popInputCallbacks()
139 run_test(desc="Loading using standard i/o after unregistering all callbacks",
140 docpath="tst.xml", catalog=None,
141 exp_status="not loaded", exp_err=[
144 (-1, "failed to load external entity \"tst.xml\"\n")