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