xsvalErrorHandler.py
Go to the documentation of this file.
00001 #
00002 # minixsv, Release 0.9.0
00003 # file: xsvalErrorHandler.py
00004 #
00005 # XML schema validator classes
00006 #
00007 # history:
00008 # 2004-09-23 rl   created
00009 #
00010 # Copyright (c) 2004-2008 by Roland Leuthe.  All rights reserved.
00011 #
00012 # --------------------------------------------------------------------
00013 # The minixsv XML schema validator is
00014 #
00015 # Copyright (c) 2004-2008 by Roland Leuthe
00016 #
00017 # By obtaining, using, and/or copying this software and/or its
00018 # associated documentation, you agree that you have read, understood,
00019 # and will comply with the following terms and conditions:
00020 #
00021 # Permission to use, copy, modify, and distribute this software and
00022 # its associated documentation for any purpose and without fee is
00023 # hereby granted, provided that the above copyright notice appears in
00024 # all copies, and that both that copyright notice and this permission
00025 # notice appear in supporting documentation, and that the name of
00026 # the author not be used in advertising or publicity
00027 # pertaining to distribution of the software without specific, written
00028 # prior permission.
00029 #
00030 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
00031 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
00032 # ABILITY AND FITNESS.  IN NO EVENT SHALL THE AUTHOR
00033 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
00034 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00035 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00036 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
00037 # OF THIS SOFTWARE.
00038 # --------------------------------------------------------------------
00039 
00040 import string
00041 import os
00042 
00043 IGNORE_WARNINGS   = 0
00044 PRINT_WARNINGS    = 1
00045 STOP_ON_WARNINGS  = 2
00046 
00047 
00048 ########################################
00049 # Error-Handler class for XML schema validator
00050 # handles only validator errors, no parser errors!
00051 
00052 class ErrorHandler:
00053 
00054     def __init__(self, errorLimit, warningProc, verbose):
00055         self.errorLimit  = errorLimit
00056         self.warningProc = warningProc
00057         self.verbose     = verbose
00058         
00059         self.errorList = []
00060         self.noOfErrors = 0
00061         self.warningList = []
00062         self.infoDict = {}
00063 
00064 
00065     ########################################
00066     # check if errors have already been reported
00067 
00068     def hasErrors (self):
00069         return self.errorList != []
00070 
00071     ########################################
00072     # add error to errorList (raise exception only if error limit is reached)
00073 
00074     def addError (self, errstr, element=None, endTag=0):
00075         filePath = ""
00076         lineNo = 0
00077         if element:
00078             filePath = element.getFilePath()
00079             if endTag:
00080                 lineNo = element.getEndLineNumber()
00081             else:
00082                 lineNo = element.getStartLineNumber()
00083         self.errorList.append ((filePath, lineNo, "ERROR", "%s" %errstr))
00084         self.noOfErrors += 1
00085         if self.noOfErrors == self.errorLimit:
00086             self._raiseXsvalException ("\nError Limit reached!!")
00087 
00088 
00089     ########################################
00090     # add warning to warningList
00091 
00092     def addWarning (self, warnstr, element=None):
00093         filePath = ""
00094         lineNo = 0
00095         if element:
00096             filePath = element.getFilePath()
00097             lineNo = element.getStartLineNumber()
00098         self.warningList.append ((filePath, lineNo, "WARNING", warnstr))
00099 
00100 
00101     ########################################
00102     # add info string to errorList
00103 
00104     def addInfo (self, infostr, element=None):
00105         self.infoDict.setdefault("INFO: %s" %infostr, 1)
00106 
00107 
00108     ########################################
00109     # add error to errorList (if given) and raise exception
00110 
00111     def raiseError (self, errstr, element=None):
00112         self.addError (errstr, element)
00113         self._raiseXsvalException ()
00114 
00115 
00116     ########################################
00117     # raise exception with complete errorList as exception string
00118     # (only if errors occurred)
00119 
00120     def flushOutput (self):
00121         if self.infoDict != {}:
00122             print string.join (self.infoDict.keys(), "\n")
00123             self.infoList = []
00124 
00125         if self.warningProc == PRINT_WARNINGS and self.warningList != []:
00126             print self._assembleOutputList(self.warningList, sorted=1)
00127             self.warningList = []
00128         elif self.warningProc == STOP_ON_WARNINGS:
00129             self.errorList.extend (self.warningList)
00130 
00131         if self.errorList != []:
00132             self._raiseXsvalException ()
00133 
00134 
00135     ########################################
00136     # Private methods
00137 
00138     def _raiseXsvalException (self, additionalInfo=""):
00139         output = self._assembleOutputList(self.errorList) + additionalInfo
00140         self.errorList = self.warningList = []
00141         raise XsvalError (output)
00142 
00143 
00144     def _assembleOutputList (self, outputList, sorted=0):
00145         if sorted:
00146             outputList.sort()
00147         outputStrList = []
00148         for outElement in outputList:
00149             outputStrList.append (self._assembleOutString(outElement))
00150         return string.join (outputStrList, "\n")
00151         
00152         
00153     def _assembleOutString (self, listElement):
00154         fileStr = ""
00155         lineStr = ""
00156         if listElement[0] != "":
00157             if self.verbose:
00158                 fileStr = "%s: " %(listElement[0])
00159             else:
00160                 fileStr = "%s: " %(os.path.basename(listElement[0]))
00161         if listElement[1] != 0:
00162             lineStr = "line %d: " %(listElement[1])
00163         return "%s: %s%s%s" %(listElement[2], fileStr, lineStr, listElement[3])
00164     
00165 
00166 class XsvalError (StandardError):
00167     pass
00168 


mavlink
Author(s): Lorenz Meier
autogenerated on Sun May 22 2016 04:05:43