2 A library of useful helper classes to the sax classes, for the
3 convenience of application and driver writers.
8 import types
, string
, sys
11 def escape(data
, entities
= {}):
12 """Escape &, <, and > in a string of data.
13 You can escape other strings of data by passing a dictionary as
14 the optional entities parameter. The keys and values must all be
15 strings; each key will be replaced with its corresponding value.
17 data
= string
.replace(data
, "&", "&")
18 data
= string
.replace(data
, "<", "<")
19 data
= string
.replace(data
, ">", ">")
20 for chars
, entity
in entities
.items():
21 data
= string
.replace(data
, chars
, entity
)
24 class XMLGenerator(handler
.ContentHandler
):
26 def __init__(self
, out
= sys
.stdout
):
27 handler
.ContentHandler
.__init
__(self
)
30 # ContentHandler methods
32 def startDocument(self
):
33 self
._out
.write('<?xml version="1.0" encoding="iso-8859-1"?>\n')
35 def startPrefixMapping(self
, prefix
, uri
):
38 def endPrefixMapping(self
, prefix
):
41 def startElement(self
, name
, attrs
):
42 if type(name
)==type(()):
43 uri
, localname
, prefix
=name
44 name
="%s:%s"%(prefix
,localname
)
45 self
._out
.write('<' + name
)
46 for (name
, value
) in attrs
.items():
47 self
._out
.write(' %s="%s"' % (name
, escape(value
)))
50 def endElement(self
, name
):
51 # FIXME: not namespace friendly yet
52 self
._out
.write('</%s>' % name
)
54 def characters(self
, content
):
55 self
._out
.write(escape(content
))
57 def ignorableWhitespace(self
, content
):
58 self
._out
.write(content
)
60 def processingInstruction(self
, target
, data
):
61 self
._out
.write('<?%s %s?>' % (target
, data
))
64 """This class is designed to sit between an XMLReader and the
65 client application's event handlers. By default, it does nothing
66 but pass requests up to the reader and events on to the handlers
67 unmodified, but subclasses can override specific methods to modify
68 the event stream or the configuration requests as they pass
71 # ErrorHandler methods
73 def error(self
, exception
):
74 self
._err
_handler
.error(exception
)
76 def fatalError(self
, exception
):
77 self
._err
_handler
.fatalError(exception
)
79 def warning(self
, exception
):
80 self
._err
_handler
.warning(exception
)
82 # ContentHandler methods
84 def setDocumentLocator(self
, locator
):
85 self
._cont
_handler
.setDocumentLocator(locator
)
87 def startDocument(self
):
88 self
._cont
_handler
.startDocument()
90 def endDocument(self
):
91 self
._cont
_handler
.endDocument()
93 def startPrefixMapping(self
, prefix
, uri
):
94 self
._cont
_handler
.startPrefixMapping(prefix
, uri
)
96 def endPrefixMapping(self
, prefix
):
97 self
._cont
_handler
.endPrefixMapping(prefix
)
99 def startElement(self
, name
, attrs
):
100 self
._cont
_handler
.startElement(name
, attrs
)
102 def endElement(self
, name
, qname
):
103 self
._cont
_handler
.endElement(name
, qname
)
105 def characters(self
, content
):
106 self
._cont
_handler
.characters(content
)
108 def ignorableWhitespace(self
, chars
, start
, end
):
109 self
._cont
_handler
.ignorableWhitespace(chars
, start
, end
)
111 def processingInstruction(self
, target
, data
):
112 self
._cont
_handler
.processingInstruction(target
, data
)
114 def skippedEntity(self
, name
):
115 self
._cont
_handler
.skippedEntity(name
)
119 def notationDecl(self
, name
, publicId
, systemId
):
120 self
._dtd
_handler
.notationDecl(name
, publicId
, systemId
)
122 def unparsedEntityDecl(self
, name
, publicId
, systemId
, ndata
):
123 self
._dtd
_handler
.unparsedEntityDecl(name
, publicId
, systemId
, ndata
)
125 # EntityResolver methods
127 def resolveEntity(self
, publicId
, systemId
):
128 self
._ent
_handler
.resolveEntity(publicId
, systemId
)
132 def parse(self
, source
):
133 self
._parent
.setContentHandler(self
)
134 self
._parent
.setErrorHandler(self
)
135 self
._parent
.setEntityResolver(self
)
136 self
._parent
.setDTDHandler(self
)
137 self
._parent
.parse(source
)
139 def setLocale(self
, locale
):
140 self
._parent
.setLocale(locale
)
142 def getFeature(self
, name
):
143 return self
._parent
.getFeature(name
)
145 def setFeature(self
, name
, state
):
146 self
._parent
.setFeature(name
, state
)
148 def getProperty(self
, name
):
149 return self
._parent
.getProperty(name
)
151 def setProperty(self
, name
, value
):
152 self
._parent
.setProperty(name
, value
)