1 | n/a | """Facility to use the Expat parser to load a minidom instance |
---|
2 | n/a | from a string or file. |
---|
3 | n/a | |
---|
4 | n/a | This avoids all the overhead of SAX and pulldom to gain performance. |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | # Warning! |
---|
8 | n/a | # |
---|
9 | n/a | # This module is tightly bound to the implementation details of the |
---|
10 | n/a | # minidom DOM and can't be used with other DOM implementations. This |
---|
11 | n/a | # is due, in part, to a lack of appropriate methods in the DOM (there is |
---|
12 | n/a | # no way to create Entity and Notation nodes via the DOM Level 2 |
---|
13 | n/a | # interface), and for performance. The latter is the cause of some fairly |
---|
14 | n/a | # cryptic code. |
---|
15 | n/a | # |
---|
16 | n/a | # Performance hacks: |
---|
17 | n/a | # |
---|
18 | n/a | # - .character_data_handler() has an extra case in which continuing |
---|
19 | n/a | # data is appended to an existing Text node; this can be a |
---|
20 | n/a | # speedup since pyexpat can break up character data into multiple |
---|
21 | n/a | # callbacks even though we set the buffer_text attribute on the |
---|
22 | n/a | # parser. This also gives us the advantage that we don't need a |
---|
23 | n/a | # separate normalization pass. |
---|
24 | n/a | # |
---|
25 | n/a | # - Determining that a node exists is done using an identity comparison |
---|
26 | n/a | # with None rather than a truth test; this avoids searching for and |
---|
27 | n/a | # calling any methods on the node object if it exists. (A rather |
---|
28 | n/a | # nice speedup is achieved this way as well!) |
---|
29 | n/a | |
---|
30 | n/a | from xml.dom import xmlbuilder, minidom, Node |
---|
31 | n/a | from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE |
---|
32 | n/a | from xml.parsers import expat |
---|
33 | n/a | from xml.dom.minidom import _append_child, _set_attribute_node |
---|
34 | n/a | from xml.dom.NodeFilter import NodeFilter |
---|
35 | n/a | |
---|
36 | n/a | TEXT_NODE = Node.TEXT_NODE |
---|
37 | n/a | CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE |
---|
38 | n/a | DOCUMENT_NODE = Node.DOCUMENT_NODE |
---|
39 | n/a | |
---|
40 | n/a | FILTER_ACCEPT = xmlbuilder.DOMBuilderFilter.FILTER_ACCEPT |
---|
41 | n/a | FILTER_REJECT = xmlbuilder.DOMBuilderFilter.FILTER_REJECT |
---|
42 | n/a | FILTER_SKIP = xmlbuilder.DOMBuilderFilter.FILTER_SKIP |
---|
43 | n/a | FILTER_INTERRUPT = xmlbuilder.DOMBuilderFilter.FILTER_INTERRUPT |
---|
44 | n/a | |
---|
45 | n/a | theDOMImplementation = minidom.getDOMImplementation() |
---|
46 | n/a | |
---|
47 | n/a | # Expat typename -> TypeInfo |
---|
48 | n/a | _typeinfo_map = { |
---|
49 | n/a | "CDATA": minidom.TypeInfo(None, "cdata"), |
---|
50 | n/a | "ENUM": minidom.TypeInfo(None, "enumeration"), |
---|
51 | n/a | "ENTITY": minidom.TypeInfo(None, "entity"), |
---|
52 | n/a | "ENTITIES": minidom.TypeInfo(None, "entities"), |
---|
53 | n/a | "ID": minidom.TypeInfo(None, "id"), |
---|
54 | n/a | "IDREF": minidom.TypeInfo(None, "idref"), |
---|
55 | n/a | "IDREFS": minidom.TypeInfo(None, "idrefs"), |
---|
56 | n/a | "NMTOKEN": minidom.TypeInfo(None, "nmtoken"), |
---|
57 | n/a | "NMTOKENS": minidom.TypeInfo(None, "nmtokens"), |
---|
58 | n/a | } |
---|
59 | n/a | |
---|
60 | n/a | class ElementInfo(object): |
---|
61 | n/a | __slots__ = '_attr_info', '_model', 'tagName' |
---|
62 | n/a | |
---|
63 | n/a | def __init__(self, tagName, model=None): |
---|
64 | n/a | self.tagName = tagName |
---|
65 | n/a | self._attr_info = [] |
---|
66 | n/a | self._model = model |
---|
67 | n/a | |
---|
68 | n/a | def __getstate__(self): |
---|
69 | n/a | return self._attr_info, self._model, self.tagName |
---|
70 | n/a | |
---|
71 | n/a | def __setstate__(self, state): |
---|
72 | n/a | self._attr_info, self._model, self.tagName = state |
---|
73 | n/a | |
---|
74 | n/a | def getAttributeType(self, aname): |
---|
75 | n/a | for info in self._attr_info: |
---|
76 | n/a | if info[1] == aname: |
---|
77 | n/a | t = info[-2] |
---|
78 | n/a | if t[0] == "(": |
---|
79 | n/a | return _typeinfo_map["ENUM"] |
---|
80 | n/a | else: |
---|
81 | n/a | return _typeinfo_map[info[-2]] |
---|
82 | n/a | return minidom._no_type |
---|
83 | n/a | |
---|
84 | n/a | def getAttributeTypeNS(self, namespaceURI, localName): |
---|
85 | n/a | return minidom._no_type |
---|
86 | n/a | |
---|
87 | n/a | def isElementContent(self): |
---|
88 | n/a | if self._model: |
---|
89 | n/a | type = self._model[0] |
---|
90 | n/a | return type not in (expat.model.XML_CTYPE_ANY, |
---|
91 | n/a | expat.model.XML_CTYPE_MIXED) |
---|
92 | n/a | else: |
---|
93 | n/a | return False |
---|
94 | n/a | |
---|
95 | n/a | def isEmpty(self): |
---|
96 | n/a | if self._model: |
---|
97 | n/a | return self._model[0] == expat.model.XML_CTYPE_EMPTY |
---|
98 | n/a | else: |
---|
99 | n/a | return False |
---|
100 | n/a | |
---|
101 | n/a | def isId(self, aname): |
---|
102 | n/a | for info in self._attr_info: |
---|
103 | n/a | if info[1] == aname: |
---|
104 | n/a | return info[-2] == "ID" |
---|
105 | n/a | return False |
---|
106 | n/a | |
---|
107 | n/a | def isIdNS(self, euri, ename, auri, aname): |
---|
108 | n/a | # not sure this is meaningful |
---|
109 | n/a | return self.isId((auri, aname)) |
---|
110 | n/a | |
---|
111 | n/a | def _intern(builder, s): |
---|
112 | n/a | return builder._intern_setdefault(s, s) |
---|
113 | n/a | |
---|
114 | n/a | def _parse_ns_name(builder, name): |
---|
115 | n/a | assert ' ' in name |
---|
116 | n/a | parts = name.split(' ') |
---|
117 | n/a | intern = builder._intern_setdefault |
---|
118 | n/a | if len(parts) == 3: |
---|
119 | n/a | uri, localname, prefix = parts |
---|
120 | n/a | prefix = intern(prefix, prefix) |
---|
121 | n/a | qname = "%s:%s" % (prefix, localname) |
---|
122 | n/a | qname = intern(qname, qname) |
---|
123 | n/a | localname = intern(localname, localname) |
---|
124 | n/a | elif len(parts) == 2: |
---|
125 | n/a | uri, localname = parts |
---|
126 | n/a | prefix = EMPTY_PREFIX |
---|
127 | n/a | qname = localname = intern(localname, localname) |
---|
128 | n/a | else: |
---|
129 | n/a | raise ValueError("Unsupported syntax: spaces in URIs not supported: %r" % name) |
---|
130 | n/a | return intern(uri, uri), localname, prefix, qname |
---|
131 | n/a | |
---|
132 | n/a | |
---|
133 | n/a | class ExpatBuilder: |
---|
134 | n/a | """Document builder that uses Expat to build a ParsedXML.DOM document |
---|
135 | n/a | instance.""" |
---|
136 | n/a | |
---|
137 | n/a | def __init__(self, options=None): |
---|
138 | n/a | if options is None: |
---|
139 | n/a | options = xmlbuilder.Options() |
---|
140 | n/a | self._options = options |
---|
141 | n/a | if self._options.filter is not None: |
---|
142 | n/a | self._filter = FilterVisibilityController(self._options.filter) |
---|
143 | n/a | else: |
---|
144 | n/a | self._filter = None |
---|
145 | n/a | # This *really* doesn't do anything in this case, so |
---|
146 | n/a | # override it with something fast & minimal. |
---|
147 | n/a | self._finish_start_element = id |
---|
148 | n/a | self._parser = None |
---|
149 | n/a | self.reset() |
---|
150 | n/a | |
---|
151 | n/a | def createParser(self): |
---|
152 | n/a | """Create a new parser object.""" |
---|
153 | n/a | return expat.ParserCreate() |
---|
154 | n/a | |
---|
155 | n/a | def getParser(self): |
---|
156 | n/a | """Return the parser object, creating a new one if needed.""" |
---|
157 | n/a | if not self._parser: |
---|
158 | n/a | self._parser = self.createParser() |
---|
159 | n/a | self._intern_setdefault = self._parser.intern.setdefault |
---|
160 | n/a | self._parser.buffer_text = True |
---|
161 | n/a | self._parser.ordered_attributes = True |
---|
162 | n/a | self._parser.specified_attributes = True |
---|
163 | n/a | self.install(self._parser) |
---|
164 | n/a | return self._parser |
---|
165 | n/a | |
---|
166 | n/a | def reset(self): |
---|
167 | n/a | """Free all data structures used during DOM construction.""" |
---|
168 | n/a | self.document = theDOMImplementation.createDocument( |
---|
169 | n/a | EMPTY_NAMESPACE, None, None) |
---|
170 | n/a | self.curNode = self.document |
---|
171 | n/a | self._elem_info = self.document._elem_info |
---|
172 | n/a | self._cdata = False |
---|
173 | n/a | |
---|
174 | n/a | def install(self, parser): |
---|
175 | n/a | """Install the callbacks needed to build the DOM into the parser.""" |
---|
176 | n/a | # This creates circular references! |
---|
177 | n/a | parser.StartDoctypeDeclHandler = self.start_doctype_decl_handler |
---|
178 | n/a | parser.StartElementHandler = self.first_element_handler |
---|
179 | n/a | parser.EndElementHandler = self.end_element_handler |
---|
180 | n/a | parser.ProcessingInstructionHandler = self.pi_handler |
---|
181 | n/a | if self._options.entities: |
---|
182 | n/a | parser.EntityDeclHandler = self.entity_decl_handler |
---|
183 | n/a | parser.NotationDeclHandler = self.notation_decl_handler |
---|
184 | n/a | if self._options.comments: |
---|
185 | n/a | parser.CommentHandler = self.comment_handler |
---|
186 | n/a | if self._options.cdata_sections: |
---|
187 | n/a | parser.StartCdataSectionHandler = self.start_cdata_section_handler |
---|
188 | n/a | parser.EndCdataSectionHandler = self.end_cdata_section_handler |
---|
189 | n/a | parser.CharacterDataHandler = self.character_data_handler_cdata |
---|
190 | n/a | else: |
---|
191 | n/a | parser.CharacterDataHandler = self.character_data_handler |
---|
192 | n/a | parser.ExternalEntityRefHandler = self.external_entity_ref_handler |
---|
193 | n/a | parser.XmlDeclHandler = self.xml_decl_handler |
---|
194 | n/a | parser.ElementDeclHandler = self.element_decl_handler |
---|
195 | n/a | parser.AttlistDeclHandler = self.attlist_decl_handler |
---|
196 | n/a | |
---|
197 | n/a | def parseFile(self, file): |
---|
198 | n/a | """Parse a document from a file object, returning the document |
---|
199 | n/a | node.""" |
---|
200 | n/a | parser = self.getParser() |
---|
201 | n/a | first_buffer = True |
---|
202 | n/a | try: |
---|
203 | n/a | while 1: |
---|
204 | n/a | buffer = file.read(16*1024) |
---|
205 | n/a | if not buffer: |
---|
206 | n/a | break |
---|
207 | n/a | parser.Parse(buffer, 0) |
---|
208 | n/a | if first_buffer and self.document.documentElement: |
---|
209 | n/a | self._setup_subset(buffer) |
---|
210 | n/a | first_buffer = False |
---|
211 | n/a | parser.Parse("", True) |
---|
212 | n/a | except ParseEscape: |
---|
213 | n/a | pass |
---|
214 | n/a | doc = self.document |
---|
215 | n/a | self.reset() |
---|
216 | n/a | self._parser = None |
---|
217 | n/a | return doc |
---|
218 | n/a | |
---|
219 | n/a | def parseString(self, string): |
---|
220 | n/a | """Parse a document from a string, returning the document node.""" |
---|
221 | n/a | parser = self.getParser() |
---|
222 | n/a | try: |
---|
223 | n/a | parser.Parse(string, True) |
---|
224 | n/a | self._setup_subset(string) |
---|
225 | n/a | except ParseEscape: |
---|
226 | n/a | pass |
---|
227 | n/a | doc = self.document |
---|
228 | n/a | self.reset() |
---|
229 | n/a | self._parser = None |
---|
230 | n/a | return doc |
---|
231 | n/a | |
---|
232 | n/a | def _setup_subset(self, buffer): |
---|
233 | n/a | """Load the internal subset if there might be one.""" |
---|
234 | n/a | if self.document.doctype: |
---|
235 | n/a | extractor = InternalSubsetExtractor() |
---|
236 | n/a | extractor.parseString(buffer) |
---|
237 | n/a | subset = extractor.getSubset() |
---|
238 | n/a | self.document.doctype.internalSubset = subset |
---|
239 | n/a | |
---|
240 | n/a | def start_doctype_decl_handler(self, doctypeName, systemId, publicId, |
---|
241 | n/a | has_internal_subset): |
---|
242 | n/a | doctype = self.document.implementation.createDocumentType( |
---|
243 | n/a | doctypeName, publicId, systemId) |
---|
244 | n/a | doctype.ownerDocument = self.document |
---|
245 | n/a | _append_child(self.document, doctype) |
---|
246 | n/a | self.document.doctype = doctype |
---|
247 | n/a | if self._filter and self._filter.acceptNode(doctype) == FILTER_REJECT: |
---|
248 | n/a | self.document.doctype = None |
---|
249 | n/a | del self.document.childNodes[-1] |
---|
250 | n/a | doctype = None |
---|
251 | n/a | self._parser.EntityDeclHandler = None |
---|
252 | n/a | self._parser.NotationDeclHandler = None |
---|
253 | n/a | if has_internal_subset: |
---|
254 | n/a | if doctype is not None: |
---|
255 | n/a | doctype.entities._seq = [] |
---|
256 | n/a | doctype.notations._seq = [] |
---|
257 | n/a | self._parser.CommentHandler = None |
---|
258 | n/a | self._parser.ProcessingInstructionHandler = None |
---|
259 | n/a | self._parser.EndDoctypeDeclHandler = self.end_doctype_decl_handler |
---|
260 | n/a | |
---|
261 | n/a | def end_doctype_decl_handler(self): |
---|
262 | n/a | if self._options.comments: |
---|
263 | n/a | self._parser.CommentHandler = self.comment_handler |
---|
264 | n/a | self._parser.ProcessingInstructionHandler = self.pi_handler |
---|
265 | n/a | if not (self._elem_info or self._filter): |
---|
266 | n/a | self._finish_end_element = id |
---|
267 | n/a | |
---|
268 | n/a | def pi_handler(self, target, data): |
---|
269 | n/a | node = self.document.createProcessingInstruction(target, data) |
---|
270 | n/a | _append_child(self.curNode, node) |
---|
271 | n/a | if self._filter and self._filter.acceptNode(node) == FILTER_REJECT: |
---|
272 | n/a | self.curNode.removeChild(node) |
---|
273 | n/a | |
---|
274 | n/a | def character_data_handler_cdata(self, data): |
---|
275 | n/a | childNodes = self.curNode.childNodes |
---|
276 | n/a | if self._cdata: |
---|
277 | n/a | if ( self._cdata_continue |
---|
278 | n/a | and childNodes[-1].nodeType == CDATA_SECTION_NODE): |
---|
279 | n/a | childNodes[-1].appendData(data) |
---|
280 | n/a | return |
---|
281 | n/a | node = self.document.createCDATASection(data) |
---|
282 | n/a | self._cdata_continue = True |
---|
283 | n/a | elif childNodes and childNodes[-1].nodeType == TEXT_NODE: |
---|
284 | n/a | node = childNodes[-1] |
---|
285 | n/a | value = node.data + data |
---|
286 | n/a | node.data = value |
---|
287 | n/a | return |
---|
288 | n/a | else: |
---|
289 | n/a | node = minidom.Text() |
---|
290 | n/a | node.data = data |
---|
291 | n/a | node.ownerDocument = self.document |
---|
292 | n/a | _append_child(self.curNode, node) |
---|
293 | n/a | |
---|
294 | n/a | def character_data_handler(self, data): |
---|
295 | n/a | childNodes = self.curNode.childNodes |
---|
296 | n/a | if childNodes and childNodes[-1].nodeType == TEXT_NODE: |
---|
297 | n/a | node = childNodes[-1] |
---|
298 | n/a | node.data = node.data + data |
---|
299 | n/a | return |
---|
300 | n/a | node = minidom.Text() |
---|
301 | n/a | node.data = node.data + data |
---|
302 | n/a | node.ownerDocument = self.document |
---|
303 | n/a | _append_child(self.curNode, node) |
---|
304 | n/a | |
---|
305 | n/a | def entity_decl_handler(self, entityName, is_parameter_entity, value, |
---|
306 | n/a | base, systemId, publicId, notationName): |
---|
307 | n/a | if is_parameter_entity: |
---|
308 | n/a | # we don't care about parameter entities for the DOM |
---|
309 | n/a | return |
---|
310 | n/a | if not self._options.entities: |
---|
311 | n/a | return |
---|
312 | n/a | node = self.document._create_entity(entityName, publicId, |
---|
313 | n/a | systemId, notationName) |
---|
314 | n/a | if value is not None: |
---|
315 | n/a | # internal entity |
---|
316 | n/a | # node *should* be readonly, but we'll cheat |
---|
317 | n/a | child = self.document.createTextNode(value) |
---|
318 | n/a | node.childNodes.append(child) |
---|
319 | n/a | self.document.doctype.entities._seq.append(node) |
---|
320 | n/a | if self._filter and self._filter.acceptNode(node) == FILTER_REJECT: |
---|
321 | n/a | del self.document.doctype.entities._seq[-1] |
---|
322 | n/a | |
---|
323 | n/a | def notation_decl_handler(self, notationName, base, systemId, publicId): |
---|
324 | n/a | node = self.document._create_notation(notationName, publicId, systemId) |
---|
325 | n/a | self.document.doctype.notations._seq.append(node) |
---|
326 | n/a | if self._filter and self._filter.acceptNode(node) == FILTER_ACCEPT: |
---|
327 | n/a | del self.document.doctype.notations._seq[-1] |
---|
328 | n/a | |
---|
329 | n/a | def comment_handler(self, data): |
---|
330 | n/a | node = self.document.createComment(data) |
---|
331 | n/a | _append_child(self.curNode, node) |
---|
332 | n/a | if self._filter and self._filter.acceptNode(node) == FILTER_REJECT: |
---|
333 | n/a | self.curNode.removeChild(node) |
---|
334 | n/a | |
---|
335 | n/a | def start_cdata_section_handler(self): |
---|
336 | n/a | self._cdata = True |
---|
337 | n/a | self._cdata_continue = False |
---|
338 | n/a | |
---|
339 | n/a | def end_cdata_section_handler(self): |
---|
340 | n/a | self._cdata = False |
---|
341 | n/a | self._cdata_continue = False |
---|
342 | n/a | |
---|
343 | n/a | def external_entity_ref_handler(self, context, base, systemId, publicId): |
---|
344 | n/a | return 1 |
---|
345 | n/a | |
---|
346 | n/a | def first_element_handler(self, name, attributes): |
---|
347 | n/a | if self._filter is None and not self._elem_info: |
---|
348 | n/a | self._finish_end_element = id |
---|
349 | n/a | self.getParser().StartElementHandler = self.start_element_handler |
---|
350 | n/a | self.start_element_handler(name, attributes) |
---|
351 | n/a | |
---|
352 | n/a | def start_element_handler(self, name, attributes): |
---|
353 | n/a | node = self.document.createElement(name) |
---|
354 | n/a | _append_child(self.curNode, node) |
---|
355 | n/a | self.curNode = node |
---|
356 | n/a | |
---|
357 | n/a | if attributes: |
---|
358 | n/a | for i in range(0, len(attributes), 2): |
---|
359 | n/a | a = minidom.Attr(attributes[i], EMPTY_NAMESPACE, |
---|
360 | n/a | None, EMPTY_PREFIX) |
---|
361 | n/a | value = attributes[i+1] |
---|
362 | n/a | a.value = value |
---|
363 | n/a | a.ownerDocument = self.document |
---|
364 | n/a | _set_attribute_node(node, a) |
---|
365 | n/a | |
---|
366 | n/a | if node is not self.document.documentElement: |
---|
367 | n/a | self._finish_start_element(node) |
---|
368 | n/a | |
---|
369 | n/a | def _finish_start_element(self, node): |
---|
370 | n/a | if self._filter: |
---|
371 | n/a | # To be general, we'd have to call isSameNode(), but this |
---|
372 | n/a | # is sufficient for minidom: |
---|
373 | n/a | if node is self.document.documentElement: |
---|
374 | n/a | return |
---|
375 | n/a | filt = self._filter.startContainer(node) |
---|
376 | n/a | if filt == FILTER_REJECT: |
---|
377 | n/a | # ignore this node & all descendents |
---|
378 | n/a | Rejecter(self) |
---|
379 | n/a | elif filt == FILTER_SKIP: |
---|
380 | n/a | # ignore this node, but make it's children become |
---|
381 | n/a | # children of the parent node |
---|
382 | n/a | Skipper(self) |
---|
383 | n/a | else: |
---|
384 | n/a | return |
---|
385 | n/a | self.curNode = node.parentNode |
---|
386 | n/a | node.parentNode.removeChild(node) |
---|
387 | n/a | node.unlink() |
---|
388 | n/a | |
---|
389 | n/a | # If this ever changes, Namespaces.end_element_handler() needs to |
---|
390 | n/a | # be changed to match. |
---|
391 | n/a | # |
---|
392 | n/a | def end_element_handler(self, name): |
---|
393 | n/a | curNode = self.curNode |
---|
394 | n/a | self.curNode = curNode.parentNode |
---|
395 | n/a | self._finish_end_element(curNode) |
---|
396 | n/a | |
---|
397 | n/a | def _finish_end_element(self, curNode): |
---|
398 | n/a | info = self._elem_info.get(curNode.tagName) |
---|
399 | n/a | if info: |
---|
400 | n/a | self._handle_white_text_nodes(curNode, info) |
---|
401 | n/a | if self._filter: |
---|
402 | n/a | if curNode is self.document.documentElement: |
---|
403 | n/a | return |
---|
404 | n/a | if self._filter.acceptNode(curNode) == FILTER_REJECT: |
---|
405 | n/a | self.curNode.removeChild(curNode) |
---|
406 | n/a | curNode.unlink() |
---|
407 | n/a | |
---|
408 | n/a | def _handle_white_text_nodes(self, node, info): |
---|
409 | n/a | if (self._options.whitespace_in_element_content |
---|
410 | n/a | or not info.isElementContent()): |
---|
411 | n/a | return |
---|
412 | n/a | |
---|
413 | n/a | # We have element type information and should remove ignorable |
---|
414 | n/a | # whitespace; identify for text nodes which contain only |
---|
415 | n/a | # whitespace. |
---|
416 | n/a | L = [] |
---|
417 | n/a | for child in node.childNodes: |
---|
418 | n/a | if child.nodeType == TEXT_NODE and not child.data.strip(): |
---|
419 | n/a | L.append(child) |
---|
420 | n/a | |
---|
421 | n/a | # Remove ignorable whitespace from the tree. |
---|
422 | n/a | for child in L: |
---|
423 | n/a | node.removeChild(child) |
---|
424 | n/a | |
---|
425 | n/a | def element_decl_handler(self, name, model): |
---|
426 | n/a | info = self._elem_info.get(name) |
---|
427 | n/a | if info is None: |
---|
428 | n/a | self._elem_info[name] = ElementInfo(name, model) |
---|
429 | n/a | else: |
---|
430 | n/a | assert info._model is None |
---|
431 | n/a | info._model = model |
---|
432 | n/a | |
---|
433 | n/a | def attlist_decl_handler(self, elem, name, type, default, required): |
---|
434 | n/a | info = self._elem_info.get(elem) |
---|
435 | n/a | if info is None: |
---|
436 | n/a | info = ElementInfo(elem) |
---|
437 | n/a | self._elem_info[elem] = info |
---|
438 | n/a | info._attr_info.append( |
---|
439 | n/a | [None, name, None, None, default, 0, type, required]) |
---|
440 | n/a | |
---|
441 | n/a | def xml_decl_handler(self, version, encoding, standalone): |
---|
442 | n/a | self.document.version = version |
---|
443 | n/a | self.document.encoding = encoding |
---|
444 | n/a | # This is still a little ugly, thanks to the pyexpat API. ;-( |
---|
445 | n/a | if standalone >= 0: |
---|
446 | n/a | if standalone: |
---|
447 | n/a | self.document.standalone = True |
---|
448 | n/a | else: |
---|
449 | n/a | self.document.standalone = False |
---|
450 | n/a | |
---|
451 | n/a | |
---|
452 | n/a | # Don't include FILTER_INTERRUPT, since that's checked separately |
---|
453 | n/a | # where allowed. |
---|
454 | n/a | _ALLOWED_FILTER_RETURNS = (FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP) |
---|
455 | n/a | |
---|
456 | n/a | class FilterVisibilityController(object): |
---|
457 | n/a | """Wrapper around a DOMBuilderFilter which implements the checks |
---|
458 | n/a | to make the whatToShow filter attribute work.""" |
---|
459 | n/a | |
---|
460 | n/a | __slots__ = 'filter', |
---|
461 | n/a | |
---|
462 | n/a | def __init__(self, filter): |
---|
463 | n/a | self.filter = filter |
---|
464 | n/a | |
---|
465 | n/a | def startContainer(self, node): |
---|
466 | n/a | mask = self._nodetype_mask[node.nodeType] |
---|
467 | n/a | if self.filter.whatToShow & mask: |
---|
468 | n/a | val = self.filter.startContainer(node) |
---|
469 | n/a | if val == FILTER_INTERRUPT: |
---|
470 | n/a | raise ParseEscape |
---|
471 | n/a | if val not in _ALLOWED_FILTER_RETURNS: |
---|
472 | n/a | raise ValueError( |
---|
473 | n/a | "startContainer() returned illegal value: " + repr(val)) |
---|
474 | n/a | return val |
---|
475 | n/a | else: |
---|
476 | n/a | return FILTER_ACCEPT |
---|
477 | n/a | |
---|
478 | n/a | def acceptNode(self, node): |
---|
479 | n/a | mask = self._nodetype_mask[node.nodeType] |
---|
480 | n/a | if self.filter.whatToShow & mask: |
---|
481 | n/a | val = self.filter.acceptNode(node) |
---|
482 | n/a | if val == FILTER_INTERRUPT: |
---|
483 | n/a | raise ParseEscape |
---|
484 | n/a | if val == FILTER_SKIP: |
---|
485 | n/a | # move all child nodes to the parent, and remove this node |
---|
486 | n/a | parent = node.parentNode |
---|
487 | n/a | for child in node.childNodes[:]: |
---|
488 | n/a | parent.appendChild(child) |
---|
489 | n/a | # node is handled by the caller |
---|
490 | n/a | return FILTER_REJECT |
---|
491 | n/a | if val not in _ALLOWED_FILTER_RETURNS: |
---|
492 | n/a | raise ValueError( |
---|
493 | n/a | "acceptNode() returned illegal value: " + repr(val)) |
---|
494 | n/a | return val |
---|
495 | n/a | else: |
---|
496 | n/a | return FILTER_ACCEPT |
---|
497 | n/a | |
---|
498 | n/a | _nodetype_mask = { |
---|
499 | n/a | Node.ELEMENT_NODE: NodeFilter.SHOW_ELEMENT, |
---|
500 | n/a | Node.ATTRIBUTE_NODE: NodeFilter.SHOW_ATTRIBUTE, |
---|
501 | n/a | Node.TEXT_NODE: NodeFilter.SHOW_TEXT, |
---|
502 | n/a | Node.CDATA_SECTION_NODE: NodeFilter.SHOW_CDATA_SECTION, |
---|
503 | n/a | Node.ENTITY_REFERENCE_NODE: NodeFilter.SHOW_ENTITY_REFERENCE, |
---|
504 | n/a | Node.ENTITY_NODE: NodeFilter.SHOW_ENTITY, |
---|
505 | n/a | Node.PROCESSING_INSTRUCTION_NODE: NodeFilter.SHOW_PROCESSING_INSTRUCTION, |
---|
506 | n/a | Node.COMMENT_NODE: NodeFilter.SHOW_COMMENT, |
---|
507 | n/a | Node.DOCUMENT_NODE: NodeFilter.SHOW_DOCUMENT, |
---|
508 | n/a | Node.DOCUMENT_TYPE_NODE: NodeFilter.SHOW_DOCUMENT_TYPE, |
---|
509 | n/a | Node.DOCUMENT_FRAGMENT_NODE: NodeFilter.SHOW_DOCUMENT_FRAGMENT, |
---|
510 | n/a | Node.NOTATION_NODE: NodeFilter.SHOW_NOTATION, |
---|
511 | n/a | } |
---|
512 | n/a | |
---|
513 | n/a | |
---|
514 | n/a | class FilterCrutch(object): |
---|
515 | n/a | __slots__ = '_builder', '_level', '_old_start', '_old_end' |
---|
516 | n/a | |
---|
517 | n/a | def __init__(self, builder): |
---|
518 | n/a | self._level = 0 |
---|
519 | n/a | self._builder = builder |
---|
520 | n/a | parser = builder._parser |
---|
521 | n/a | self._old_start = parser.StartElementHandler |
---|
522 | n/a | self._old_end = parser.EndElementHandler |
---|
523 | n/a | parser.StartElementHandler = self.start_element_handler |
---|
524 | n/a | parser.EndElementHandler = self.end_element_handler |
---|
525 | n/a | |
---|
526 | n/a | class Rejecter(FilterCrutch): |
---|
527 | n/a | __slots__ = () |
---|
528 | n/a | |
---|
529 | n/a | def __init__(self, builder): |
---|
530 | n/a | FilterCrutch.__init__(self, builder) |
---|
531 | n/a | parser = builder._parser |
---|
532 | n/a | for name in ("ProcessingInstructionHandler", |
---|
533 | n/a | "CommentHandler", |
---|
534 | n/a | "CharacterDataHandler", |
---|
535 | n/a | "StartCdataSectionHandler", |
---|
536 | n/a | "EndCdataSectionHandler", |
---|
537 | n/a | "ExternalEntityRefHandler", |
---|
538 | n/a | ): |
---|
539 | n/a | setattr(parser, name, None) |
---|
540 | n/a | |
---|
541 | n/a | def start_element_handler(self, *args): |
---|
542 | n/a | self._level = self._level + 1 |
---|
543 | n/a | |
---|
544 | n/a | def end_element_handler(self, *args): |
---|
545 | n/a | if self._level == 0: |
---|
546 | n/a | # restore the old handlers |
---|
547 | n/a | parser = self._builder._parser |
---|
548 | n/a | self._builder.install(parser) |
---|
549 | n/a | parser.StartElementHandler = self._old_start |
---|
550 | n/a | parser.EndElementHandler = self._old_end |
---|
551 | n/a | else: |
---|
552 | n/a | self._level = self._level - 1 |
---|
553 | n/a | |
---|
554 | n/a | class Skipper(FilterCrutch): |
---|
555 | n/a | __slots__ = () |
---|
556 | n/a | |
---|
557 | n/a | def start_element_handler(self, *args): |
---|
558 | n/a | node = self._builder.curNode |
---|
559 | n/a | self._old_start(*args) |
---|
560 | n/a | if self._builder.curNode is not node: |
---|
561 | n/a | self._level = self._level + 1 |
---|
562 | n/a | |
---|
563 | n/a | def end_element_handler(self, *args): |
---|
564 | n/a | if self._level == 0: |
---|
565 | n/a | # We're popping back out of the node we're skipping, so we |
---|
566 | n/a | # shouldn't need to do anything but reset the handlers. |
---|
567 | n/a | self._builder._parser.StartElementHandler = self._old_start |
---|
568 | n/a | self._builder._parser.EndElementHandler = self._old_end |
---|
569 | n/a | self._builder = None |
---|
570 | n/a | else: |
---|
571 | n/a | self._level = self._level - 1 |
---|
572 | n/a | self._old_end(*args) |
---|
573 | n/a | |
---|
574 | n/a | |
---|
575 | n/a | # framework document used by the fragment builder. |
---|
576 | n/a | # Takes a string for the doctype, subset string, and namespace attrs string. |
---|
577 | n/a | |
---|
578 | n/a | _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID = \ |
---|
579 | n/a | "http://xml.python.org/entities/fragment-builder/internal" |
---|
580 | n/a | |
---|
581 | n/a | _FRAGMENT_BUILDER_TEMPLATE = ( |
---|
582 | n/a | '''\ |
---|
583 | n/a | <!DOCTYPE wrapper |
---|
584 | n/a | %%s [ |
---|
585 | n/a | <!ENTITY fragment-builder-internal |
---|
586 | n/a | SYSTEM "%s"> |
---|
587 | n/a | %%s |
---|
588 | n/a | ]> |
---|
589 | n/a | <wrapper %%s |
---|
590 | n/a | >&fragment-builder-internal;</wrapper>''' |
---|
591 | n/a | % _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID) |
---|
592 | n/a | |
---|
593 | n/a | |
---|
594 | n/a | class FragmentBuilder(ExpatBuilder): |
---|
595 | n/a | """Builder which constructs document fragments given XML source |
---|
596 | n/a | text and a context node. |
---|
597 | n/a | |
---|
598 | n/a | The context node is expected to provide information about the |
---|
599 | n/a | namespace declarations which are in scope at the start of the |
---|
600 | n/a | fragment. |
---|
601 | n/a | """ |
---|
602 | n/a | |
---|
603 | n/a | def __init__(self, context, options=None): |
---|
604 | n/a | if context.nodeType == DOCUMENT_NODE: |
---|
605 | n/a | self.originalDocument = context |
---|
606 | n/a | self.context = context |
---|
607 | n/a | else: |
---|
608 | n/a | self.originalDocument = context.ownerDocument |
---|
609 | n/a | self.context = context |
---|
610 | n/a | ExpatBuilder.__init__(self, options) |
---|
611 | n/a | |
---|
612 | n/a | def reset(self): |
---|
613 | n/a | ExpatBuilder.reset(self) |
---|
614 | n/a | self.fragment = None |
---|
615 | n/a | |
---|
616 | n/a | def parseFile(self, file): |
---|
617 | n/a | """Parse a document fragment from a file object, returning the |
---|
618 | n/a | fragment node.""" |
---|
619 | n/a | return self.parseString(file.read()) |
---|
620 | n/a | |
---|
621 | n/a | def parseString(self, string): |
---|
622 | n/a | """Parse a document fragment from a string, returning the |
---|
623 | n/a | fragment node.""" |
---|
624 | n/a | self._source = string |
---|
625 | n/a | parser = self.getParser() |
---|
626 | n/a | doctype = self.originalDocument.doctype |
---|
627 | n/a | ident = "" |
---|
628 | n/a | if doctype: |
---|
629 | n/a | subset = doctype.internalSubset or self._getDeclarations() |
---|
630 | n/a | if doctype.publicId: |
---|
631 | n/a | ident = ('PUBLIC "%s" "%s"' |
---|
632 | n/a | % (doctype.publicId, doctype.systemId)) |
---|
633 | n/a | elif doctype.systemId: |
---|
634 | n/a | ident = 'SYSTEM "%s"' % doctype.systemId |
---|
635 | n/a | else: |
---|
636 | n/a | subset = "" |
---|
637 | n/a | nsattrs = self._getNSattrs() # get ns decls from node's ancestors |
---|
638 | n/a | document = _FRAGMENT_BUILDER_TEMPLATE % (ident, subset, nsattrs) |
---|
639 | n/a | try: |
---|
640 | n/a | parser.Parse(document, 1) |
---|
641 | n/a | except: |
---|
642 | n/a | self.reset() |
---|
643 | n/a | raise |
---|
644 | n/a | fragment = self.fragment |
---|
645 | n/a | self.reset() |
---|
646 | n/a | ## self._parser = None |
---|
647 | n/a | return fragment |
---|
648 | n/a | |
---|
649 | n/a | def _getDeclarations(self): |
---|
650 | n/a | """Re-create the internal subset from the DocumentType node. |
---|
651 | n/a | |
---|
652 | n/a | This is only needed if we don't already have the |
---|
653 | n/a | internalSubset as a string. |
---|
654 | n/a | """ |
---|
655 | n/a | doctype = self.context.ownerDocument.doctype |
---|
656 | n/a | s = "" |
---|
657 | n/a | if doctype: |
---|
658 | n/a | for i in range(doctype.notations.length): |
---|
659 | n/a | notation = doctype.notations.item(i) |
---|
660 | n/a | if s: |
---|
661 | n/a | s = s + "\n " |
---|
662 | n/a | s = "%s<!NOTATION %s" % (s, notation.nodeName) |
---|
663 | n/a | if notation.publicId: |
---|
664 | n/a | s = '%s PUBLIC "%s"\n "%s">' \ |
---|
665 | n/a | % (s, notation.publicId, notation.systemId) |
---|
666 | n/a | else: |
---|
667 | n/a | s = '%s SYSTEM "%s">' % (s, notation.systemId) |
---|
668 | n/a | for i in range(doctype.entities.length): |
---|
669 | n/a | entity = doctype.entities.item(i) |
---|
670 | n/a | if s: |
---|
671 | n/a | s = s + "\n " |
---|
672 | n/a | s = "%s<!ENTITY %s" % (s, entity.nodeName) |
---|
673 | n/a | if entity.publicId: |
---|
674 | n/a | s = '%s PUBLIC "%s"\n "%s"' \ |
---|
675 | n/a | % (s, entity.publicId, entity.systemId) |
---|
676 | n/a | elif entity.systemId: |
---|
677 | n/a | s = '%s SYSTEM "%s"' % (s, entity.systemId) |
---|
678 | n/a | else: |
---|
679 | n/a | s = '%s "%s"' % (s, entity.firstChild.data) |
---|
680 | n/a | if entity.notationName: |
---|
681 | n/a | s = "%s NOTATION %s" % (s, entity.notationName) |
---|
682 | n/a | s = s + ">" |
---|
683 | n/a | return s |
---|
684 | n/a | |
---|
685 | n/a | def _getNSattrs(self): |
---|
686 | n/a | return "" |
---|
687 | n/a | |
---|
688 | n/a | def external_entity_ref_handler(self, context, base, systemId, publicId): |
---|
689 | n/a | if systemId == _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID: |
---|
690 | n/a | # this entref is the one that we made to put the subtree |
---|
691 | n/a | # in; all of our given input is parsed in here. |
---|
692 | n/a | old_document = self.document |
---|
693 | n/a | old_cur_node = self.curNode |
---|
694 | n/a | parser = self._parser.ExternalEntityParserCreate(context) |
---|
695 | n/a | # put the real document back, parse into the fragment to return |
---|
696 | n/a | self.document = self.originalDocument |
---|
697 | n/a | self.fragment = self.document.createDocumentFragment() |
---|
698 | n/a | self.curNode = self.fragment |
---|
699 | n/a | try: |
---|
700 | n/a | parser.Parse(self._source, 1) |
---|
701 | n/a | finally: |
---|
702 | n/a | self.curNode = old_cur_node |
---|
703 | n/a | self.document = old_document |
---|
704 | n/a | self._source = None |
---|
705 | n/a | return -1 |
---|
706 | n/a | else: |
---|
707 | n/a | return ExpatBuilder.external_entity_ref_handler( |
---|
708 | n/a | self, context, base, systemId, publicId) |
---|
709 | n/a | |
---|
710 | n/a | |
---|
711 | n/a | class Namespaces: |
---|
712 | n/a | """Mix-in class for builders; adds support for namespaces.""" |
---|
713 | n/a | |
---|
714 | n/a | def _initNamespaces(self): |
---|
715 | n/a | # list of (prefix, uri) ns declarations. Namespace attrs are |
---|
716 | n/a | # constructed from this and added to the element's attrs. |
---|
717 | n/a | self._ns_ordered_prefixes = [] |
---|
718 | n/a | |
---|
719 | n/a | def createParser(self): |
---|
720 | n/a | """Create a new namespace-handling parser.""" |
---|
721 | n/a | parser = expat.ParserCreate(namespace_separator=" ") |
---|
722 | n/a | parser.namespace_prefixes = True |
---|
723 | n/a | return parser |
---|
724 | n/a | |
---|
725 | n/a | def install(self, parser): |
---|
726 | n/a | """Insert the namespace-handlers onto the parser.""" |
---|
727 | n/a | ExpatBuilder.install(self, parser) |
---|
728 | n/a | if self._options.namespace_declarations: |
---|
729 | n/a | parser.StartNamespaceDeclHandler = ( |
---|
730 | n/a | self.start_namespace_decl_handler) |
---|
731 | n/a | |
---|
732 | n/a | def start_namespace_decl_handler(self, prefix, uri): |
---|
733 | n/a | """Push this namespace declaration on our storage.""" |
---|
734 | n/a | self._ns_ordered_prefixes.append((prefix, uri)) |
---|
735 | n/a | |
---|
736 | n/a | def start_element_handler(self, name, attributes): |
---|
737 | n/a | if ' ' in name: |
---|
738 | n/a | uri, localname, prefix, qname = _parse_ns_name(self, name) |
---|
739 | n/a | else: |
---|
740 | n/a | uri = EMPTY_NAMESPACE |
---|
741 | n/a | qname = name |
---|
742 | n/a | localname = None |
---|
743 | n/a | prefix = EMPTY_PREFIX |
---|
744 | n/a | node = minidom.Element(qname, uri, prefix, localname) |
---|
745 | n/a | node.ownerDocument = self.document |
---|
746 | n/a | _append_child(self.curNode, node) |
---|
747 | n/a | self.curNode = node |
---|
748 | n/a | |
---|
749 | n/a | if self._ns_ordered_prefixes: |
---|
750 | n/a | for prefix, uri in self._ns_ordered_prefixes: |
---|
751 | n/a | if prefix: |
---|
752 | n/a | a = minidom.Attr(_intern(self, 'xmlns:' + prefix), |
---|
753 | n/a | XMLNS_NAMESPACE, prefix, "xmlns") |
---|
754 | n/a | else: |
---|
755 | n/a | a = minidom.Attr("xmlns", XMLNS_NAMESPACE, |
---|
756 | n/a | "xmlns", EMPTY_PREFIX) |
---|
757 | n/a | a.value = uri |
---|
758 | n/a | a.ownerDocument = self.document |
---|
759 | n/a | _set_attribute_node(node, a) |
---|
760 | n/a | del self._ns_ordered_prefixes[:] |
---|
761 | n/a | |
---|
762 | n/a | if attributes: |
---|
763 | n/a | node._ensure_attributes() |
---|
764 | n/a | _attrs = node._attrs |
---|
765 | n/a | _attrsNS = node._attrsNS |
---|
766 | n/a | for i in range(0, len(attributes), 2): |
---|
767 | n/a | aname = attributes[i] |
---|
768 | n/a | value = attributes[i+1] |
---|
769 | n/a | if ' ' in aname: |
---|
770 | n/a | uri, localname, prefix, qname = _parse_ns_name(self, aname) |
---|
771 | n/a | a = minidom.Attr(qname, uri, localname, prefix) |
---|
772 | n/a | _attrs[qname] = a |
---|
773 | n/a | _attrsNS[(uri, localname)] = a |
---|
774 | n/a | else: |
---|
775 | n/a | a = minidom.Attr(aname, EMPTY_NAMESPACE, |
---|
776 | n/a | aname, EMPTY_PREFIX) |
---|
777 | n/a | _attrs[aname] = a |
---|
778 | n/a | _attrsNS[(EMPTY_NAMESPACE, aname)] = a |
---|
779 | n/a | a.ownerDocument = self.document |
---|
780 | n/a | a.value = value |
---|
781 | n/a | a.ownerElement = node |
---|
782 | n/a | |
---|
783 | n/a | if __debug__: |
---|
784 | n/a | # This only adds some asserts to the original |
---|
785 | n/a | # end_element_handler(), so we only define this when -O is not |
---|
786 | n/a | # used. If changing one, be sure to check the other to see if |
---|
787 | n/a | # it needs to be changed as well. |
---|
788 | n/a | # |
---|
789 | n/a | def end_element_handler(self, name): |
---|
790 | n/a | curNode = self.curNode |
---|
791 | n/a | if ' ' in name: |
---|
792 | n/a | uri, localname, prefix, qname = _parse_ns_name(self, name) |
---|
793 | n/a | assert (curNode.namespaceURI == uri |
---|
794 | n/a | and curNode.localName == localname |
---|
795 | n/a | and curNode.prefix == prefix), \ |
---|
796 | n/a | "element stack messed up! (namespace)" |
---|
797 | n/a | else: |
---|
798 | n/a | assert curNode.nodeName == name, \ |
---|
799 | n/a | "element stack messed up - bad nodeName" |
---|
800 | n/a | assert curNode.namespaceURI == EMPTY_NAMESPACE, \ |
---|
801 | n/a | "element stack messed up - bad namespaceURI" |
---|
802 | n/a | self.curNode = curNode.parentNode |
---|
803 | n/a | self._finish_end_element(curNode) |
---|
804 | n/a | |
---|
805 | n/a | |
---|
806 | n/a | class ExpatBuilderNS(Namespaces, ExpatBuilder): |
---|
807 | n/a | """Document builder that supports namespaces.""" |
---|
808 | n/a | |
---|
809 | n/a | def reset(self): |
---|
810 | n/a | ExpatBuilder.reset(self) |
---|
811 | n/a | self._initNamespaces() |
---|
812 | n/a | |
---|
813 | n/a | |
---|
814 | n/a | class FragmentBuilderNS(Namespaces, FragmentBuilder): |
---|
815 | n/a | """Fragment builder that supports namespaces.""" |
---|
816 | n/a | |
---|
817 | n/a | def reset(self): |
---|
818 | n/a | FragmentBuilder.reset(self) |
---|
819 | n/a | self._initNamespaces() |
---|
820 | n/a | |
---|
821 | n/a | def _getNSattrs(self): |
---|
822 | n/a | """Return string of namespace attributes from this element and |
---|
823 | n/a | ancestors.""" |
---|
824 | n/a | # XXX This needs to be re-written to walk the ancestors of the |
---|
825 | n/a | # context to build up the namespace information from |
---|
826 | n/a | # declarations, elements, and attributes found in context. |
---|
827 | n/a | # Otherwise we have to store a bunch more data on the DOM |
---|
828 | n/a | # (though that *might* be more reliable -- not clear). |
---|
829 | n/a | attrs = "" |
---|
830 | n/a | context = self.context |
---|
831 | n/a | L = [] |
---|
832 | n/a | while context: |
---|
833 | n/a | if hasattr(context, '_ns_prefix_uri'): |
---|
834 | n/a | for prefix, uri in context._ns_prefix_uri.items(): |
---|
835 | n/a | # add every new NS decl from context to L and attrs string |
---|
836 | n/a | if prefix in L: |
---|
837 | n/a | continue |
---|
838 | n/a | L.append(prefix) |
---|
839 | n/a | if prefix: |
---|
840 | n/a | declname = "xmlns:" + prefix |
---|
841 | n/a | else: |
---|
842 | n/a | declname = "xmlns" |
---|
843 | n/a | if attrs: |
---|
844 | n/a | attrs = "%s\n %s='%s'" % (attrs, declname, uri) |
---|
845 | n/a | else: |
---|
846 | n/a | attrs = " %s='%s'" % (declname, uri) |
---|
847 | n/a | context = context.parentNode |
---|
848 | n/a | return attrs |
---|
849 | n/a | |
---|
850 | n/a | |
---|
851 | n/a | class ParseEscape(Exception): |
---|
852 | n/a | """Exception raised to short-circuit parsing in InternalSubsetExtractor.""" |
---|
853 | n/a | pass |
---|
854 | n/a | |
---|
855 | n/a | class InternalSubsetExtractor(ExpatBuilder): |
---|
856 | n/a | """XML processor which can rip out the internal document type subset.""" |
---|
857 | n/a | |
---|
858 | n/a | subset = None |
---|
859 | n/a | |
---|
860 | n/a | def getSubset(self): |
---|
861 | n/a | """Return the internal subset as a string.""" |
---|
862 | n/a | return self.subset |
---|
863 | n/a | |
---|
864 | n/a | def parseFile(self, file): |
---|
865 | n/a | try: |
---|
866 | n/a | ExpatBuilder.parseFile(self, file) |
---|
867 | n/a | except ParseEscape: |
---|
868 | n/a | pass |
---|
869 | n/a | |
---|
870 | n/a | def parseString(self, string): |
---|
871 | n/a | try: |
---|
872 | n/a | ExpatBuilder.parseString(self, string) |
---|
873 | n/a | except ParseEscape: |
---|
874 | n/a | pass |
---|
875 | n/a | |
---|
876 | n/a | def install(self, parser): |
---|
877 | n/a | parser.StartDoctypeDeclHandler = self.start_doctype_decl_handler |
---|
878 | n/a | parser.StartElementHandler = self.start_element_handler |
---|
879 | n/a | |
---|
880 | n/a | def start_doctype_decl_handler(self, name, publicId, systemId, |
---|
881 | n/a | has_internal_subset): |
---|
882 | n/a | if has_internal_subset: |
---|
883 | n/a | parser = self.getParser() |
---|
884 | n/a | self.subset = [] |
---|
885 | n/a | parser.DefaultHandler = self.subset.append |
---|
886 | n/a | parser.EndDoctypeDeclHandler = self.end_doctype_decl_handler |
---|
887 | n/a | else: |
---|
888 | n/a | raise ParseEscape() |
---|
889 | n/a | |
---|
890 | n/a | def end_doctype_decl_handler(self): |
---|
891 | n/a | s = ''.join(self.subset).replace('\r\n', '\n').replace('\r', '\n') |
---|
892 | n/a | self.subset = s |
---|
893 | n/a | raise ParseEscape() |
---|
894 | n/a | |
---|
895 | n/a | def start_element_handler(self, name, attrs): |
---|
896 | n/a | raise ParseEscape() |
---|
897 | n/a | |
---|
898 | n/a | |
---|
899 | n/a | def parse(file, namespaces=True): |
---|
900 | n/a | """Parse a document, returning the resulting Document node. |
---|
901 | n/a | |
---|
902 | n/a | 'file' may be either a file name or an open file object. |
---|
903 | n/a | """ |
---|
904 | n/a | if namespaces: |
---|
905 | n/a | builder = ExpatBuilderNS() |
---|
906 | n/a | else: |
---|
907 | n/a | builder = ExpatBuilder() |
---|
908 | n/a | |
---|
909 | n/a | if isinstance(file, str): |
---|
910 | n/a | with open(file, 'rb') as fp: |
---|
911 | n/a | result = builder.parseFile(fp) |
---|
912 | n/a | else: |
---|
913 | n/a | result = builder.parseFile(file) |
---|
914 | n/a | return result |
---|
915 | n/a | |
---|
916 | n/a | |
---|
917 | n/a | def parseString(string, namespaces=True): |
---|
918 | n/a | """Parse a document from a string, returning the resulting |
---|
919 | n/a | Document node. |
---|
920 | n/a | """ |
---|
921 | n/a | if namespaces: |
---|
922 | n/a | builder = ExpatBuilderNS() |
---|
923 | n/a | else: |
---|
924 | n/a | builder = ExpatBuilder() |
---|
925 | n/a | return builder.parseString(string) |
---|
926 | n/a | |
---|
927 | n/a | |
---|
928 | n/a | def parseFragment(file, context, namespaces=True): |
---|
929 | n/a | """Parse a fragment of a document, given the context from which it |
---|
930 | n/a | was originally extracted. context should be the parent of the |
---|
931 | n/a | node(s) which are in the fragment. |
---|
932 | n/a | |
---|
933 | n/a | 'file' may be either a file name or an open file object. |
---|
934 | n/a | """ |
---|
935 | n/a | if namespaces: |
---|
936 | n/a | builder = FragmentBuilderNS(context) |
---|
937 | n/a | else: |
---|
938 | n/a | builder = FragmentBuilder(context) |
---|
939 | n/a | |
---|
940 | n/a | if isinstance(file, str): |
---|
941 | n/a | with open(file, 'rb') as fp: |
---|
942 | n/a | result = builder.parseFile(fp) |
---|
943 | n/a | else: |
---|
944 | n/a | result = builder.parseFile(file) |
---|
945 | n/a | return result |
---|
946 | n/a | |
---|
947 | n/a | |
---|
948 | n/a | def parseFragmentString(string, context, namespaces=True): |
---|
949 | n/a | """Parse a fragment of a document from a string, given the context |
---|
950 | n/a | from which it was originally extracted. context should be the |
---|
951 | n/a | parent of the node(s) which are in the fragment. |
---|
952 | n/a | """ |
---|
953 | n/a | if namespaces: |
---|
954 | n/a | builder = FragmentBuilderNS(context) |
---|
955 | n/a | else: |
---|
956 | n/a | builder = FragmentBuilder(context) |
---|
957 | n/a | return builder.parseString(string) |
---|
958 | n/a | |
---|
959 | n/a | |
---|
960 | n/a | def makeBuilder(options): |
---|
961 | n/a | """Create a builder based on an Options object.""" |
---|
962 | n/a | if options.namespaces: |
---|
963 | n/a | return ExpatBuilderNS(options) |
---|
964 | n/a | else: |
---|
965 | n/a | return ExpatBuilder(options) |
---|