Author: scoder
Date: Wed Jul 26 19:16:47 2006
New Revision: 30600
Modified:
lxml/branch/capi/doc/objectify.txt
lxml/branch/capi/src/lxml/objectify.pyx
Log:
ObjectifyElementClassLookup: support for using objectify on a per-parser basis
etc. through the classlookup framework
Modified: lxml/branch/capi/doc/objectify.txt
==============================================================================
--- lxml/branch/capi/doc/objectify.txt (original)
+++ lxml/branch/capi/doc/objectify.txt Wed Jul 26 19:16:47 2006
@@ -4,7 +4,7 @@
lxml supports an alternative element API similar to the Amara_ bindery through
a custom Element implementation. This API is very different from the
-ElementTree API. If it is used, it can only be used *exclusively*, to avoid
+ElementTree API. If it is used, it should be used exclusively, to avoid
common pitfalls when mixing element implementations.
.. _Amara: http://uche.ogbuji.net/tech/4suite/amara/
@@ -27,24 +27,38 @@
.. _`namespace specific classes`: namespace_extensions.html
+It is possible to use objectify's element classes on a more fine-grained
+basis. Instead of activating it globally, it can be integrated with the class
+lookup framework from `lxml.elements.classlookup`_. This is accomplished by
+the class ``ObjectifyElementClassLookup``. By setting it as the local class
+lookup scheme of a parser, for example, you can restrict the objectify API to
+documents that were parsed by this specific parser. As said above, you really
+have to take care in this case to prevent mixing the Element implementations
+between documents. If you do, however, this provides a very convenient way of
+using different XML APIs at the same time, e.g. in differen Python modules.
+
+.. _`lxml.elements.classlookup`: elements.html
+
Since this API is meant for data-centered XML (as opposed to document XML with
mixed content), it might be worth-wile in this context to change the default
parser::
>>> etree.setDefaultParser( etree.XMLParser(remove_blank_text=True) )
-Now the parser will remove whitespace-only text from the parsed document.
-Note that this alters the document infoset, so if you consider spaces as data
-in your specific use case, you should go with the normal parser.
-
+Now the parser will remove whitespace-only text from the parsed document,
+unless it is found enclosed by an XML element. Note that this alters the
+document infoset, so if you consider the removed spaces as data in your
+specific use case, you should go with the normal parser.
.. contents::
..
1 Element access through object attributes
2 Namespace handling
3 Python data types
- 4 What is different from ElementTree?
- 5 Resetting the API
+ 4 Defining additional data classes
+ 5 Recursive string representation of elements
+ 6 What is different from ElementTree?
+ 7 Resetting the API
Element access through object attributes
Modified: lxml/branch/capi/src/lxml/objectify.pyx
==============================================================================
--- lxml/branch/capi/src/lxml/objectify.pyx (original)
+++ lxml/branch/capi/src/lxml/objectify.pyx Wed Jul 26 19:16:47 2006
@@ -1,4 +1,5 @@
-from etreepublic cimport _Document, _Element, ElementBase, _ElementIterator
+from etreepublic cimport _Document, _Element, ElementBase
+from etreepublic cimport _ElementIterator, _ElementClassLookup
from etreepublic cimport elementFactory, import_etree, textOf
from python cimport isinstance, issubclass, callable, getattr, _cstr,
Py_ssize_t
cimport etreepublic as cetree
@@ -765,12 +766,28 @@
################################################################################
# Element class lookup
+cdef class ObjectifyElementClassLookup(_ElementClassLookup):
+ """Element class lookup method that uses the objectify classes.
+
+ The constructor accepts a keyword argument 'default_to_nsclasses'. You can
+ set it to False to divert from the default behaviour of looking up
+ namespace registered classes before trying to determine the right
+ objectify type class.
+ """
+ cdef int _default_to_nsclasses
+ def __init__(self, default_to_nsclasses=True):
+ self._lookup_function = _lookupElementClass
+ self._default_to_nsclasses = bool(default_to_nsclasses)
+
cdef object _lookupElementClass(state, _Document doc, tree.xmlNode* c_node):
cdef python.PyObject* dict_result
- # default to namespace specific classes
- nsclass = cetree.lookupNamespaceElementClass(state, doc, c_node)
- if nsclass is not ObjectifiedElement:
- return nsclass
+ if state is None or \
+ not isinstance(state, ObjectifyElementClassLookup) or \
+ (<ObjectifyElementClassLookup>state._default_to_nsclasses):
+ # default to namespace specific classes
+ nsclass = cetree.lookupNamespaceElementClass(state, doc, c_node)
+ if nsclass is not ObjectifiedElement:
+ return nsclass
# if element has children => no data class
if cetree.findChildForwards(c_node, 0):
|