libxml2dom

Changeset

336:92d6d68675cb
2008-08-29 Paul Boddie raw files shortlog changelog graph Introduced stricter checking of unfinished documents. Introduced more generic testing and reporting of fatal errors in parsing.
libxml2dom/macrolib/macrolib.py (file)
     1.1 --- a/libxml2dom/macrolib/macrolib.py	Thu Aug 28 23:41:04 2008 +0200
     1.2 +++ b/libxml2dom/macrolib/macrolib.py	Fri Aug 29 00:44:46 2008 +0200
     1.3 @@ -601,28 +601,42 @@
     1.4      Parser_configure(context, validate, remote)
     1.5      Parser_parse(context)
     1.6      doc = Parser_document(context)
     1.7 +    error = Parser_error()
     1.8  
     1.9 -    if validate and not Parser_valid(context):
    1.10 +    try:
    1.11 +        if validate and not Parser_valid(context):
    1.12 +
    1.13 +            # NOTE: May not be the correct exception.
    1.14  
    1.15 -        # NOTE: May not be the correct exception.
    1.16 +            raise LSException(
    1.17 +                LSException.PARSE_ERR,
    1.18 +                DOMError(
    1.19 +                    DOMError.SEVERITY_FATAL_ERROR,
    1.20 +                    get_parse_error_message() or "Document did not validate"
    1.21 +                    ))
    1.22 +
    1.23 +        elif unfinished and (error is None or Parser_errorCode(error) == XML_ERR_TAG_NOT_FINISHED):
    1.24 +
    1.25 +            # NOTE: There may be other unfinished conditions.
    1.26  
    1.27 -        raise LSException(
    1.28 -            LSException.PARSE_ERR,
    1.29 -            DOMError(
    1.30 -                DOMError.SEVERITY_FATAL_ERROR,
    1.31 -                get_parse_error_message() or "Document did not validate"
    1.32 -                ))
    1.33 +            return doc
    1.34  
    1.35 -    elif unfinished or Parser_well_formed(context):
    1.36 -        return doc
    1.37 +        elif error is not None and Parser_errorLevel(error) == XML_ERR_FATAL:
    1.38 +            raise LSException(
    1.39 +                LSException.PARSE_ERR,
    1.40 +                DOMError(
    1.41 +                    DOMError.SEVERITY_FATAL_ERROR,
    1.42 +                    get_parse_error_message() or "Document caused fatal error"
    1.43 +                    ))
    1.44  
    1.45 -    else:
    1.46 -        raise LSException(
    1.47 -            LSException.PARSE_ERR,
    1.48 -            DOMError(
    1.49 -                DOMError.SEVERITY_FATAL_ERROR,
    1.50 -                get_parse_error_message() or "Document not well-formed"
    1.51 -                ))
    1.52 +        else:
    1.53 +
    1.54 +            # NOTE: Could provide non-fatal errors or warnings.
    1.55 +
    1.56 +            return doc
    1.57 +
    1.58 +    finally:
    1.59 +        Parser_resetError()
    1.60  
    1.61  def toString(node, encoding=None, prettyprint=0):
    1.62      return libxml2mod.serializeNode(node, encoding, prettyprint)
    1.63 @@ -643,6 +657,13 @@
    1.64  XML_PARSE_NOWARNING = 64
    1.65  XML_PARSE_NONET = 2048
    1.66  
    1.67 +XML_ERR_NONE = 0
    1.68 +XML_ERR_WARNING = 1
    1.69 +XML_ERR_ERROR = 2
    1.70 +XML_ERR_FATAL = 3
    1.71 +
    1.72 +XML_ERR_TAG_NOT_FINISHED = 77
    1.73 +
    1.74  def html_net_flag(remote):
    1.75      if remote:
    1.76          return 0
    1.77 @@ -662,7 +683,7 @@
    1.78          return 0
    1.79  
    1.80  def get_parse_error_message():
    1.81 -    error = libxml2mod.xmlGetLastError()
    1.82 +    error = Parser_error()
    1.83      if error is not None:
    1.84          filename = libxml2mod.xmlErrorGetFile(error)
    1.85          if filename is None:
    1.86 @@ -675,6 +696,18 @@
    1.87      else:
    1.88          return None
    1.89  
    1.90 +def Parser_error():
    1.91 +    return libxml2mod.xmlGetLastError()
    1.92 +
    1.93 +def Parser_resetError():
    1.94 +    return libxml2mod.xmlResetLastError()
    1.95 +
    1.96 +def Parser_errorLevel(error):
    1.97 +    return libxml2mod.xmlErrorGetLevel(error)
    1.98 +
    1.99 +def Parser_errorCode(error):
   1.100 +    return libxml2mod.xmlErrorGetCode(error)
   1.101 +
   1.102  def Parser_push():
   1.103      return libxml2mod.xmlCreatePushParser(None, "", 0, None)
   1.104