/Lib/xml/sax/__init__.py

http://unladen-swallow.googlecode.com/ · Python · 108 lines · 91 code · 5 blank · 12 comment · 2 complexity · 321eb277e9221720c222af17f5815072 MD5 · raw file

  1. """Simple API for XML (SAX) implementation for Python.
  2. This module provides an implementation of the SAX 2 interface;
  3. information about the Java version of the interface can be found at
  4. http://www.megginson.com/SAX/. The Python version of the interface is
  5. documented at <...>.
  6. This package contains the following modules:
  7. handler -- Base classes and constants which define the SAX 2 API for
  8. the 'client-side' of SAX for Python.
  9. saxutils -- Implementation of the convenience classes commonly used to
  10. work with SAX.
  11. xmlreader -- Base classes and constants which define the SAX 2 API for
  12. the parsers used with SAX for Python.
  13. expatreader -- Driver that allows use of the Expat parser with SAX.
  14. """
  15. from xmlreader import InputSource
  16. from handler import ContentHandler, ErrorHandler
  17. from _exceptions import SAXException, SAXNotRecognizedException, \
  18. SAXParseException, SAXNotSupportedException, \
  19. SAXReaderNotAvailable
  20. def parse(source, handler, errorHandler=ErrorHandler()):
  21. parser = make_parser()
  22. parser.setContentHandler(handler)
  23. parser.setErrorHandler(errorHandler)
  24. parser.parse(source)
  25. def parseString(string, handler, errorHandler=ErrorHandler()):
  26. try:
  27. from cStringIO import StringIO
  28. except ImportError:
  29. from StringIO import StringIO
  30. if errorHandler is None:
  31. errorHandler = ErrorHandler()
  32. parser = make_parser()
  33. parser.setContentHandler(handler)
  34. parser.setErrorHandler(errorHandler)
  35. inpsrc = InputSource()
  36. inpsrc.setByteStream(StringIO(string))
  37. parser.parse(inpsrc)
  38. # this is the parser list used by the make_parser function if no
  39. # alternatives are given as parameters to the function
  40. default_parser_list = ["xml.sax.expatreader"]
  41. # tell modulefinder that importing sax potentially imports expatreader
  42. _false = 0
  43. if _false:
  44. import xml.sax.expatreader
  45. import os, sys
  46. if os.environ.has_key("PY_SAX_PARSER"):
  47. default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
  48. del os
  49. _key = "python.xml.sax.parser"
  50. if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
  51. default_parser_list = sys.registry.getProperty(_key).split(",")
  52. def make_parser(parser_list = []):
  53. """Creates and returns a SAX parser.
  54. Creates the first parser it is able to instantiate of the ones
  55. given in the list created by doing parser_list +
  56. default_parser_list. The lists must contain the names of Python
  57. modules containing both a SAX parser and a create_parser function."""
  58. for parser_name in parser_list + default_parser_list:
  59. try:
  60. return _create_parser(parser_name)
  61. except ImportError,e:
  62. import sys
  63. if parser_name in sys.modules:
  64. # The parser module was found, but importing it
  65. # failed unexpectedly, pass this exception through
  66. raise
  67. except SAXReaderNotAvailable:
  68. # The parser module detected that it won't work properly,
  69. # so try the next one
  70. pass
  71. raise SAXReaderNotAvailable("No parsers found", None)
  72. # --- Internal utility methods used by make_parser
  73. if sys.platform[ : 4] == "java":
  74. def _create_parser(parser_name):
  75. from org.python.core import imp
  76. drv_module = imp.importName(parser_name, 0, globals())
  77. return drv_module.create_parser()
  78. else:
  79. def _create_parser(parser_name):
  80. drv_module = __import__(parser_name,{},{},['create_parser'])
  81. return drv_module.create_parser()
  82. del sys