Kaydet (Commit) fb73bb12 authored tarafından Martin v. Löwis's avatar Martin v. Löwis

Synchronize with 1.13 of PyXML:

Allow application to set a new content handler and lex_prop handler during
parsing. Closes bug #433761.
Small hack to make expat be ignored in Jython.
üst d083839f
...@@ -6,6 +6,13 @@ pyexpat.__version__ == '2.22'. ...@@ -6,6 +6,13 @@ pyexpat.__version__ == '2.22'.
version = "0.20" version = "0.20"
from xml.sax._exceptions import * from xml.sax._exceptions import *
# xml.parsers.expat does not raise ImportError in Jython
import sys
if sys.platform[ : 4] == "java":
raise SAXReaderNotAvailable("expat not available in Java", None)
del sys
try: try:
from xml.parsers import expat from xml.parsers import expat
except ImportError: except ImportError:
...@@ -46,6 +53,13 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): ...@@ -46,6 +53,13 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
if source.getSystemId() != None: if source.getSystemId() != None:
self._parser.SetBase(source.getSystemId()) self._parser.SetBase(source.getSystemId())
# Redefined setContentHandle to allow changing handlers during parsing
def setContentHandler(self, handler):
xmlreader.IncrementalParser.setContentHandler(self, handler)
if self._parsing:
self._reset_cont_handler()
def getFeature(self, name): def getFeature(self, name):
if name == handler.feature_namespaces: if name == handler.feature_namespaces:
return self._namespaces return self._namespaces
...@@ -68,6 +82,8 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): ...@@ -68,6 +82,8 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
def setProperty(self, name, value): def setProperty(self, name, value):
if name == handler.property_lexical_handler: if name == handler.property_lexical_handler:
self._lex_handler_prop = value self._lex_handler_prop = value
if self._parsing:
self._reset_lex_handler_prop()
else: else:
raise SAXNotRecognizedException("Property '%s' not recognized" % name) raise SAXNotRecognizedException("Property '%s' not recognized" % name)
...@@ -101,6 +117,16 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): ...@@ -101,6 +117,16 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# break cycle created by expat handlers pointing to our methods # break cycle created by expat handlers pointing to our methods
self._parser = None self._parser = None
def _reset_cont_handler(self):
self._parser.ProcessingInstructionHandler = \
self._cont_handler.processingInstruction
self._parser.CharacterDataHandler = self._cont_handler.characters
def _reset_lex_handler_prop(self):
self._parser.CommentHandler = self._lex_handler_prop.comment
self._parser.StartCdataSectionHandler = self._lex_handler_prop.startCDATA
self._parser.EndCdataSectionHandler = self._lex_handler_prop.endCDATA
def reset(self): def reset(self):
if self._namespaces: if self._namespaces:
self._parser = expat.ParserCreate(None, " ") self._parser = expat.ParserCreate(None, " ")
...@@ -111,9 +137,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): ...@@ -111,9 +137,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._parser.StartElementHandler = self.start_element self._parser.StartElementHandler = self.start_element
self._parser.EndElementHandler = self.end_element self._parser.EndElementHandler = self.end_element
self._parser.ProcessingInstructionHandler = \ self._reset_cont_handler()
self._cont_handler.processingInstruction
self._parser.CharacterDataHandler = self._cont_handler.characters
self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
self._parser.NotationDeclHandler = self.notation_decl self._parser.NotationDeclHandler = self.notation_decl
self._parser.StartNamespaceDeclHandler = self.start_namespace_decl self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
...@@ -121,9 +145,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): ...@@ -121,9 +145,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._decl_handler_prop = None self._decl_handler_prop = None
if self._lex_handler_prop: if self._lex_handler_prop:
self._parser.CommentHandler = self._lex_handler_prop.comment self._reset_lex_handler_prop()
self._parser.StartCdataSectionHandler = self._lex_handler_prop.startCDATA
self._parser.EndCdataSectionHandler = self._lex_handler_prop.endCDATA
# self._parser.DefaultHandler = # self._parser.DefaultHandler =
# self._parser.DefaultHandlerExpand = # self._parser.DefaultHandlerExpand =
# self._parser.NotStandaloneHandler = # self._parser.NotStandaloneHandler =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment