logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

r45211 - in lxml/trunk: . src/lxml: msg#00114

Subject: r45211 - in lxml/trunk: . src/lxml
Author: scoder
Date: Fri Jul 20 11:37:35 2007
New Revision: 45211

Modified:
   lxml/trunk/TODO.txt
   lxml/trunk/src/lxml/config.pxd
   lxml/trunk/src/lxml/etree.pyx
   lxml/trunk/src/lxml/etree_defs.h
   lxml/trunk/src/lxml/schematron.pxi
Log:
integrate Schematron in a backwards compatible way

Modified: lxml/trunk/TODO.txt
==============================================================================
--- lxml/trunk/TODO.txt (original)
+++ lxml/trunk/TODO.txt Fri Jul 20 11:37:35 2007
@@ -51,8 +51,6 @@
 lxml 2.0
 ========
 
-* reformat error log lines, add column number
-
 * always use '<string>' as URL when tree was parsed from string? (can libxml2
   handle this?)
 
@@ -62,8 +60,6 @@
 
 * remove first 'context' argument from extension functions
 
-* find a way to integrate Schematron (if it's available)
-
 * always use ns-prefixed type names in objectify's ``xsi:type`` attributes
 
 * remove ``findOrBuildNodeNs()`` from C-API (replaced by
@@ -71,7 +67,7 @@
 
 * follow PEP 8 in API naming (avoidCamelCase in_favour_of_underscores)
 
-* clean support for entities (maybe an Entity element class?)
+* clean support for entities (is the Entity element class enough?)
 
 
 Changes in 2.0

Modified: lxml/trunk/src/lxml/config.pxd
==============================================================================
--- lxml/trunk/src/lxml/config.pxd      (original)
+++ lxml/trunk/src/lxml/config.pxd      Fri Jul 20 11:37:35 2007
@@ -1,2 +1,3 @@
 cdef extern from "etree_defs.h":
     cdef int ENABLE_THREADING
+    cdef int ENABLE_SCHEMATRON

Modified: lxml/trunk/src/lxml/etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/etree.pyx       (original)
+++ lxml/trunk/src/lxml/etree.pyx       Fri Jul 20 11:37:35 2007
@@ -2124,7 +2124,7 @@
 include "dtd.pxi"        # DTD
 include "relaxng.pxi"    # RelaxNG
 include "xmlschema.pxi"  # XMLSchema
-#include "schematron.pxi" # Schematron (requires libxml2 2.6.21+)
+include "schematron.pxi" # Schematron (requires libxml2 2.6.21+)
 
 
################################################################################
 # Public C API

Modified: lxml/trunk/src/lxml/etree_defs.h
==============================================================================
--- lxml/trunk/src/lxml/etree_defs.h    (original)
+++ lxml/trunk/src/lxml/etree_defs.h    Fri Jul 20 11:37:35 2007
@@ -7,65 +7,80 @@
 
 /* Py_ssize_t support was added in Python 2.5 */
 #if PY_VERSION_HEX < 0x02050000
-#ifndef PY_SSIZE_T_MAX /* patched Pyrex? */
-  typedef int Py_ssize_t;
-  #define PY_SSIZE_T_MAX INT_MAX
-  #define PY_SSIZE_T_MIN INT_MIN
-  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
-  #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
-#endif
+#  ifndef PY_SSIZE_T_MAX /* patched Pyrex? */
+     typedef int Py_ssize_t;
+#    define PY_SSIZE_T_MAX INT_MAX
+#    define PY_SSIZE_T_MIN INT_MIN
+#    define PyInt_FromSsize_t(z) PyInt_FromLong(z)
+#    define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
+#  endif
 #endif
 
 /* Threading can crash under Python <= 2.4.1 */
 #if PY_VERSION_HEX < 0x02040200
-#ifndef WITHOUT_THREADING
-  #define WITHOUT_THREADING
-#endif
+#  ifndef WITHOUT_THREADING
+#    define WITHOUT_THREADING
+#  endif
 #endif
 
 #ifdef WITHOUT_THREADING
-  #define PyEval_SaveThread() (NULL)
-  #define PyEval_RestoreThread(state)
-  #define PyGILState_Ensure() (PyGILState_UNLOCKED)
-  #define PyGILState_Release(state)
+#  define PyEval_SaveThread() (NULL)
+#  define PyEval_RestoreThread(state)
+#  define PyGILState_Ensure() (PyGILState_UNLOCKED)
+#  define PyGILState_Release(state)
 #endif
 
 #ifdef WITHOUT_THREADING
-  #define ENABLE_THREADING 0
+#  define ENABLE_THREADING 0
 #else
-  #define ENABLE_THREADING 1
+#  define ENABLE_THREADING 1
 #endif
 
 /* libxml2 version specific setup */
 #include "libxml/xmlversion.h"
 #if LIBXML_VERSION < 20621
 /* (X|HT)ML_PARSE_COMPACT were added in libxml2 2.6.21 */
-#define XML_PARSE_COMPACT  0
-#define HTML_PARSE_COMPACT 0
+#  define XML_PARSE_COMPACT  0
+#  define HTML_PARSE_COMPACT 0
 
 /* HTML_PARSE_RECOVER was added in libxml2 2.6.21 */
-#define HTML_PARSE_RECOVER XML_PARSE_RECOVER
+#  define HTML_PARSE_RECOVER XML_PARSE_RECOVER
 #endif
 
 /* added to xmlsave API in libxml2 2.6.23 */
 #if LIBXML_VERSION < 20623
-#define xmlSaveToBuffer(buffer, encoding, options)
+#  define xmlSaveToBuffer(buffer, encoding, options)
 #endif
 
 /* added to xmlsave API in libxml2 2.6.22 */
 #if LIBXML_VERSION < 20622
-#define XML_SAVE_NO_EMPTY   1<<2, /* no empty tags */
-#define XML_SAVE_NO_XHTML   1<<3  /* disable XHTML1 specific rules */
+#  define XML_SAVE_NO_EMPTY   1<<2, /* no empty tags */
+#  define XML_SAVE_NO_XHTML   1<<3  /* disable XHTML1 specific rules */
 #endif
 
 /* added to xmlsave API in libxml2 2.6.21 */
 #if LIBXML_VERSION < 20621
-#define XML_SAVE_NO_DECL    1<<1, /* drop the xml declaration */
+#  define XML_SAVE_NO_DECL    1<<1, /* drop the xml declaration */
 #endif
 
-/* added to xmlsave API in libxml2 2.6.17 */
-#if LIBXML_VERSION < 20617
-#define XML_SAVE_FORMAT    1<<0, /* format save output */
+/* schematron was added in libxml2 2.6.21 */
+#ifdef LIBXML_SCHEMATRON_ENABLED
+#  define ENABLE_SCHEMATRON 1
+#else
+#  define ENABLE_SCHEMATRON 0
+#  define XML_SCHEMATRON_OUT_QUIET 0
+#  define XML_SCHEMATRON_OUT_XML 0
+   typedef void xmlSchematron;
+   typedef void xmlSchematronParserCtxt;
+   typedef void xmlSchematronValidCtxt;
+#  define xmlSchematronNewDocParserCtxt(doc) NULL
+#  define xmlSchematronNewParserCtxt(file) NULL
+#  define xmlSchematronParse(ctxt) NULL
+#  define xmlSchematronFreeParserCtxt(ctxt)
+#  define xmlSchematronFree(schema)
+#  define xmlSchematronNewValidCtxt(schema, options) NULL
+#  define xmlSchematronValidateDoc(ctxt, doc) 0
+#  define xmlSchematronFreeValidCtxt(ctxt)
 #endif
 
 /* work around MSDEV 6.0 */

Modified: lxml/trunk/src/lxml/schematron.pxi
==============================================================================
--- lxml/trunk/src/lxml/schematron.pxi  (original)
+++ lxml/trunk/src/lxml/schematron.pxi  Fri Jul 20 11:37:35 2007
@@ -1,7 +1,7 @@
 # support for Schematron validation
 cimport schematron
 
-"""
+'''
 Schematron
 ----------
 
@@ -45,7 +45,7 @@
 however, Schematron lacks support for error reporting other than to stderr.
 It is therefore not possible to retrieve validation warnings and errors in
 lxml.
-"""
+'''
 
 class SchematronError(LxmlError):
     """Base class of all Schematron errors.
@@ -73,7 +73,6 @@
     the file system.
     """
     cdef schematron.xmlSchematron* _c_schema
-    cdef tree.xmlDoc* _c_doc
     def __init__(self, etree=None, file=None):
         cdef _Document doc
         cdef _Element root_node
@@ -81,13 +80,15 @@
         cdef xmlDoc* c_doc
         cdef char* c_href
         cdef schematron.xmlSchematronParserCtxt* parser_ctxt
+        if not config.ENABLE_SCHEMATRON:
+            raise SchematronError, \
+                  "lxml.etree was compiled without Schematron support."
         self._c_schema = NULL
-        self._c_doc = NULL
         if etree is not None:
             doc = _documentOrRaise(etree)
             root_node = _rootNodeOrRaise(etree)
-            self._c_doc = _copyDocRoot(doc._c_doc, root_node._c_node)
-            parser_ctxt = schematron.xmlSchematronNewDocParserCtxt(self._c_doc)
+            c_doc = _copyDocRoot(doc._c_doc, root_node._c_node)
+            parser_ctxt = schematron.xmlSchematronNewDocParserCtxt(c_doc)
         elif file is not None:
             filename = _getFilenameForFile(file)
             if filename is None:
@@ -95,26 +96,23 @@
                 filename = file
             filename = _encodeFilename(filename)
             parser_ctxt = 
schematron.xmlSchematronNewParserCtxt(_cstr(filename))
+            c_doc = NULL
         else:
             raise SchematronParseError, "No tree or file given"
 
         if parser_ctxt is NULL:
-            if self._c_doc is not NULL:
-                tree.xmlFreeDoc(self._c_doc)
+            if c_doc is not NULL:
+                tree.xmlFreeDoc(c_doc)
             raise SchematronParseError, "Document is not parsable as 
Schematron"
         self._c_schema = schematron.xmlSchematronParse(parser_ctxt)
 
+        schematron.xmlSchematronFreeParserCtxt(parser_ctxt)
         if self._c_schema is NULL:
-            if self._c_doc is not NULL:
-                schematron.xmlSchematronFreeParserCtxt(parser_ctxt)
-                tree.xmlFreeDoc(self._c_doc)
             raise SchematronParseError, "Document is not a valid Schematron 
schema"
-        schematron.xmlSchematronFreeParserCtxt(parser_ctxt)
         _Validator.__init__(self)
 
     def __dealloc__(self):
         schematron.xmlSchematronFree(self._c_schema)
-        tree.xmlFreeDoc(self._c_doc)
 
     def __call__(self, etree):
         """Validate doc using Schematron.
@@ -126,13 +124,18 @@
         cdef xmlDoc* c_doc
         cdef schematron.xmlSchematronValidCtxt* valid_ctxt
         cdef int ret
+        cdef int options
 
         doc = _documentOrRaise(etree)
         root_node = _rootNodeOrRaise(etree)
 
         self._error_log.connect()
+        options = schematron.XML_SCHEMATRON_OUT_QUIET
+        if tree.LIBXML_VERSION <= 20629:
+            # hack to switch off stderr output
+            options = options | schematron.XML_SCHEMATRON_OUT_XML
         valid_ctxt = schematron.xmlSchematronNewValidCtxt(
-            self._c_schema, schematron.XML_SCHEMATRON_OUT_QUIET)
+            self._c_schema, options)
         if valid_ctxt is NULL:
             self._error_log.disconnect()
             raise SchematronError, "Failed to create validation context"


<Prev in Thread] Current Thread [Next in Thread>