home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Lib / Python2.0 / xml / sax / __init__.py next >
Encoding:
Python Source  |  2000-10-16  |  3.4 KB  |  104 lines

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