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