X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=doc%2Fhtml%2Fapi%2Flxml.sax-pysrc.html;h=819a0d2c4a02d88e78a08505fa7f9918e2114386;hb=a3ead4cf65f2b8ab03dd9d2fe7d6d179d209db7e;hp=72a5a196e1009b87d91cb01f3de6bd66c966b8c4;hpb=aac406df7ff3b0eff020f949b7d81c27b8006932;p=platform%2Fupstream%2Fpython-lxml.git diff --git a/doc/html/api/lxml.sax-pysrc.html b/doc/html/api/lxml.sax-pysrc.html index 72a5a19..819a0d2 100644 --- a/doc/html/api/lxml.sax-pysrc.html +++ b/doc/html/api/lxml.sax-pysrc.html @@ -61,76 +61,90 @@
- 1 """ - 2 SAX-based adapter to copy trees from/to the Python standard library. - 3 - 4 Use the `ElementTreeContentHandler` class to build an ElementTree from - 5 SAX events. - 6 - 7 Use the `ElementTreeProducer` class or the `saxify()` function to fire - 8 the SAX events of an ElementTree against a SAX ContentHandler. - 9 - 10 See http://codespeak.net/lxml/sax.html - 11 """ - 12 - 13 from xml.sax.handler import ContentHandler - 14 from lxml import 1 # cython: language_level=2 + 2 + 3 """ + 4 SAX-based adapter to copy trees from/to the Python standard library. + 5 + 6 Use the `ElementTreeContentHandler` class to build an ElementTree from + 7 SAX events. + 8 + 9 Use the `ElementTreeProducer` class or the `saxify()` function to fire + 10 the SAX events of an ElementTree against a SAX ContentHandler. + 11 + 12 See http://codespeak.net/lxml/sax.html + 13 """ + 14 + 15 from __future__ import absolute_import + 16 + 17 from xml.sax.handler import ContentHandler + 18 from lxml import etree - 15 from lxml. 19 from lxml.etree import ElementTree, SubElement - 16 from lxml. 20 from lxml.etree import Comment, ProcessingInstruction - 17 - 18 - 22 - 23 -25 if 21 + 22 + 26 + 27 +29 if tag[0] == '{': - 26 return tuple( 30 return tuple(tag[1:].split('}', 1)) - 27 else: - 28 return (None, 31 else: + 32 return None, tag) -29 - 30 -32 """Build an lxml ElementTree from SAX events. - 33 """ -35 ContentHandler.tag +33 + 34 +36 """Build an lxml ElementTree from SAX events. + 37 """ +39 ContentHandler.__init__(self) - 36 self._root = None - 37 self._root_siblings = [] - 38 self._element_stack = [] - 39 self._default_ns = None - 40 self._ns_mapping = { None : [None] } - 41 self._new_mappings = {} - 42 if 40 self._root = None + 41 self._root_siblings = [] + 42 self._element_stack = [] + 43 self._default_ns = None + 44 self._ns_mapping = { None : [None] } + 45 self._new_mappings = {} + 46 if makeelement is None: - 43 47 makeelement = etree.Element - 44 self._makeelement = 48 self._makeelement = makeelement -45 -47 "Contains the generated ElementTree after parsing is finished." - 48 return 49 +51 "Contains the generated ElementTree after parsing is finished." + 52 return ElementTree(self._root) -49 - 50 53 + 54 etree = property(_get_etree, doc=_get_etree.__doc__) - 51 - 54 - 57 - 60 -62 self._new_mappings[prefix] = uri - 63 try: - 64 self._ns_mapping[prefix].append(uri) - 65 except KeyError: - 66 self._ns_mapping[prefix] = [uri] - 67 if prefix is None: - 68 self._default_ns = uri -69 -71 ns_uri_list = self._ns_mapping[prefix] - 72 ns_uri_list.pop() - 73 if prefix is None: - 74 self._default_ns = ns_uri_list[-1] -75 -77 ns_uri, local_name = ns_name_tuple - 78 if ns_uri: - 79 el_tag = "{%s}%s" % ns_name_tuple - 80 elif self._default_ns: - 81 el_tag = "{%s}%s" % (self._default_ns, local_name) - 82 else: - 83 el_tag = local_name - 84 return el_tag -85 -87 el_name = self._buildTag(ns_name) - 88 if attributes: - 89 attrs = {} - 90 try: - 91 iter_attributes = attributes. 55 + 58 + 61 + 64 +66 self._new_mappings[prefix] = uri + 67 try: + 68 self._ns_mapping[prefix].append(uri) + 69 except KeyError: + 70 self._ns_mapping[prefix] = [uri] + 71 if prefix is None: + 72 self._default_ns = uri +73 +75 ns_uri_list = self._ns_mapping[prefix] + 76 ns_uri_list.pop() + 77 if prefix is None: + 78 self._default_ns = ns_uri_list[-1] +79 +81 ns_uri, local_name = ns_name_tuple + 82 if ns_uri: + 83 el_tag = "{%s}%s" % ns_name_tuple + 84 elif self._default_ns: + 85 el_tag = "{%s}%s" % (self._default_ns, local_name) + 86 else: + 87 el_tag = local_name + 88 return el_tag +89 +91 el_name = self._buildTag(ns_name) + 92 if attributes: + 93 attrs = {} + 94 try: + 95 iter_attributes = attributes.iteritems() - 92 except AttributeError: - 93 iter_attributes = attributes. 96 except AttributeError: + 97 iter_attributes = attributes.items() - 94 - 95 for name_tuple, 98 + 99 for name_tuple, value in iter_attributes: - 96 if name_tuple[0]: - 97 attr_name = "{%s}%s" % name_tuple - 98 else: - 99 attr_name = name_tuple[1] -100 attrs[attr_name] = 100 if name_tuple[0]: +101 attr_name = "{%s}%s" % name_tuple +102 else: +103 attr_name = name_tuple[1] +104 attrs[attr_name] = value -101 else: -102 attrs = None -103 -104 element_stack = self._element_stack -105 if self._root is None: -106 element = self._root = \ -107 self._makeelement(el_name, attrs, self._new_mappings) -108 if self._root_siblings and hasattr(element, 'addprevious'): -109 for sibling in self._root_siblings: -110 element.addprevious(sibling) -111 del self._root_siblings[:] -112 else: -113 element = SubElement(element_stack[-1], el_name, -114 attrs, self._new_mappings) -115 element_stack.append(element) -116 -117 self._new_mappings.105 else: +106 attrs = None +107 +108 element_stack = self._element_stack +109 if self._root is None: +110 element = self._root = \ +111 self._makeelement(el_name, attrs, self._new_mappings) +112 if self._root_siblings and hasattr(element, 'addprevious'): +113 for sibling in self._root_siblings: +114 element.addprevious(sibling) +115 del self._root_siblings[:] +116 else: +117 element = SubElement(element_stack[-1], el_name, +118 attrs, self._new_mappings) +119 element_stack.append(element) +120 +121 self._new_mappings.clear() -118 -120 pi = ProcessingInstruction(target, data) -121 if self._root is None: -122 self._root_siblings.append(pi) -123 else: -124 self._element_stack[-1].append(pi) -125 -127 element = self._element_stack.pop() -128 el_tag = self._buildTag(ns_name) -129 if el_tag != element.122 +124 pi = ProcessingInstruction(target, data) +125 if self._root is None: +126 self._root_siblings.append(pi) +127 else: +128 self._element_stack[-1].append(pi) +129 +131 element = self._element_stack.pop() +132 el_tag = self._buildTag(ns_name) +133 if el_tag != element.tag: -130 raise SaxError("Unexpected element closed: " + el_tag) -131 -133 if attributes: -134 attributes = dict( -135 [((None, k), v) for k, v in attributes.134 raise SaxError("Unexpected element closed: " + el_tag) +135 +137 if attributes: +138 attributes = dict( +139 [((None, k), v) for k, v in attributes.items()] -136 ) -137 self.startElementNS((None, 140 ) +141 self.startElementNS((None, name), name, attributes) -138 -140 self.endElementNS((None, 142 +141 -143 last_element = self._element_stack[-1] -144 try: -145 # if there already is a child element, we must append to its tail -146 last_element = last_element[-1] -147 last_element.145 +153 -154 -147 last_element = self._element_stack[-1] +148 try: +149 # if there already is a child element, we must append to its tail +150 last_element = last_element[-1] +151 last_element.tail = (last_element.tail or '') + data -148 except IndexError: -149 # otherwise: append to the text -150 last_element.tail or '') + data +152 except IndexError: +153 # otherwise: append to the text +154 last_element.text or '') + data -151 -152 ignorableWhitespace = characters -156 """Produces SAX events for an element and children. -157 """ -157 +158 +159 try: -160 element = element_or_tree.getroot() -161 except AttributeError: -162 element = element_or_tree -163 self._element = element -164 self._content_handler = content_handler -165 from xml.sax.xmlreader import AttributesNSImpl as attr_class -166 self._attr_class = attr_class -167 self._empty_attributes = attr_class({}, {}) -168 -170 self._content_handler.startDocument() -171 -172 element = self._element -173 if hasattr(element, 'getprevious'): -174 siblings = [] -175 sibling = element.getprevious() -176 while getattr(sibling, 'tag', None) is ProcessingInstruction: -177 siblings.append(sibling) -178 sibling = sibling.getprevious() -179 for sibling in siblings[::-1]: -180 self._recursive_saxify(sibling, {}) -181 -182 self._recursive_saxify(element, {}) -183 -184 if hasattr(element, 'getnext'): -185 sibling = element.getnext() -186 while getattr(sibling, 'tag', None) is ProcessingInstruction: -187 self._recursive_saxify(sibling, {}) -188 sibling = sibling.getnext() -189 -190 self._content_handler.endDocument() -191 -155 +156 ignorableWhitespace = characters +160 """Produces SAX events for an element and children. +161 """ +244 -163 try: +164 element = element_or_tree.getroot() +165 except AttributeError: +166 element = element_or_tree +167 self._element = element +168 self._content_handler = content_handler +169 from xml.sax.xmlreader import AttributesNSImpl as attr_class +170 self._attr_class = attr_class +171 self._empty_attributes = attr_class({}, {}) +172 +174 self._content_handler.startDocument() +175 +176 element = self._element +177 if hasattr(element, 'getprevious'): +178 siblings = [] +179 sibling = element.getprevious() +180 while getattr(sibling, 'tag', None) is ProcessingInstruction: +181 siblings.append(sibling) +182 sibling = sibling.getprevious() +183 for sibling in siblings[::-1]: +184 self._recursive_saxify(sibling, {}) +185 +186 self._recursive_saxify(element, {}) +187 +188 if hasattr(element, 'getnext'): +189 sibling = element.getnext() +190 while getattr(sibling, 'tag', None) is ProcessingInstruction: +191 self._recursive_saxify(sibling, {}) +192 sibling = sibling.getnext() +193 +194 self._content_handler.endDocument() +195 +197 content_handler = self._content_handler +198 tag = element.tag = element.tag -195 if tag +199 if tag is Comment or tag is Comment or tag is ProcessingInstruction: -196 if tag is ProcessingInstruction: +200 if tag is ProcessingInstruction: -197 content_handler.processingInstruction( -198 element.target, element.tag is ProcessingInstruction: +201 content_handler.processingInstruction( +202 element.target, element.text) -199 if element.tail: -200 content_handler.characters(element.tail) -201 return -202 -203 new_prefixes = [] -204 build_qname = self._build_qname -205 attribs = element.text) +203 tail = element.tail +204 if tail: +205 content_handler.characters(tail) +206 return +207 +208 element_nsmap = element.nsmap +209 new_prefixes = [] +210 if element_nsmap != parent_nsmap: +211 # There have been updates to the namespace +212 for prefix, ns_uri in element_nsmap.items(): +213 if parent_nsmap.get(prefix) != ns_uri: +214 new_prefixes.append( (prefix, ns_uri) ) +215 +216 attribs = element.items() -206 if attribs: -207 attr_values = {} -208 attr_qnames = {} -209 for attr_ns_name, items() +217 if attribs: +218 attr_values = {} +219 attr_qnames = {} +220 for attr_ns_name, value in attribs: -210 attr_ns_tuple = _getNsTag(attr_ns_name) -211 attr_values[attr_ns_tuple] = value in attribs: +221 attr_ns_tuple = _getNsTag(attr_ns_name) +222 attr_values[attr_ns_tuple] = value -212 attr_qnames[attr_ns_tuple] = build_qname( -213 attr_ns_tuple[0], attr_ns_tuple[1], prefixes, new_prefixes) -214 sax_attributes = self._attr_class(attr_values, attr_qnames) -215 else: -216 sax_attributes = self._empty_attributes -217 -218 ns_uri, local_name = _getNsTag(value +223 attr_qnames[attr_ns_tuple] = self._build_qname( +224 attr_ns_tuple[0], attr_ns_tuple[1], element_nsmap, +225 preferred_prefix=None, is_attribute=True) +226 sax_attributes = self._attr_class(attr_values, attr_qnames) +227 else: +228 sax_attributes = self._empty_attributes +229 +230 ns_uri, local_name = _getNsTag(tag) -219 qname = build_qname(ns_uri, local_name, prefixes, new_prefixes) -220 -221 for prefix, uri in new_prefixes: -222 content_handler.startPrefixMapping(prefix, uri) -223 content_handler.startElementNS((ns_uri, local_name), -224 qname, sax_attributes) -225 if element.tag) +231 qname = self._build_qname( +232 ns_uri, local_name, element_nsmap, element.prefix, is_attribute=False) +233 +234 for prefix, uri in new_prefixes: +235 content_handler.startPrefixMapping(prefix, uri) +236 content_handler.startElementNS( +237 (ns_uri, local_name), qname, sax_attributes) +238 text = element.text +239 if text: -226 content_handler.characters(element.text: +240 content_handler.characters(text) -227 for child in element: -228 self._recursive_saxify(child, prefixes) -229 content_handler.endElementNS((ns_uri, local_name), qname) -230 for prefix, uri in new_prefixes: -231 content_handler.endPrefixMapping(prefix) -232 if element.tail: -233 content_handler.characters(element.tail) -234 -246 """One-shot helper to generate SAX events from an XML tree and fire -247 them against a SAX ContentHandler. -248 """ -249 return ElementTreeProducer(element_or_tree, content_handler).saxify() -250