1
2
3 """
4 Tests for different Element class lookup mechanisms.
5 """
6
7
8 import unittest, doctest, operator
9
10 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
11 from common_imports import SillyFileLike, canonicalize
12
13 xml_str = '''\
14 <root xmlns="myNS" xmlns:other="otherNS">
15 <c1 a1="A1" a2="A2" other:a3="A3">
16 <c2 a1="C2">0</c2>
17 <c2>1</c2>
18 <other:c2>2</other:c2>
19 </c1>
20 </root>'''
21
47
49 class TestElement(etree.ElementBase):
50 FIND_ME = "default element"
51 class TestComment(etree.CommentBase):
52 FIND_ME = "default comment"
53 class TestPI(etree.PIBase):
54 FIND_ME = "default pi"
55
56 parser = etree.XMLParser()
57
58 lookup = etree.ElementDefaultClassLookup(
59 element=TestElement, comment=TestComment, pi=TestPI)
60 parser.setElementClassLookup(lookup)
61
62 root = etree.XML("""<?xml version='1.0'?>
63 <root>
64 <?myPI?>
65 <!-- hi -->
66 </root>
67 """, parser)
68
69 self.assertEquals("default element", root.FIND_ME)
70 self.assertEquals("default pi", root[0].FIND_ME)
71 self.assertEquals("default comment", root[1].FIND_ME)
72
74 class TestElement(etree.ElementBase):
75 FIND_ME = "attribute_based"
76
77 class_dict = {"A1" : TestElement}
78
79 lookup = etree.AttributeBasedElementClassLookup(
80 "a1", class_dict)
81 etree.setElementClassLookup(lookup)
82
83 root = etree.XML(xml_str)
84 self.assertFalse(hasattr(root, 'FIND_ME'))
85 self.assertEquals(root[0].FIND_ME,
86 TestElement.FIND_ME)
87 self.assertFalse(hasattr(root[0][0], 'FIND_ME'))
88
90 class TestElement(etree.ElementBase):
91 FIND_ME = "custom"
92
93 class MyLookup(etree.CustomElementClassLookup):
94 def lookup(self, t, d, ns, name):
95 if name == 'c1':
96 return TestElement
97
98 etree.setElementClassLookup( MyLookup() )
99
100 root = etree.XML(xml_str)
101 self.assertFalse(hasattr(root, 'FIND_ME'))
102 self.assertEquals(root[0].FIND_ME,
103 TestElement.FIND_ME)
104 self.assertFalse(hasattr(root[0][1], 'FIND_ME'))
105
107 class TestElement1(etree.ElementBase):
108 FIND_ME = "custom"
109
110 class TestElement2(etree.ElementBase):
111 FIND_ME = "nsclasses"
112
113 class MyLookup(etree.CustomElementClassLookup):
114 def lookup(self, t, d, ns, name):
115 if name == 'c1':
116 return TestElement1
117
118 lookup = etree.ElementNamespaceClassLookup( MyLookup() )
119 etree.setElementClassLookup(lookup)
120
121 ns = lookup.get_namespace("otherNS")
122 ns[None] = TestElement2
123
124 root = etree.XML(xml_str)
125 self.assertFalse(hasattr(root, 'FIND_ME'))
126 self.assertEquals(root[0].FIND_ME,
127 TestElement1.FIND_ME)
128 self.assertFalse(hasattr(root[0][1], 'FIND_ME'))
129 self.assertEquals(root[0][-1].FIND_ME,
130 TestElement2.FIND_ME)
131
133 class TestElement(etree.ElementBase):
134 FIND_ME = "parser_based"
135
136 lookup = etree.ParserBasedElementClassLookup()
137 etree.setElementClassLookup(lookup)
138
139 class MyLookup(etree.CustomElementClassLookup):
140 def lookup(self, t, d, ns, name):
141 return TestElement
142
143 parser = etree.XMLParser()
144 parser.setElementClassLookup( MyLookup() )
145
146 root = etree.parse(StringIO(xml_str), parser).getroot()
147 self.assertEquals(root.FIND_ME,
148 TestElement.FIND_ME)
149 self.assertEquals(root[0].FIND_ME,
150 TestElement.FIND_ME)
151
152 root = etree.parse(StringIO(xml_str)).getroot()
153 self.assertFalse(hasattr(root, 'FIND_ME'))
154 self.assertFalse(hasattr(root[0], 'FIND_ME'))
155
157 class Lookup(etree.CustomElementClassLookup):
158 def __init__(self):
159
160 pass
161
162 def lookup(self, node_type, document, namespace, name):
163 return Foo
164
165 class Foo(etree.ElementBase):
166 def custom(self):
167 return "test"
168
169 parser = self.etree.XMLParser()
170 parser.set_element_class_lookup( Lookup() )
171
172 root = etree.XML('<foo/>', parser)
173
174 self.assertEquals("test", root.custom())
175
176
178 suite = unittest.TestSuite()
179 suite.addTests([unittest.makeSuite(ClassLookupTestCase)])
180 return suite
181
182 if __name__ == '__main__':
183 print 'to test use test.py %s' % __file__
184