blob: 4657b99d89b95054576845271953b2066736b84d [file] [log] [blame]
Fred Drake45cd9de2000-06-29 19:34:54 +00001"""
2A library of useful helper classes to the sax classes, for the
3convenience of application and driver writers.
4
5$Id$
6"""
7
Paul Prescodbd8c2ae2000-07-01 19:19:32 +00008import types, string, sys
Fred Drake45cd9de2000-06-29 19:34:54 +00009import handler
10
11def 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
23
24class XMLGenerator(handler.ContentHandler):
25
26 def __init__(self, out = sys.stdout):
27 handler.ContentHandler.__init__(self)
28 self._out = out
29
30 # ContentHandler methods
31
32 def startDocument(self):
33 self._out.write('<?xml version="1.0" encoding="iso-8859-1"?>\n')
34
35 def startPrefixMapping(self, prefix, uri):
36 pass
37
38 def endPrefixMapping(self, prefix):
39 pass
40
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('>')
49
50 def endElement(self, name):
51 # FIXME: not namespace friendly yet
52 self._out.write('</%s>' % name)
53
54 def characters(self, content):
55 self._out.write(escape(content))
56
57 def ignorableWhitespace(self, content):
58 self._out.write(content)
59
60 def processingInstruction(self, target, data):
61 self._out.write('<?%s %s?>' % (target, data))
62
63class 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."""
70
71 # ErrorHandler methods
72
73 def error(self, exception):
74 self._err_handler.error(exception)
75
76 def fatalError(self, exception):
77 self._err_handler.fatalError(exception)
78
79 def warning(self, exception):
80 self._err_handler.warning(exception)
81
82 # ContentHandler methods
83
84 def setDocumentLocator(self, locator):
85 self._cont_handler.setDocumentLocator(locator)
86
87 def startDocument(self):
88 self._cont_handler.startDocument()
89
90 def endDocument(self):
91 self._cont_handler.endDocument()
92
93 def startPrefixMapping(self, prefix, uri):
94 self._cont_handler.startPrefixMapping(prefix, uri)
95
96 def endPrefixMapping(self, prefix):
97 self._cont_handler.endPrefixMapping(prefix)
98
99 def startElement(self, name, attrs):
100 self._cont_handler.startElement(name, attrs)
101
102 def endElement(self, name, qname):
103 self._cont_handler.endElement(name, qname)
104
105 def characters(self, content):
106 self._cont_handler.characters(content)
107
108 def ignorableWhitespace(self, chars, start, end):
109 self._cont_handler.ignorableWhitespace(chars, start, end)
110
111 def processingInstruction(self, target, data):
112 self._cont_handler.processingInstruction(target, data)
113
114 def skippedEntity(self, name):
115 self._cont_handler.skippedEntity(name)
116
117 # DTDHandler methods
118
119 def notationDecl(self, name, publicId, systemId):
120 self._dtd_handler.notationDecl(name, publicId, systemId)
121
122 def unparsedEntityDecl(self, name, publicId, systemId, ndata):
123 self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata)
124
125 # EntityResolver methods
126
127 def resolveEntity(self, publicId, systemId):
128 self._ent_handler.resolveEntity(publicId, systemId)
129
130 # XMLReader methods
131
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)
138
139 def setLocale(self, locale):
140 self._parent.setLocale(locale)
141
142 def getFeature(self, name):
143 return self._parent.getFeature(name)
144
145 def setFeature(self, name, state):
146 self._parent.setFeature(name, state)
147
148 def getProperty(self, name):
149 return self._parent.getProperty(name)
150
151 def setProperty(self, name, value):
152 self._parent.setProperty(name, value)
153