1 #!/usr/bin/env python 2 3 "A test of macros." 4 5 import libxml2macro as x2_ 6 import xml.dom 7 8 class Container: 9 pass 10 11 doc = """<?xml version="1.0"?> 12 <doc> 13 <element attr="value"> 14 <subelement/> 15 </element> 16 </doc> 17 """ 18 19 def find_root(x2_d): 20 x2_root = None 21 22 # Property access should be transformed. 23 24 for x2_n in x2_d.childNodes: 25 if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: 26 x2_root = x2_n 27 break 28 29 return x2_root 30 31 def test(): 32 global doc 33 34 # Assignment should not be transformed. 35 36 x2_d = parseString(doc) 37 return process(x2_d) 38 39 def test_file(filename): 40 41 # Assignment should not be transformed. 42 43 x2_d = parseFile(filename) 44 return process(x2_d) 45 46 def process(x2_d): 47 48 # Not even within containers, and not special names alone. 49 50 c = Container() 51 c.x2_d = x2_d 52 53 # Find the root element. 54 55 x2_root = find_root(x2_d) 56 c.x2_root = x2_root 57 58 # Create new attributes. 59 # Method access should be transformed. 60 61 x2_root.setAttributeNS("ns", "xxx:yyy", "zzz") 62 c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ") 63 64 # Create new elements. 65 # Method access should be transformed. 66 67 x2_new = x2_d.createElementNS("ns2", "ppp:qqq") 68 x2_root.appendChild(x2_new) 69 x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ") 70 c.x2_root.appendChild(x2_new2) 71 72 # Create new elements using ownerDocument. 73 # Chaining properties is not 74 75 x2_new3 = x2_new.ownerDocument.createElement("fff") 76 x2_new.appendChild(x2_new3) 77 x2_new4 = x2_new2.ownerDocument.createElement("FFF") 78 x2_new2.appendChild(x2_new4) 79 80 return x2_d 81 82 def test_import(x2_d): 83 84 # Change the prefix. 85 86 import libxml2macro as node_ 87 node_d = x2_d 88 89 node_root = find_root(node_d) 90 91 # Create a new document. 92 93 node_d2 = createDocument("nsD", "newdoc", None) 94 node_root2 = find_root(node_d2) 95 96 # Attempt to import nodes from the original document. 97 98 node_imported = node_d2.importNode(node_root, 1) 99 node_d2.replaceChild(node_imported, node_root2) 100 101 return node_d, node_d2 102 103 if __name__ == "__main__": 104 import sys 105 if len(sys.argv) > 1: 106 test_file(sys.argv[1]) 107 else: 108 test() 109 110 # vim: tabstop=4 expandtab shiftwidth=4