""" $Id: XMLParserFunc.py,v 1.4 2000/10/17 02:40:38 kmacleod Exp $ """ import unittest import string import re from StringIO import StringIO import Orchard import Orchard.Parsers.SAX xml_test_file = 'test.xml' xml_string = \ """ text5 """ xmlns_ns = "http://www.w3.org/2000/xmlns/" class MyHandler: def startDocument(self, document): if document.node_type is Orchard.XML.DocumentType: self.tests_21 = 1 else: self.fail_startDocument = 1 def endDocument(self, document): if document.node_type is Orchard.XML.DocumentType: self.tests_22 = 1 else: self.fail_endDocument = 1 def startElement(self, element): if ( element.node_type is Orchard.XML.ElementType and element.name == 'el1' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el1' and len(element.attributes) == 1 and element.attributes.attr1.name == 'attr1' and element.attributes.attr1.value == 'text1' and not hasattr(element.attributes.attr1, 'prefix') and element.attributes.attr1.namespace_uri == None ): self.tests_1 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el2' and element.namespace_uri == 'uri:ns1' and not hasattr(element, 'prefix') and element.local_name == 'el2' and len(element.attributes) == 1 and element.attributes.xmlns.name == 'xmlns' and element.attributes.xmlns.value == 'uri:ns1' and not hasattr(element.attributes.xmlns, 'Prefix') and element.attributes.xmlns.namespace_uri == None ): self.tests_2 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el3' and element.namespace_uri == 'uri:ns1' and not hasattr(element, 'prefix') and element.local_name == 'el3' and len(element.attributes) == 1 and element.attributes[('uri:ns1', 'attr2')].name == 'attr2' and element.attributes[('uri:ns1', 'attr2')].value == 'text2' and not hasattr(element.attributes[('uri:ns1', 'attr2')], 'prefix') and element.attributes[('uri:ns1', 'attr2')].namespace_uri == 'uri:ns1' ): self.tests_3 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el4' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el4' and len(element.attributes) == 1 and element.attributes[(xmlns_ns, 'ns2')].name == 'xmlns:ns2' and element.attributes[(xmlns_ns, 'ns2')].value == 'uri:ns2' and element.attributes[(xmlns_ns, 'ns2')].namespace_uri == xmlns_ns and element.attributes[(xmlns_ns, 'ns2')].prefix == 'xmlns' ): self.tests_4 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'ns2:el5' and element.namespace_uri == 'uri:ns2' and element.prefix == 'ns2' and element.local_name == 'el5' and len(element.attributes) == 2 and element.attributes.attr3.name == 'attr3' and element.attributes.attr3.value == 'text3' and element.attributes.attr3.namespace_uri == None and not hasattr(element.attributes.attr3, 'prefix') and element.attributes[('uri:ns2', 'attr4')].name == 'ns2:attr4' and element.attributes[('uri:ns2', 'attr4')].value == 'text4' and element.attributes[('uri:ns2', 'attr4')].namespace_uri == 'uri:ns2' and element.attributes[('uri:ns2', 'attr4')].prefix == 'ns2' ): self.tests_5 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el6' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el6' and len(element.attributes) == 0 ): self.tests_6 = 1 else: self.fail_startElement = 1 def endElement(self, element): if ( element.node_type is Orchard.XML.ElementType and element.name == 'el1' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el1' and len(element.attributes) == 1 and element.attributes.attr1.name == 'attr1' and element.attributes.attr1.value == 'text1' and not hasattr(element.attributes.attr1, 'prefix') and element.attributes.attr1.namespace_uri == None ): self.tests_7 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el2' and element.namespace_uri == 'uri:ns1' and not hasattr(element, 'prefix') and element.local_name == 'el2' and len(element.attributes) == 1 and element.attributes.xmlns.name == 'xmlns' and element.attributes.xmlns.value == 'uri:ns1' and not hasattr(element.attributes.xmlns, 'Prefix') and element.attributes.xmlns.namespace_uri == None ): self.tests_8 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el3' and element.namespace_uri == 'uri:ns1' and not hasattr(element, 'prefix') and element.local_name == 'el3' and len(element.attributes) == 1 and element.attributes[('uri:ns1', 'attr2')].name == 'attr2' and element.attributes[('uri:ns1', 'attr2')].value == 'text2' and not hasattr(element.attributes[('uri:ns1', 'attr2')], 'prefix') and element.attributes[('uri:ns1', 'attr2')].namespace_uri == 'uri:ns1' ): self.tests_9 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el4' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el4' and len(element.attributes) == 1 and element.attributes[(xmlns_ns, 'ns2')].name == 'xmlns:ns2' and element.attributes[(xmlns_ns, 'ns2')].value == 'uri:ns2' and element.attributes[(xmlns_ns, 'ns2')].namespace_uri == xmlns_ns and element.attributes[(xmlns_ns, 'ns2')].prefix == 'xmlns' ): self.tests_10 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'ns2:el5' and element.namespace_uri == 'uri:ns2' and element.prefix == 'ns2' and element.local_name == 'el5' and len(element.attributes) == 2 and element.attributes.attr3.name == 'attr3' and element.attributes.attr3.value == 'text3' and element.attributes.attr3.namespace_uri == None and not hasattr(element.attributes.attr3, 'prefix') and element.attributes[('uri:ns2', 'attr4')].name == 'ns2:attr4' and element.attributes[('uri:ns2', 'attr4')].value == 'text4' and element.attributes[('uri:ns2', 'attr4')].namespace_uri == 'uri:ns2' and element.attributes[('uri:ns2', 'attr4')].prefix == 'ns2' ): self.tests_11 = 1 elif ( element.node_type is Orchard.XML.ElementType and element.name == 'el6' and element.namespace_uri == None and not hasattr(element, 'prefix') and element.local_name == 'el6' and len(element.attributes) == 0 ): self.tests_12 = 1 else: self.fail_endElement = 1 def characters(self, characters): if ( characters.node_type is Orchard.XML.CharactersType and re.match(r'^\s*$', characters.data) != None ): self.tests_13 = 1 elif ( characters.node_type is Orchard.XML.CharactersType and characters.data == 'text5' ): self.tests_14 = 1 else: self.fail_characters = 1 def ignorableWhitespace(self, characters): if ( characters.node_type is Orchard.XML.IgnorableWhitespaceType and re.match(r'^\s*$', characters.data) != None ): self.tests_23 = 1 else: self.fail_ignorableWhitespace = 1 def processingInstruction(self, pi): if ( pi.node_type is Orchard.XML.ProcessingInstructionType and pi.target == 'pi1' and pi.data == 'data1' ): self.tests_18 = 1 elif ( pi.node_type is Orchard.XML.ProcessingInstructionType and pi.target == 'pi2' and pi.data == 'data2' ): self.tests_19 = 1 elif ( pi.node_type is Orchard.XML.ProcessingInstructionType and pi.target == 'pi3' and pi.data == 'data3' ): self.tests_20 = 1 else: self.fail_processingInstruction = 1 class DummyHandler: pass class OriginalHandler: def startDocument(self, document): self.in_original_handler = 1 class XMLParserFuncTestCase(unittest.TestCase): def checkParser(self): handler = MyHandler() parser = Orchard.Parsers.SAX.SAX( handler=handler ) parser.parse(xml_string) assert hasattr(handler, 'tests_21') assert not hasattr(handler, 'fail_startDocument') assert hasattr(handler, 'tests_22') assert not hasattr(handler, 'fail_endDocument') assert hasattr(handler, 'tests_1') assert hasattr(handler, 'tests_2') assert hasattr(handler, 'tests_3') assert hasattr(handler, 'tests_4') assert hasattr(handler, 'tests_5') assert hasattr(handler, 'tests_6') assert not hasattr(handler, 'fail_startElement') assert hasattr(handler, 'tests_7') assert hasattr(handler, 'tests_8') assert hasattr(handler, 'tests_9') assert hasattr(handler, 'tests_10') assert hasattr(handler, 'tests_11') assert hasattr(handler, 'tests_12') assert not hasattr(handler, 'fail_endElement') assert hasattr(handler, 'tests_13') assert hasattr(handler, 'tests_14') assert not hasattr(handler, 'fail_characters') assert hasattr(handler, 'tests_18') assert hasattr(handler, 'tests_19') assert hasattr(handler, 'tests_20') assert not hasattr(handler, 'fail_processingInstruction') # SAX_expat doesn't report whitespace outside the root element # as ignorable, whereas SAX_XPParser apparently does if ( hasattr(handler, 'tests_23') or hasattr(handler, 'fail_ignorableWhitespace') ): assert hasattr(handler, 'tests_23') assert not hasattr(handler, 'fail_ignorableWhitespace') def checkParseFile(self): # [I3:3]parse() a file handler = MyHandler() parser = Orchard.Parsers.SAX.SAX() xml_file = StringIO(xml_string) parser.parse( xml_file, handler=handler ) assert hasattr(handler, 'tests_21') def checkParseString(self): # [I3:3]parse() a string handler = MyHandler() parser = Orchard.Parsers.SAX.SAX() parser.parse( xml_string, handler=handler ) assert hasattr(handler, 'tests_21') def checkParse(self): # [I3:3]parse(), with Handler option handler = MyHandler() parser = Orchard.Parsers.SAX.SAX() parser.parse( xml_test_file, handler=handler ) assert hasattr(handler, 'tests_21') def checkExceptions(self): # [I3:3]exceptions handler = MyHandler() parser = Orchard.Parsers.SAX.SAX() try: parser.parse( xml_string + 'blah', handler=handler ) raise AssertionError, "should not parse 'blah' correctly!" except Orchard.Parsers.SAX.SAXException: pass def checkOverridingOptions(self): # [I3:3]parse() options override new() options orig_handler = OriginalHandler() handler = MyHandler() parser = Orchard.Parsers.SAX.SAX( handler=orig_handler ) parser.parse( xml_string, handler=handler ) assert hasattr(handler, 'tests_21') # this is a unit test parser.parse( xml_string ) assert hasattr(orig_handler, 'in_original_handler') def checkMissingHandlers(self): # [I3:3]missing handlers are ok handler = DummyHandler() parser = Orchard.Parsers.SAX.SAX( handler=None ) parser.parse( xml_string, handler=handler ) def suite(): return unittest.makeSuite(XMLParserFuncTestCase,'check') if __name__ == "__main__": unittest.TextTestRunner().run(suite())