+++ /dev/null
-"""Registration facilities for DOM. This module should not be used\r
-directly. Instead, the functions getDOMImplementation and\r
-registerDOMImplementation should be imported from xml.dom."""\r
-\r
-from xml.dom.minicompat import * # isinstance, StringTypes\r
-\r
-# This is a list of well-known implementations. Well-known names\r
-# should be published by posting to xml-sig@python.org, and are\r
-# subsequently recorded in this file.\r
-\r
-well_known_implementations = {\r
- 'minidom':'xml.dom.minidom',\r
- '4DOM': 'xml.dom.DOMImplementation',\r
- }\r
-\r
-# DOM implementations not officially registered should register\r
-# themselves with their\r
-\r
-registered = {}\r
-\r
-def registerDOMImplementation(name, factory):\r
- """registerDOMImplementation(name, factory)\r
-\r
- Register the factory function with the name. The factory function\r
- should return an object which implements the DOMImplementation\r
- interface. The factory function can either return the same object,\r
- or a new one (e.g. if that implementation supports some\r
- customization)."""\r
-\r
- registered[name] = factory\r
-\r
-def _good_enough(dom, features):\r
- "_good_enough(dom, features) -> Return 1 if the dom offers the features"\r
- for f,v in features:\r
- if not dom.hasFeature(f,v):\r
- return 0\r
- return 1\r
-\r
-def getDOMImplementation(name = None, features = ()):\r
- """getDOMImplementation(name = None, features = ()) -> DOM implementation.\r
-\r
- Return a suitable DOM implementation. The name is either\r
- well-known, the module name of a DOM implementation, or None. If\r
- it is not None, imports the corresponding module and returns\r
- DOMImplementation object if the import succeeds.\r
-\r
- If name is not given, consider the available implementations to\r
- find one with the required feature set. If no implementation can\r
- be found, raise an ImportError. The features list must be a sequence\r
- of (feature, version) pairs which are passed to hasFeature."""\r
-\r
- import os\r
- creator = None\r
- mod = well_known_implementations.get(name)\r
- if mod:\r
- mod = __import__(mod, {}, {}, ['getDOMImplementation'])\r
- return mod.getDOMImplementation()\r
- elif name:\r
- return registered[name]()\r
- elif "PYTHON_DOM" in os.environ:\r
- return getDOMImplementation(name = os.environ["PYTHON_DOM"])\r
-\r
- # User did not specify a name, try implementations in arbitrary\r
- # order, returning the one that has the required features\r
- if isinstance(features, StringTypes):\r
- features = _parse_feature_string(features)\r
- for creator in registered.values():\r
- dom = creator()\r
- if _good_enough(dom, features):\r
- return dom\r
-\r
- for creator in well_known_implementations.keys():\r
- try:\r
- dom = getDOMImplementation(name = creator)\r
- except StandardError: # typically ImportError, or AttributeError\r
- continue\r
- if _good_enough(dom, features):\r
- return dom\r
-\r
- raise ImportError,"no suitable DOM implementation found"\r
-\r
-def _parse_feature_string(s):\r
- features = []\r
- parts = s.split()\r
- i = 0\r
- length = len(parts)\r
- while i < length:\r
- feature = parts[i]\r
- if feature[0] in "0123456789":\r
- raise ValueError, "bad feature name: %r" % (feature,)\r
- i = i + 1\r
- version = None\r
- if i < length:\r
- v = parts[i]\r
- if v[0] in "0123456789":\r
- i = i + 1\r
- version = v\r
- features.append((feature, version))\r
- return tuple(features)\r