home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / xml / sax / __init__.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.6 KB  |  113 lines

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