[Erp5-report] r25732 - /erp5/trunk/products/ERP5OOo/tests/utils.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Thu Feb 26 16:53:23 CET 2009
Author: jerome
Date: Thu Feb 26 16:53:22 2009
New Revision: 25732
URL: http://svn.erp5.org?rev=25732&view=rev
Log:
rewrite validator using lxml instead of libxml2
Modified:
erp5/trunk/products/ERP5OOo/tests/utils.py
Modified: erp5/trunk/products/ERP5OOo/tests/utils.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/utils.py?rev=25732&r1=25731&r2=25732&view=diff
==============================================================================
--- erp5/trunk/products/ERP5OOo/tests/utils.py [utf8] (original)
+++ erp5/trunk/products/ERP5OOo/tests/utils.py [utf8] Thu Feb 26 16:53:22 2009
@@ -42,32 +42,17 @@
from cStringIO import StringIO
try:
- import libxml2
+ import lxml
except ImportError:
- libxml2 = None
+ lxml = None
try:
import odfpy
except ImportError:
odfpy = None
-
-if libxml2:
-
- class ErrorHandler:
- """Collect errors"""
- def __init__(self, file_name):
- libxml2.lineNumbersDefault(1)
- self.file_name = file_name
- self.error_list = []
-
- def onError(self, msg, data):
- line = libxml2.lastError().line()
- self.error_list.append('%s:%s: %s' % (self.file_name, line, msg))
- onWarning = onError
-
-
- class LibXML2Validator:
- """Validate ODF document using RelaxNG and libxml2"""
+if lxml:
+ class LXMLValidator:
+ """Validate ODF document using RelaxNG and lxml"""
schema_url = \
'http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-schema-v1.1.rng'
@@ -78,8 +63,7 @@
# download if local copy does not exists
if not os.path.exists(self.schema_path):
self._download()
- ctxt = libxml2.relaxNGNewParserCtxt(self.schema_path)
- self.relaxng = ctxt.relaxNGParse()
+ self.relaxng = lxml.etree.RelaxNG(lxml.etree.parse(open(self.schema_path)))
def validate(self, odf_file_content):
error_list = []
@@ -98,20 +82,12 @@
r.close()
def _validateXML(self, odf_file, content_file_name):
- validationCtxt = self.relaxng.relaxNGNewValidCtxt()
- err = ErrorHandler(content_file_name)
+ zfd = zipfile.ZipFile(odf_file)
+ doc = lxml.etree.parse(StringIO(zfd.read(content_file_name)))
+ self.relaxng.validate(doc)
+ return [error for error in str(self.relaxng.error_log).splitlines(True)]
- validationCtxt.setValidityErrorHandler(
- err.onError, err.onWarning)
-
- zfd = zipfile.ZipFile(odf_file)
- content = zfd.read(content_file_name)
-
- validationCtxt.relaxNGValidateDoc(
- libxml2.parseMemory(content, len(content)))
- return err.error_list
-
- Validator = LibXML2Validator
+ Validator = LXMLValidator
elif odfpy:
More information about the Erp5-report
mailing list