commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r10177 - gnuradio/trunk/grc/src/utils


From: jblum
Subject: [Commit-gnuradio] r10177 - gnuradio/trunk/grc/src/utils
Date: Mon, 29 Dec 2008 17:51:56 -0700 (MST)

Author: jblum
Date: 2008-12-29 17:51:54 -0700 (Mon, 29 Dec 2008)
New Revision: 10177

Modified:
   gnuradio/trunk/grc/src/utils/ParseXML.py
Log:
error handling for dtd validation

Modified: gnuradio/trunk/grc/src/utils/ParseXML.py
===================================================================
--- gnuradio/trunk/grc/src/utils/ParseXML.py    2008-12-29 01:35:48 UTC (rev 
10176)
+++ gnuradio/trunk/grc/src/utils/ParseXML.py    2008-12-30 00:51:54 UTC (rev 
10177)
@@ -20,7 +20,11 @@
 from lxml import etree
 from .. utils import odict
 
-XMLSyntaxError = etree.XMLSyntaxError
+class XMLSyntaxError(Exception):
+       def __init__(self, error_log):
+               self._error_log = error_log
+       def __str__(self):
+               return '\n'.join(map(str, self._error_log.filter_from_errors()))
 
 def validate_dtd(xml_file, dtd_file=None):
        """
@@ -29,16 +33,14 @@
        @param dtd_file the optional dtd file
        @throws Exception validation fails
        """
-       if dtd_file:
-               dtd = etree.DTD(dtd_file)
-               xml = etree.parse(xml_file)
-               if not dtd.validate(xml.getroot()):
-                       raise XMLSyntaxError, '\n'.join(map(str, 
dtd.error_log.filter_from_errors()))
-       else:
-               parser = etree.XMLParser(dtd_validation=True)
-               xml = etree.parse(xml_file, parser=parser)
-               if parser.error_log:
-                       raise XMLSyntaxError, '\n'.join(map(str, 
parser.error_log.filter_from_errors()))
+       #perform parsing, use dtd validation if dtd file is not specified
+       parser = etree.XMLParser(dtd_validation=not dtd_file)
+       xml = etree.parse(xml_file, parser=parser)
+       if parser.error_log: raise XMLSyntaxError(parser.error_log)
+       #perform dtd validation if the dtd file is specified
+       if not dtd_file: return
+       dtd = etree.DTD(dtd_file)
+       if not dtd.validate(xml.getroot()): raise XMLSyntaxError(dtd.error_log)
 
 def from_file(xml_file):
        """





reply via email to

[Prev in Thread] Current Thread [Next in Thread]