annotate Lib/xml/sax/expatreader.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents e33b65627188
children d3e7aea8a550
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39275
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 SAX driver for the pyexpat C module. This driver works with
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 pyexpat.__version__ == '2.22'.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 """
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 version = "0.20"
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 from xml.sax._exceptions import *
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 from xml.sax.handler import feature_validation, feature_namespaces
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 from xml.sax.handler import feature_namespace_prefixes
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 from xml.sax.handler import feature_external_ges, feature_external_pes
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 from xml.sax.handler import feature_string_interning
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 from xml.sax.handler import property_xml_string, property_interning_dict
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 # xml.parsers.expat does not raise ImportError in Jython
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 import sys
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 if sys.platform[:4] == "java":
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 raise SAXReaderNotAvailable("expat not available in Java", None)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 del sys
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 try:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 from xml.parsers import expat
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 except ImportError:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 raise SAXReaderNotAvailable("expat not supported", None)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 if not hasattr(expat, "ParserCreate"):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 raise SAXReaderNotAvailable("expat not supported", None)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 from xml.sax import xmlreader, saxutils, handler
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 AttributesImpl = xmlreader.AttributesImpl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 AttributesNSImpl = xmlreader.AttributesNSImpl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 # If we're using a sufficiently recent version of Python, we can use
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 # weak references to avoid cycles between the parser and content
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 # handler, otherwise we'll just have to pretend.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 try:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 import _weakref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 except ImportError:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 def _mkproxy(o):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 return o
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 import weakref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 _mkproxy = weakref.proxy
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 del weakref, _weakref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 # --- ExpatLocator
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 class ExpatLocator(xmlreader.Locator):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 """Locator for use with the ExpatParser class.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 This uses a weak reference to the parser object to avoid creating
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 a circular reference between the parser and the content handler.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 """
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 def __init__(self, parser):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 self._ref = _mkproxy(parser)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 def getColumnNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 parser = self._ref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 if parser._parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 return None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 return parser._parser.ErrorColumnNumber
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 def getLineNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 parser = self._ref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 if parser._parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 return 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 return parser._parser.ErrorLineNumber
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 def getPublicId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 parser = self._ref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71 if parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 return None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 return parser._source.getPublicId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 def getSystemId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 parser = self._ref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 if parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 return None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 return parser._source.getSystemId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 # --- ExpatParser
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 """SAX driver for the pyexpat C module."""
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 def __init__(self, namespaceHandling=0, bufsize=2**16-20):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 xmlreader.IncrementalParser.__init__(self, bufsize)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 self._source = xmlreader.InputSource()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 self._parser = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 self._namespaces = namespaceHandling
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 self._lex_handler_prop = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 self._parsing = 0
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 self._entity_stack = []
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 self._external_ges = 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 self._interning = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 # XMLReader methods
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 def parse(self, source):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 "Parse an XML document from a URL or an InputSource."
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 source = saxutils.prepare_input_source(source)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 self._source = source
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 self.reset()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 self._cont_handler.setDocumentLocator(ExpatLocator(self))
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 xmlreader.IncrementalParser.parse(self, source)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 def prepareParser(self, source):
46197
01a7e66525c2 #2503 make singletons compared with "is" not == or !=
Benjamin Peterson <benjamin@python.org>
parents: 39275
diff changeset
110 if source.getSystemId() is not None:
39275
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 self._parser.SetBase(source.getSystemId())
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 # Redefined setContentHandler to allow changing handlers during parsing
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 def setContentHandler(self, handler):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 xmlreader.IncrementalParser.setContentHandler(self, handler)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 if self._parsing:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 self._reset_cont_handler()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 def getFeature(self, name):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 if name == feature_namespaces:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 return self._namespaces
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 elif name == feature_string_interning:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 return self._interning is not None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 elif name in (feature_validation, feature_external_pes,
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 feature_namespace_prefixes):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 return 0
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 elif name == feature_external_ges:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 return self._external_ges
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132 def setFeature(self, name, state):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 if self._parsing:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 raise SAXNotSupportedException("Cannot set features while parsing")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 if name == feature_namespaces:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 self._namespaces = state
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 elif name == feature_external_ges:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 self._external_ges = state
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 elif name == feature_string_interning:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 if state:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 if self._interning is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 self._interning = {}
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 self._interning = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146 elif name == feature_validation:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147 if state:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148 raise SAXNotSupportedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 "expat does not support validation")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 elif name == feature_external_pes:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 if state:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 raise SAXNotSupportedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153 "expat does not read external parameter entities")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 elif name == feature_namespace_prefixes:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155 if state:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156 raise SAXNotSupportedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 "expat does not report namespace prefixes")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 raise SAXNotRecognizedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 "Feature '%s' not recognized" % name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
161
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162 def getProperty(self, name):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163 if name == handler.property_lexical_handler:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
164 return self._lex_handler_prop
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
165 elif name == property_interning_dict:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 return self._interning
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 elif name == property_xml_string:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 if self._parser:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 if hasattr(self._parser, "GetInputContext"):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170 return self._parser.GetInputContext()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 raise SAXNotRecognizedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173 "This version of expat does not support getting"
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174 " the XML string")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 raise SAXNotSupportedException(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 "XML string cannot be returned when not parsing")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 raise SAXNotRecognizedException("Property '%s' not recognized" % name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 def setProperty(self, name, value):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 if name == handler.property_lexical_handler:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 self._lex_handler_prop = value
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 if self._parsing:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 self._reset_lex_handler_prop()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 elif name == property_interning_dict:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 self._interning = value
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 elif name == property_xml_string:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 raise SAXNotSupportedException("Property '%s' cannot be set" %
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189 name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
191 raise SAXNotRecognizedException("Property '%s' not recognized" %
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
192 name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 # IncrementalParser methods
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 def feed(self, data, isFinal = 0):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 if not self._parsing:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 self.reset()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199 self._parsing = 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 self._cont_handler.startDocument()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
201
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 try:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 # The isFinal parameter is internal to the expat reader.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 # If it is set to true, expat will check validity of the entire
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 # document. When feeding chunks, they are not normally final -
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
206 # except when invoked from close.
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 self._parser.Parse(data, isFinal)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208 except expat.error, e:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209 exc = SAXParseException(expat.ErrorString(e.code), e, self)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 # FIXME: when to invoke error()?
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 self._err_handler.fatalError(exc)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213 def close(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214 if self._entity_stack:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 # If we are completing an external entity, do nothing here
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
216 return
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 self.feed("", isFinal = 1)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218 self._cont_handler.endDocument()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 self._parsing = 0
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 # break cycle created by expat handlers pointing to our methods
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 self._parser = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 def _reset_cont_handler(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
224 self._parser.ProcessingInstructionHandler = \
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
225 self._cont_handler.processingInstruction
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226 self._parser.CharacterDataHandler = self._cont_handler.characters
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
227
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
228 def _reset_lex_handler_prop(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
229 lex = self._lex_handler_prop
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
230 parser = self._parser
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231 if lex is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
232 parser.CommentHandler = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233 parser.StartCdataSectionHandler = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
234 parser.EndCdataSectionHandler = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
235 parser.StartDoctypeDeclHandler = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
236 parser.EndDoctypeDeclHandler = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 parser.CommentHandler = lex.comment
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239 parser.StartCdataSectionHandler = lex.startCDATA
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
240 parser.EndCdataSectionHandler = lex.endCDATA
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 parser.StartDoctypeDeclHandler = self.start_doctype_decl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 parser.EndDoctypeDeclHandler = lex.endDTD
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
244 def reset(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245 if self._namespaces:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
246 self._parser = expat.ParserCreate(self._source.getEncoding(), " ",
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
247 intern=self._interning)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
248 self._parser.namespace_prefixes = 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
249 self._parser.StartElementHandler = self.start_element_ns
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
250 self._parser.EndElementHandler = self.end_element_ns
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
251 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
252 self._parser = expat.ParserCreate(self._source.getEncoding(),
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
253 intern = self._interning)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
254 self._parser.StartElementHandler = self.start_element
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
255 self._parser.EndElementHandler = self.end_element
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
256
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
257 self._reset_cont_handler()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 self._parser.NotationDeclHandler = self.notation_decl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
260 self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 self._parser.EndNamespaceDeclHandler = self.end_namespace_decl
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 self._decl_handler_prop = None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 if self._lex_handler_prop:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 self._reset_lex_handler_prop()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 # self._parser.DefaultHandler =
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 # self._parser.DefaultHandlerExpand =
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 # self._parser.NotStandaloneHandler =
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 self._parser.ExternalEntityRefHandler = self.external_entity_ref
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270 try:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 self._parser.SkippedEntityHandler = self.skipped_entity_handler
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272 except AttributeError:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273 # This pyexpat does not support SkippedEntity
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
274 pass
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275 self._parser.SetParamEntityParsing(
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
276 expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
277
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
278 self._parsing = 0
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
279 self._entity_stack = []
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
280
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
281 # Locator methods
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
282
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 def getColumnNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284 if self._parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285 return None
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
286 return self._parser.ErrorColumnNumber
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
287
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
288 def getLineNumber(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
289 if self._parser is None:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
290 return 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
291 return self._parser.ErrorLineNumber
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293 def getPublicId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294 return self._source.getPublicId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296 def getSystemId(self):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
297 return self._source.getSystemId()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
298
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
299 # event handlers
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
300 def start_element(self, name, attrs):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
301 self._cont_handler.startElement(name, AttributesImpl(attrs))
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
302
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
303 def end_element(self, name):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
304 self._cont_handler.endElement(name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
305
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
306 def start_element_ns(self, name, attrs):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307 pair = name.split()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 if len(pair) == 1:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 # no namespace
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310 pair = (None, name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311 elif len(pair) == 3:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312 pair = pair[0], pair[1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
314 # default namespace
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 pair = tuple(pair)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
317 newattrs = {}
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318 qnames = {}
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319 for (aname, value) in attrs.items():
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
320 parts = aname.split()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
321 length = len(parts)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
322 if length == 1:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
323 # no namespace
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324 qname = aname
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325 apair = (None, aname)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
326 elif length == 3:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 qname = "%s:%s" % (parts[2], parts[1])
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
328 apair = parts[0], parts[1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
329 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330 # default namespace
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 qname = parts[1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332 apair = tuple(parts)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
333
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334 newattrs[apair] = value
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 qnames[apair] = qname
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 self._cont_handler.startElementNS(pair, None,
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338 AttributesNSImpl(newattrs, qnames))
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 def end_element_ns(self, name):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 pair = name.split()
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 if len(pair) == 1:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 pair = (None, name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 elif len(pair) == 3:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 pair = pair[0], pair[1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 else:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347 pair = tuple(pair)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 self._cont_handler.endElementNS(pair, None)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 # this is not used (call directly to ContentHandler)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 def processing_instruction(self, target, data):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 self._cont_handler.processingInstruction(target, data)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 # this is not used (call directly to ContentHandler)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 def character_data(self, data):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 self._cont_handler.characters(data)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
359 def start_namespace_decl(self, prefix, uri):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360 self._cont_handler.startPrefixMapping(prefix, uri)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
361
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
362 def end_namespace_decl(self, prefix):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
363 self._cont_handler.endPrefixMapping(prefix)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
364
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
365 def start_doctype_decl(self, name, sysid, pubid, has_internal_subset):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
366 self._lex_handler_prop.startDTD(name, pubid, sysid)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
368 def unparsed_entity_decl(self, name, base, sysid, pubid, notation_name):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
369 self._dtd_handler.unparsedEntityDecl(name, pubid, sysid, notation_name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
370
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
371 def notation_decl(self, name, base, sysid, pubid):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 self._dtd_handler.notationDecl(name, pubid, sysid)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
374 def external_entity_ref(self, context, base, sysid, pubid):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
375 if not self._external_ges:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
376 return 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 source = self._ent_handler.resolveEntity(pubid, sysid)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 source = saxutils.prepare_input_source(source,
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 self._source.getSystemId() or
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 "")
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383 self._entity_stack.append((self._parser, self._source))
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 self._parser = self._parser.ExternalEntityParserCreate(context)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 self._source = source
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
386
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
387 try:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
388 xmlreader.IncrementalParser.parse(self, source)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
389 except:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
390 return 0 # FIXME: save error info here?
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
391
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
392 (self._parser, self._source) = self._entity_stack[-1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
393 del self._entity_stack[-1]
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
394 return 1
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
395
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
396 def skipped_entity_handler(self, name, is_pe):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
397 if is_pe:
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
398 # The SAX spec requires to report skipped PEs with a '%'
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
399 name = '%'+name
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
400 self._cont_handler.skippedEntity(name)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
401
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
402 # ---
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
403
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404 def create_parser(*args, **kwargs):
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 return ExpatParser(*args, **kwargs)
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 # ---
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 if __name__ == "__main__":
54815
e33b65627188 #4490 Fix sample code run by "python -m xml.sax.xmlreader"
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46197
diff changeset
410 import xml.sax.saxutils
39275
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 p = create_parser()
54815
e33b65627188 #4490 Fix sample code run by "python -m xml.sax.xmlreader"
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46197
diff changeset
412 p.setContentHandler(xml.sax.saxutils.XMLGenerator())
39275
cb1e432b4919 expunge the xmlcore changes:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
413 p.setErrorHandler(xml.sax.ErrorHandler())
54815
e33b65627188 #4490 Fix sample code run by "python -m xml.sax.xmlreader"
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46197
diff changeset
414 p.parse("http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml")