blob: fecb27ea810dd12cf05331ab4c02f6a065f1719d [file] [log] [blame]
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +00001# 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 Drake004d5e62000-10-23 17:22:08 +00005
Fred Drake7fbc85c2000-09-23 04:47:56 +00006from xml.parsers import expat
Fred Drake004d5e62000-10-23 17:22:08 +00007
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +00008class Outputter:
9 def StartElementHandler(self, name, attrs):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000010 print 'Start element:\n\t', repr(name), attrs
Fred Drake004d5e62000-10-23 17:22:08 +000011
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000012 def EndElementHandler(self, name):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000013 print 'End element:\n\t', repr(name)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000014
15 def CharacterDataHandler(self, data):
Fred Drake265a8042000-09-21 20:32:13 +000016 data = data.strip()
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000017 if data:
18 print 'Character data:'
19 print '\t', repr(data)
20
21 def ProcessingInstructionHandler(self, target, data):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000022 print 'PI:\n\t', repr(target), repr(data)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000023
24 def StartNamespaceDeclHandler(self, prefix, uri):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000025 print 'NS decl:\n\t', repr(prefix), repr(uri)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000026
27 def EndNamespaceDeclHandler(self, prefix):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000028 print 'End of NS decl:\n\t', repr(prefix)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000029
30 def StartCdataSectionHandler(self):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000031 print 'Start of CDATA section'
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000032
33 def EndCdataSectionHandler(self):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000034 print 'End of CDATA section'
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000035
36 def CommentHandler(self, text):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000037 print 'Comment:\n\t', repr(text)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000038
39 def NotationDeclHandler(self, *args):
40 name, base, sysid, pubid = args
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000041 print 'Notation declared:', args
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000042
43 def UnparsedEntityDeclHandler(self, *args):
44 entityName, base, systemId, publicId, notationName = args
45 print 'Unparsed entity decl:\n\t', args
Fred Drake004d5e62000-10-23 17:22:08 +000046
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000047 def NotStandaloneHandler(self, userData):
48 print 'Not standalone'
49 return 1
Fred Drake004d5e62000-10-23 17:22:08 +000050
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000051 def ExternalEntityRefHandler(self, *args):
52 context, base, sysId, pubId = args
Fred Drake1e0611b2000-12-23 22:12:07 +000053 print 'External entity ref:', args[1:]
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000054 return 1
55
56 def DefaultHandler(self, userData):
57 pass
58
59 def DefaultHandlerExpand(self, userData):
60 pass
61
62
Fred Drake265a8042000-09-21 20:32:13 +000063def confirm(ok):
64 if ok:
65 print "OK."
66 else:
67 print "Not OK."
68
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000069out = Outputter()
Fred Drake7fbc85c2000-09-23 04:47:56 +000070parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000071
72# Test getting/setting returns_unicode
Fred Drake265a8042000-09-21 20:32:13 +000073parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
74parser.returns_unicode = 1; confirm(parser.returns_unicode == 1)
75parser.returns_unicode = 2; confirm(parser.returns_unicode == 1)
76parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000077
Fred Drake8f42e2b2001-04-25 16:03:54 +000078# Test getting/setting ordered_attributes
79parser.ordered_attributes = 0; confirm(parser.ordered_attributes == 0)
80parser.ordered_attributes = 1; confirm(parser.ordered_attributes == 1)
81parser.ordered_attributes = 2; confirm(parser.ordered_attributes == 1)
82parser.ordered_attributes = 0; confirm(parser.ordered_attributes == 0)
83
84# Test getting/setting specified_attributes
85parser.specified_attributes = 0; confirm(parser.specified_attributes == 0)
86parser.specified_attributes = 1; confirm(parser.specified_attributes == 1)
87parser.specified_attributes = 2; confirm(parser.specified_attributes == 1)
88parser.specified_attributes = 0; confirm(parser.specified_attributes == 0)
89
Fred Drake265a8042000-09-21 20:32:13 +000090HANDLER_NAMES = [
91 'StartElementHandler', 'EndElementHandler',
92 'CharacterDataHandler', 'ProcessingInstructionHandler',
93 'UnparsedEntityDeclHandler', 'NotationDeclHandler',
94 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
95 'CommentHandler', 'StartCdataSectionHandler',
96 'EndCdataSectionHandler',
97 'DefaultHandler', 'DefaultHandlerExpand',
98 #'NotStandaloneHandler',
99 'ExternalEntityRefHandler'
100 ]
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000101for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000102 setattr(parser, name, getattr(out, name))
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000103
Fred Drake265a8042000-09-21 20:32:13 +0000104data = '''\
105<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000106<?xml-stylesheet href="stylesheet.css"?>
107<!-- comment data -->
108<!DOCTYPE quotations SYSTEM "quotations.dtd" [
109<!ELEMENT root ANY>
110<!NOTATION notation SYSTEM "notation.jpeg">
111<!ENTITY acirc "&#226;">
112<!ENTITY external_entity SYSTEM "entity.file">
113<!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
114%unparsed_entity;
115]>
116
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000117<root attr1="value1" attr2="value2&#8000;">
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000118<myns:subelement xmlns:myns="http://www.python.org/namespace">
119 Contents of subelements
120</myns:subelement>
121<sub2><![CDATA[contents of CDATA section]]></sub2>
122&external_entity;
123</root>
Fred Drake265a8042000-09-21 20:32:13 +0000124'''
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000125
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000126# Produce UTF-8 output
127parser.returns_unicode = 0
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000128try:
129 parser.Parse(data, 1)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000130except expat.error:
131 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000132 print '** Line', parser.ErrorLineNumber
133 print '** Column', parser.ErrorColumnNumber
134 print '** Byte', parser.ErrorByteIndex
135
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000136# Try the parse again, this time producing Unicode output
Fred Drake7fbc85c2000-09-23 04:47:56 +0000137parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000138parser.returns_unicode = 1
139
140for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000141 setattr(parser, name, getattr(out, name))
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000142try:
143 parser.Parse(data, 1)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000144except expat.error:
145 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000146 print '** Line', parser.ErrorLineNumber
147 print '** Column', parser.ErrorColumnNumber
148 print '** Byte', parser.ErrorByteIndex
149
150# Try parsing a file
Fred Drake7fbc85c2000-09-23 04:47:56 +0000151parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000152parser.returns_unicode = 1
153
154for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000155 setattr(parser, name, getattr(out, name))
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000156import StringIO
157file = StringIO.StringIO(data)
158try:
159 parser.ParseFile(file)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000160except expat.error:
161 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000162 print '** Line', parser.ErrorLineNumber
163 print '** Column', parser.ErrorColumnNumber
164 print '** Byte', parser.ErrorByteIndex
Fred Drake1e0611b2000-12-23 22:12:07 +0000165
166
167# Tests that make sure we get errors when the namespace_separator value
168# is illegal, and that we don't for good values:
169print
170print "Testing constructor for proper handling of namespace_separator values:"
171expat.ParserCreate()
172expat.ParserCreate(namespace_separator=None)
173expat.ParserCreate(namespace_separator=' ')
174print "Legal values tested o.k."
175try:
176 expat.ParserCreate(namespace_separator=42)
177except TypeError, e:
178 print "Caught expected TypeError:"
179 print e
180else:
181 print "Failed to catch expected TypeError."
Fred Drake8f42e2b2001-04-25 16:03:54 +0000182
Fred Drake1e0611b2000-12-23 22:12:07 +0000183try:
184 expat.ParserCreate(namespace_separator='too long')
185except ValueError, e:
186 print "Caught expected ValueError:"
187 print e
188else:
189 print "Failed to catch expected ValueError."
Fred Drake8f42e2b2001-04-25 16:03:54 +0000190
191# ParserCreate() needs to accept a namespace_separator of zero length
192# to satisfy the requirements of RDF applications that are required
193# to simply glue together the namespace URI and the localname. Though
194# considered a wart of the RDF specifications, it needs to be supported.
195#
196# See XML-SIG mailing list thread starting with
197# http://mail.python.org/pipermail/xml-sig/2001-April/005202.html
198#
199expat.ParserCreate(namespace_separator='') # too short