blob: 404dc3553187fea201b43bebfd1aa74726f52887 [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
Jeremy Hylton3c19ec42001-07-30 21:47:25 +00006import pyexpat
Fred Drake7fbc85c2000-09-23 04:47:56 +00007from xml.parsers import expat
Fred Drake004d5e62000-10-23 17:22:08 +00008
Tim Peters2f228e72001-05-13 00:19:31 +00009from test_support import sortdict
10
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000011class Outputter:
12 def StartElementHandler(self, name, attrs):
Tim Peters2f228e72001-05-13 00:19:31 +000013 print 'Start element:\n\t', repr(name), sortdict(attrs)
Fred Drake004d5e62000-10-23 17:22:08 +000014
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000015 def EndElementHandler(self, name):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000016 print 'End element:\n\t', repr(name)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000017
18 def CharacterDataHandler(self, data):
Fred Drake265a8042000-09-21 20:32:13 +000019 data = data.strip()
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000020 if data:
21 print 'Character data:'
22 print '\t', repr(data)
23
24 def ProcessingInstructionHandler(self, target, data):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000025 print 'PI:\n\t', repr(target), repr(data)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000026
27 def StartNamespaceDeclHandler(self, prefix, uri):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000028 print 'NS decl:\n\t', repr(prefix), repr(uri)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000029
30 def EndNamespaceDeclHandler(self, prefix):
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000031 print 'End of NS decl:\n\t', repr(prefix)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000032
33 def StartCdataSectionHandler(self):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000034 print 'Start of CDATA section'
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000035
36 def EndCdataSectionHandler(self):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000037 print 'End of CDATA section'
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000038
39 def CommentHandler(self, text):
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000040 print 'Comment:\n\t', repr(text)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000041
42 def NotationDeclHandler(self, *args):
43 name, base, sysid, pubid = args
Andrew M. Kuchlinge188d522000-04-02 05:15:38 +000044 print 'Notation declared:', args
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000045
46 def UnparsedEntityDeclHandler(self, *args):
47 entityName, base, systemId, publicId, notationName = args
48 print 'Unparsed entity decl:\n\t', args
Fred Drake004d5e62000-10-23 17:22:08 +000049
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000050 def NotStandaloneHandler(self, userData):
51 print 'Not standalone'
52 return 1
Fred Drake004d5e62000-10-23 17:22:08 +000053
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000054 def ExternalEntityRefHandler(self, *args):
55 context, base, sysId, pubId = args
Fred Drake1e0611b2000-12-23 22:12:07 +000056 print 'External entity ref:', args[1:]
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000057 return 1
58
59 def DefaultHandler(self, userData):
60 pass
61
62 def DefaultHandlerExpand(self, userData):
63 pass
64
65
Fred Drake265a8042000-09-21 20:32:13 +000066def confirm(ok):
67 if ok:
68 print "OK."
69 else:
70 print "Not OK."
71
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +000072out = Outputter()
Fred Drake7fbc85c2000-09-23 04:47:56 +000073parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000074
75# Test getting/setting returns_unicode
Fred Drake265a8042000-09-21 20:32:13 +000076parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
77parser.returns_unicode = 1; confirm(parser.returns_unicode == 1)
78parser.returns_unicode = 2; confirm(parser.returns_unicode == 1)
79parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +000080
Fred Drake8f42e2b2001-04-25 16:03:54 +000081# Test getting/setting ordered_attributes
82parser.ordered_attributes = 0; confirm(parser.ordered_attributes == 0)
83parser.ordered_attributes = 1; confirm(parser.ordered_attributes == 1)
84parser.ordered_attributes = 2; confirm(parser.ordered_attributes == 1)
85parser.ordered_attributes = 0; confirm(parser.ordered_attributes == 0)
86
87# Test getting/setting specified_attributes
88parser.specified_attributes = 0; confirm(parser.specified_attributes == 0)
89parser.specified_attributes = 1; confirm(parser.specified_attributes == 1)
90parser.specified_attributes = 2; confirm(parser.specified_attributes == 1)
91parser.specified_attributes = 0; confirm(parser.specified_attributes == 0)
92
Fred Drake265a8042000-09-21 20:32:13 +000093HANDLER_NAMES = [
94 'StartElementHandler', 'EndElementHandler',
95 'CharacterDataHandler', 'ProcessingInstructionHandler',
96 'UnparsedEntityDeclHandler', 'NotationDeclHandler',
97 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
98 'CommentHandler', 'StartCdataSectionHandler',
99 'EndCdataSectionHandler',
100 'DefaultHandler', 'DefaultHandlerExpand',
101 #'NotStandaloneHandler',
102 'ExternalEntityRefHandler'
103 ]
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000104for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000105 setattr(parser, name, getattr(out, name))
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000106
Fred Drake265a8042000-09-21 20:32:13 +0000107data = '''\
108<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000109<?xml-stylesheet href="stylesheet.css"?>
110<!-- comment data -->
111<!DOCTYPE quotations SYSTEM "quotations.dtd" [
112<!ELEMENT root ANY>
113<!NOTATION notation SYSTEM "notation.jpeg">
114<!ENTITY acirc "&#226;">
115<!ENTITY external_entity SYSTEM "entity.file">
116<!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
117%unparsed_entity;
118]>
119
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000120<root attr1="value1" attr2="value2&#8000;">
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000121<myns:subelement xmlns:myns="http://www.python.org/namespace">
122 Contents of subelements
123</myns:subelement>
124<sub2><![CDATA[contents of CDATA section]]></sub2>
125&external_entity;
126</root>
Fred Drake265a8042000-09-21 20:32:13 +0000127'''
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000128
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000129# Produce UTF-8 output
130parser.returns_unicode = 0
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000131try:
132 parser.Parse(data, 1)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000133except expat.error:
134 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchlingb17664d2000-03-31 15:44:52 +0000135 print '** Line', parser.ErrorLineNumber
136 print '** Column', parser.ErrorColumnNumber
137 print '** Byte', parser.ErrorByteIndex
138
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000139# Try the parse again, this time producing Unicode output
Fred Drake7fbc85c2000-09-23 04:47:56 +0000140parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000141parser.returns_unicode = 1
142
143for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000144 setattr(parser, name, getattr(out, name))
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000145try:
146 parser.Parse(data, 1)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000147except expat.error:
148 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000149 print '** Line', parser.ErrorLineNumber
150 print '** Column', parser.ErrorColumnNumber
151 print '** Byte', parser.ErrorByteIndex
152
153# Try parsing a file
Fred Drake7fbc85c2000-09-23 04:47:56 +0000154parser = expat.ParserCreate(namespace_separator='!')
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000155parser.returns_unicode = 1
156
157for name in HANDLER_NAMES:
Fred Drake265a8042000-09-21 20:32:13 +0000158 setattr(parser, name, getattr(out, name))
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000159import StringIO
160file = StringIO.StringIO(data)
161try:
162 parser.ParseFile(file)
Fred Drake7fbc85c2000-09-23 04:47:56 +0000163except expat.error:
164 print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
Andrew M. Kuchling7fd7e362000-06-27 00:37:25 +0000165 print '** Line', parser.ErrorLineNumber
166 print '** Column', parser.ErrorColumnNumber
167 print '** Byte', parser.ErrorByteIndex
Fred Drake1e0611b2000-12-23 22:12:07 +0000168
169
170# Tests that make sure we get errors when the namespace_separator value
171# is illegal, and that we don't for good values:
172print
173print "Testing constructor for proper handling of namespace_separator values:"
174expat.ParserCreate()
175expat.ParserCreate(namespace_separator=None)
176expat.ParserCreate(namespace_separator=' ')
177print "Legal values tested o.k."
178try:
179 expat.ParserCreate(namespace_separator=42)
180except TypeError, e:
181 print "Caught expected TypeError:"
182 print e
183else:
184 print "Failed to catch expected TypeError."
Fred Drake8f42e2b2001-04-25 16:03:54 +0000185
Fred Drake1e0611b2000-12-23 22:12:07 +0000186try:
187 expat.ParserCreate(namespace_separator='too long')
188except ValueError, e:
189 print "Caught expected ValueError:"
190 print e
191else:
192 print "Failed to catch expected ValueError."
Fred Drake8f42e2b2001-04-25 16:03:54 +0000193
194# ParserCreate() needs to accept a namespace_separator of zero length
195# to satisfy the requirements of RDF applications that are required
196# to simply glue together the namespace URI and the localname. Though
197# considered a wart of the RDF specifications, it needs to be supported.
198#
199# See XML-SIG mailing list thread starting with
200# http://mail.python.org/pipermail/xml-sig/2001-April/005202.html
201#
202expat.ParserCreate(namespace_separator='') # too short