PyXR

c:\python24\lib \ xml \ sax

Subpackages:

Modules

Init code



0001 """Simple API for XML (SAX) implementation for Python.
0002 
0003 This module provides an implementation of the SAX 2 interface;
0004 information about the Java version of the interface can be found at
0005 http://www.megginson.com/SAX/.  The Python version of the interface is
0006 documented at <...>.
0007 
0008 This package contains the following modules:
0009 
0010 handler -- Base classes and constants which define the SAX 2 API for
0011            the 'client-side' of SAX for Python.
0012 
0013 saxutils -- Implementation of the convenience classes commonly used to
0014             work with SAX.
0015 
0016 xmlreader -- Base classes and constants which define the SAX 2 API for
0017              the parsers used with SAX for Python.
0018 
0019 expatreader -- Driver that allows use of the Expat parser with SAX.
0020 """
0021 
0022 from xmlreader import InputSource
0023 from handler import ContentHandler, ErrorHandler
0024 from _exceptions import SAXException, SAXNotRecognizedException, \
0025                         SAXParseException, SAXNotSupportedException, \
0026                         SAXReaderNotAvailable
0027 
0028 
0029 def parse(source, handler, errorHandler=ErrorHandler()):
0030     parser = make_parser()
0031     parser.setContentHandler(handler)
0032     parser.setErrorHandler(errorHandler)
0033     parser.parse(source)
0034 
0035 def parseString(string, handler, errorHandler=ErrorHandler()):
0036     try:
0037         from cStringIO import StringIO
0038     except ImportError:
0039         from StringIO import StringIO
0040 
0041     if errorHandler is None:
0042         errorHandler = ErrorHandler()
0043     parser = make_parser()
0044     parser.setContentHandler(handler)
0045     parser.setErrorHandler(errorHandler)
0046 
0047     inpsrc = InputSource()
0048     inpsrc.setByteStream(StringIO(string))
0049     parser.parse(inpsrc)
0050 
0051 # this is the parser list used by the make_parser function if no
0052 # alternatives are given as parameters to the function
0053 
0054 default_parser_list = ["xml.sax.expatreader"]
0055 
0056 # tell modulefinder that importing sax potentially imports expatreader
0057 _false = 0
0058 if _false:
0059     import xml.sax.expatreader
0060 
0061 import os, sys
0062 if os.environ.has_key("PY_SAX_PARSER"):
0063     default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
0064 del os
0065 
0066 _key = "python.xml.sax.parser"
0067 if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
0068     default_parser_list = sys.registry.getProperty(_key).split(",")
0069 
0070 
0071 def make_parser(parser_list = []):
0072     """Creates and returns a SAX parser.
0073 
0074     Creates the first parser it is able to instantiate of the ones
0075     given in the list created by doing parser_list +
0076     default_parser_list.  The lists must contain the names of Python
0077     modules containing both a SAX parser and a create_parser function."""
0078 
0079     for parser_name in parser_list + default_parser_list:
0080         try:
0081             return _create_parser(parser_name)
0082         except ImportError,e:
0083             import sys
0084             if sys.modules.has_key(parser_name):
0085                 # The parser module was found, but importing it
0086                 # failed unexpectedly, pass this exception through
0087                 raise
0088         except SAXReaderNotAvailable:
0089             # The parser module detected that it won't work properly,
0090             # so try the next one
0091             pass
0092 
0093     raise SAXReaderNotAvailable("No parsers found", None)
0094 
0095 # --- Internal utility methods used by make_parser
0096 
0097 if sys.platform[ : 4] == "java":
0098     def _create_parser(parser_name):
0099         from org.python.core import imp
0100         drv_module = imp.importName(parser_name, 0, globals())
0101         return drv_module.create_parser()
0102 
0103 else:
0104     def _create_parser(parser_name):
0105         drv_module = __import__(parser_name,{},{},['create_parser'])
0106         return drv_module.create_parser()
0107 
0108 del sys
0109 

Generated by PyXR 0.9.4
SourceForge.net Logo