Bump version to 0.9.1.
[python/dscho.git] / Lib / xml / sax / saxutils.py
blob4657b99d89b95054576845271953b2066736b84d
1 """
2 A library of useful helper classes to the sax classes, for the
3 convenience of application and driver writers.
5 $Id$
6 """
8 import types, string, sys
9 import handler
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.
16 """
17 data = string.replace(data, "&", "&amp;")
18 data = string.replace(data, "<", "&lt;")
19 data = string.replace(data, ">", "&gt;")
20 for chars, entity in entities.items():
21 data = string.replace(data, chars, entity)
22 return data
24 class XMLGenerator(handler.ContentHandler):
26 def __init__(self, out = sys.stdout):
27 handler.ContentHandler.__init__(self)
28 self._out = out
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):
36 pass
38 def endPrefixMapping(self, prefix):
39 pass
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)))
48 self._out.write('>')
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))
63 class XMLFilterBase:
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
69 through."""
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)
117 # DTDHandler methods
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)
130 # XMLReader methods
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)