Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 1 | # Very simple test - Parse a file and print what happens |
| 2 | |
| 3 | # XXX TypeErrors on calling handlers, or on bad return values from a |
| 4 | # handler, are obscure and unhelpful. |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 5 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 6 | from xml.parsers import expat |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 8 | class Outputter: |
| 9 | def StartElementHandler(self, name, attrs): |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 10 | print 'Start element:\n\t', repr(name), attrs |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 11 | |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 12 | def EndElementHandler(self, name): |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 13 | print 'End element:\n\t', repr(name) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 14 | |
| 15 | def CharacterDataHandler(self, data): |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 16 | data = data.strip() |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 17 | if data: |
| 18 | print 'Character data:' |
| 19 | print '\t', repr(data) |
| 20 | |
| 21 | def ProcessingInstructionHandler(self, target, data): |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 22 | print 'PI:\n\t', repr(target), repr(data) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 23 | |
| 24 | def StartNamespaceDeclHandler(self, prefix, uri): |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 25 | print 'NS decl:\n\t', repr(prefix), repr(uri) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 26 | |
| 27 | def EndNamespaceDeclHandler(self, prefix): |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 28 | print 'End of NS decl:\n\t', repr(prefix) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 29 | |
| 30 | def StartCdataSectionHandler(self): |
Andrew M. Kuchling | e188d52 | 2000-04-02 05:15:38 +0000 | [diff] [blame] | 31 | print 'Start of CDATA section' |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 32 | |
| 33 | def EndCdataSectionHandler(self): |
Andrew M. Kuchling | e188d52 | 2000-04-02 05:15:38 +0000 | [diff] [blame] | 34 | print 'End of CDATA section' |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 35 | |
| 36 | def CommentHandler(self, text): |
Andrew M. Kuchling | e188d52 | 2000-04-02 05:15:38 +0000 | [diff] [blame] | 37 | print 'Comment:\n\t', repr(text) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 38 | |
| 39 | def NotationDeclHandler(self, *args): |
| 40 | name, base, sysid, pubid = args |
Andrew M. Kuchling | e188d52 | 2000-04-02 05:15:38 +0000 | [diff] [blame] | 41 | print 'Notation declared:', args |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 42 | |
| 43 | def UnparsedEntityDeclHandler(self, *args): |
| 44 | entityName, base, systemId, publicId, notationName = args |
| 45 | print 'Unparsed entity decl:\n\t', args |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 46 | |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 47 | def NotStandaloneHandler(self, userData): |
| 48 | print 'Not standalone' |
| 49 | return 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 50 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 51 | def ExternalEntityRefHandler(self, *args): |
| 52 | context, base, sysId, pubId = args |
Fred Drake | 1e0611b | 2000-12-23 22:12:07 +0000 | [diff] [blame] | 53 | print 'External entity ref:', args[1:] |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 54 | return 1 |
| 55 | |
| 56 | def DefaultHandler(self, userData): |
| 57 | pass |
| 58 | |
| 59 | def DefaultHandlerExpand(self, userData): |
| 60 | pass |
| 61 | |
| 62 | |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 63 | def confirm(ok): |
| 64 | if ok: |
| 65 | print "OK." |
| 66 | else: |
| 67 | print "Not OK." |
| 68 | |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 69 | out = Outputter() |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 70 | parser = expat.ParserCreate(namespace_separator='!') |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 71 | |
| 72 | # Test getting/setting returns_unicode |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 73 | parser.returns_unicode = 0; confirm(parser.returns_unicode == 0) |
| 74 | parser.returns_unicode = 1; confirm(parser.returns_unicode == 1) |
| 75 | parser.returns_unicode = 2; confirm(parser.returns_unicode == 1) |
| 76 | parser.returns_unicode = 0; confirm(parser.returns_unicode == 0) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 77 | |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 78 | HANDLER_NAMES = [ |
| 79 | 'StartElementHandler', 'EndElementHandler', |
| 80 | 'CharacterDataHandler', 'ProcessingInstructionHandler', |
| 81 | 'UnparsedEntityDeclHandler', 'NotationDeclHandler', |
| 82 | 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', |
| 83 | 'CommentHandler', 'StartCdataSectionHandler', |
| 84 | 'EndCdataSectionHandler', |
| 85 | 'DefaultHandler', 'DefaultHandlerExpand', |
| 86 | #'NotStandaloneHandler', |
| 87 | 'ExternalEntityRefHandler' |
| 88 | ] |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 89 | for name in HANDLER_NAMES: |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 90 | setattr(parser, name, getattr(out, name)) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 91 | |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 92 | data = '''\ |
| 93 | <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 94 | <?xml-stylesheet href="stylesheet.css"?> |
| 95 | <!-- comment data --> |
| 96 | <!DOCTYPE quotations SYSTEM "quotations.dtd" [ |
| 97 | <!ELEMENT root ANY> |
| 98 | <!NOTATION notation SYSTEM "notation.jpeg"> |
| 99 | <!ENTITY acirc "â"> |
| 100 | <!ENTITY external_entity SYSTEM "entity.file"> |
| 101 | <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> |
| 102 | %unparsed_entity; |
| 103 | ]> |
| 104 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 105 | <root attr1="value1" attr2="value2ὀ"> |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 106 | <myns:subelement xmlns:myns="http://www.python.org/namespace"> |
| 107 | Contents of subelements |
| 108 | </myns:subelement> |
| 109 | <sub2><![CDATA[contents of CDATA section]]></sub2> |
| 110 | &external_entity; |
| 111 | </root> |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 112 | ''' |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 113 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 114 | # Produce UTF-8 output |
| 115 | parser.returns_unicode = 0 |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 116 | try: |
| 117 | parser.Parse(data, 1) |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 118 | except expat.error: |
| 119 | print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode) |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 120 | print '** Line', parser.ErrorLineNumber |
| 121 | print '** Column', parser.ErrorColumnNumber |
| 122 | print '** Byte', parser.ErrorByteIndex |
| 123 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 124 | # Try the parse again, this time producing Unicode output |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 125 | parser = expat.ParserCreate(namespace_separator='!') |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 126 | parser.returns_unicode = 1 |
| 127 | |
| 128 | for name in HANDLER_NAMES: |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 129 | setattr(parser, name, getattr(out, name)) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 130 | try: |
| 131 | parser.Parse(data, 1) |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 132 | except expat.error: |
| 133 | print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 134 | print '** Line', parser.ErrorLineNumber |
| 135 | print '** Column', parser.ErrorColumnNumber |
| 136 | print '** Byte', parser.ErrorByteIndex |
| 137 | |
| 138 | # Try parsing a file |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 139 | parser = expat.ParserCreate(namespace_separator='!') |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 140 | parser.returns_unicode = 1 |
| 141 | |
| 142 | for name in HANDLER_NAMES: |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 143 | setattr(parser, name, getattr(out, name)) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 144 | import StringIO |
| 145 | file = StringIO.StringIO(data) |
| 146 | try: |
| 147 | parser.ParseFile(file) |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 148 | except expat.error: |
| 149 | print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 150 | print '** Line', parser.ErrorLineNumber |
| 151 | print '** Column', parser.ErrorColumnNumber |
| 152 | print '** Byte', parser.ErrorByteIndex |
Fred Drake | 1e0611b | 2000-12-23 22:12:07 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | # Tests that make sure we get errors when the namespace_separator value |
| 156 | # is illegal, and that we don't for good values: |
| 157 | print |
| 158 | print "Testing constructor for proper handling of namespace_separator values:" |
| 159 | expat.ParserCreate() |
| 160 | expat.ParserCreate(namespace_separator=None) |
| 161 | expat.ParserCreate(namespace_separator=' ') |
| 162 | print "Legal values tested o.k." |
| 163 | try: |
| 164 | expat.ParserCreate(namespace_separator=42) |
| 165 | except TypeError, e: |
| 166 | print "Caught expected TypeError:" |
| 167 | print e |
| 168 | else: |
| 169 | print "Failed to catch expected TypeError." |
| 170 | try: |
| 171 | expat.ParserCreate(namespace_separator='too long') |
| 172 | except ValueError, e: |
| 173 | print "Caught expected ValueError:" |
| 174 | print e |
| 175 | else: |
| 176 | print "Failed to catch expected ValueError." |
| 177 | try: |
| 178 | expat.ParserCreate(namespace_separator='') # too short |
| 179 | except ValueError, e: |
| 180 | print "Caught expected ValueError:" |
| 181 | print e |
| 182 | else: |
| 183 | print "Failed to catch expected ValueError." |