3 """An XML Reader is the SAX 2 name for an XML parser. XML Parsers
4 should be based on this code. """
5 # ===== XMLREADER =====
9 self
._cont
_handler
= handler
.ContentHandler()
10 #self._dtd_handler = handler.DTDHandler()
11 #self._ent_handler = handler.EntityResolver()
12 self
._err
_handler
= handler
.ErrorHandler()
14 def parse(self
, source
):
15 "Parse an XML document from a system identifier or an InputSource."
16 raise NotImplementedError("This method must be implemented!")
18 def getContentHandler(self
):
19 "Returns the current ContentHandler."
20 return self
._cont
_handler
22 def setContentHandler(self
, handler
):
23 "Registers a new object to receive document content events."
24 self
._cont
_handler
= handler
26 def getDTDHandler(self
):
27 "Returns the current DTD handler."
28 return self
._dtd
_handler
30 def setDTDHandler(self
, handler
):
31 "Register an object to receive basic DTD-related events."
32 self
._dtd
_handler
= handler
34 def getEntityResolver(self
):
35 "Returns the current EntityResolver."
36 return self
._ent
_handler
38 def setEntityResolver(self
, resolver
):
39 "Register an object to resolve external entities."
40 self
._ent
_handler
= resolver
42 def getErrorHandler(self
):
43 "Returns the current ErrorHandler."
44 return self
._err
_handler
46 def setErrorHandler(self
, handler
):
47 "Register an object to receive error-message events."
48 self
._err
_handler
= handler
50 def setLocale(self
, locale
):
51 """Allow an application to set the locale for errors and warnings.
53 SAX parsers are not required to provide localization for errors
54 and warnings; if they cannot support the requested locale,
55 however, they must throw a SAX exception. Applications may
56 request a locale change in the middle of a parse."""
57 raise SAXNotSupportedException("Locale support not implemented")
59 def getFeature(self
, name
):
60 "Looks up and returns the state of a SAX2 feature."
61 raise SAXNotRecognizedException("Feature '%s' not recognized" % name
)
63 def setFeature(self
, name
, state
):
64 "Sets the state of a SAX2 feature."
65 raise SAXNotRecognizedException("Feature '%s' not recognized" % name
)
67 def getProperty(self
, name
):
68 "Looks up and returns the value of a SAX2 property."
69 raise SAXNotRecognizedException("Property '%s' not recognized" % name
)
71 def setProperty(self
, name
, value
):
72 "Sets the value of a SAX2 property."
73 raise SAXNotRecognizedException("Property '%s' not recognized" % name
)
76 class IncrementalParser(XMLReader
):
77 """This interface adds three extra methods to the XMLReader
78 interface that allow XML parsers to support incremental
79 parsing. Support for this interface is optional, since not all
80 underlying XML parsers support this functionality.
82 When the parser is instantiated it is ready to begin accepting
83 data from the feed method immediately. After parsing has been
84 finished with a call to close the reset method must be called to
85 make the parser ready to accept new data, either from feed or
86 using the parse method.
88 Note that these methods must _not_ be called during parsing, that
89 is, after parse has been called and before it returns.
91 By default, the class also implements the parse method of the XMLReader
92 interface using the feed, close and reset methods of the
93 IncrementalParser interface as a convenience to SAX 2.0 driver
95 def __init__(self
, bufsize
=2**16 ):
97 XMLReader
.__init
__( self
)
99 def parse(self
, source
):
100 self
.prepareParser(source
)
101 #FIXME: do some type checking: could be already stream, URL or
104 buffer = inf
.read(self
._bufsize
)
107 buffer = inf
.read(self
._bufsize
)
111 def feed(self
, data
):
112 """This method gives the raw XML data in the data parameter to
113 the parser and makes it parse the data, emitting the
114 corresponding events. It is allowed for XML constructs to be
115 split across several calls to feed.
117 feed may raise SAXException."""
118 raise NotImplementedError("This method must be implemented!")
119 def prepareParser(self
, source
):
120 """This method is called by the parse implementation to allow
121 the SAX 2.0 driver to prepare itself for parsing."""
122 raise NotImplementedError("prepareParser must be overridden!")
125 """This method is called when the entire XML document has been
126 passed to the parser through the feed method, to notify the
127 parser that there are no more data. This allows the parser to
128 do the final checks on the document and empty the internal
131 The parser will not be ready to parse another document until
132 the reset method has been called.
134 close may raise SAXException."""
135 raise NotImplementedError("This method must be implemented!")
138 """This method is called after close has been called to reset
139 the parser so that it is ready to parse new documents. The
140 results of calling parse or feed after close without calling
141 reset are undefined."""
142 raise NotImplementedError("This method must be implemented!")
144 # ===== LOCATOR =====
146 """Interface for associating a SAX event with a document
147 location. A locator object will return valid results only during
148 calls to DocumentHandler methods; at any other time, the
149 results are unpredictable."""
151 def getColumnNumber(self
):
152 "Return the column number where the current event ends."
155 def getLineNumber(self
):
156 "Return the line number where the current event ends."
159 def getPublicId(self
):
160 "Return the public identifier for the current event."
163 def getSystemId(self
):
164 "Return the system identifier for the current event."
168 class AttributesImpl
:
169 def __init__(self
, attrs
, rawnames
):
171 self
._rawnames
= rawnames
174 return len(self
._attrs
)
176 def getType(self
, name
):
179 def getValue(self
, name
):
180 return self
._attrs
[name
]
182 def getValueByQName(self
, name
):
183 return self
._attrs
[self
._rawnames
[name
]]
185 def getNameByQName(self
, name
):
186 return self
._rawnames
[name
]
189 return self
._attrs
.keys()
192 return self
._rawnames
.keys()
195 return len(self
._attrs
)
197 def __getitem__(self
, name
):
198 return self
._attrs
[name
]
201 return self
._attrs
.keys()
203 def has_key(self
, name
):
204 return self
._attrs
.has_key(name
)
206 def get(self
, name
, alternative
=None):
207 return self
._attrs
.get(name
, alternative
)
210 return self
.__class
__(self
._attrs
, self
._rawnames
)
213 return self
._attrs
.items()
216 return self
._attrs
.values()
224 if __name__
=="__main__":