etisserant@0: # etisserant@0: # minixsv, Release 0.3 etisserant@0: # file: xsvalErrorHandler.py etisserant@0: # etisserant@0: # XML schema validator classes etisserant@0: # etisserant@0: # history: etisserant@0: # 2004-09-23 rl created etisserant@0: # etisserant@0: # Copyright (c) 2004 by Roland Leuthe. All rights reserved. etisserant@0: # etisserant@0: # -------------------------------------------------------------------- etisserant@0: # The minixsv XML schema validator is etisserant@0: # etisserant@0: # Copyright (c) 2004 by Roland Leuthe etisserant@0: # etisserant@0: # By obtaining, using, and/or copying this software and/or its etisserant@0: # associated documentation, you agree that you have read, understood, etisserant@0: # and will comply with the following terms and conditions: etisserant@0: # etisserant@0: # Permission to use, copy, modify, and distribute this software and etisserant@0: # its associated documentation for any purpose and without fee is etisserant@0: # hereby granted, provided that the above copyright notice appears in etisserant@0: # all copies, and that both that copyright notice and this permission etisserant@0: # notice appear in supporting documentation, and that the name of etisserant@0: # the author not be used in advertising or publicity etisserant@0: # pertaining to distribution of the software without specific, written etisserant@0: # prior permission. etisserant@0: # etisserant@0: # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD etisserant@0: # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- etisserant@0: # ABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR etisserant@0: # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY etisserant@0: # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, etisserant@0: # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS etisserant@0: # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE etisserant@0: # OF THIS SOFTWARE. etisserant@0: # -------------------------------------------------------------------- etisserant@0: etisserant@0: import string etisserant@0: import os etisserant@0: etisserant@0: IGNORE_WARNINGS = 0 etisserant@0: PRINT_WARNINGS = 1 etisserant@0: STOP_ON_WARNINGS = 2 etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # Error-Handler class for XML schema validator etisserant@0: # handles only validator errors, no parser errors! etisserant@0: etisserant@0: class ErrorHandler: etisserant@0: etisserant@0: def __init__(self, errorLimit, warningProc, verbose): etisserant@0: self.errorLimit = errorLimit etisserant@0: self.warningProc = warningProc etisserant@0: self.verbose = verbose etisserant@0: etisserant@0: self.errorList = [] etisserant@0: self.noOfErrors = 0 etisserant@0: self.warningList = [] etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # add error to errorList (raise exception only if error limit is reached) etisserant@0: etisserant@0: def addError (self, errstr, element=None, endTag=0): etisserant@0: filePath = "" etisserant@0: lineNo = 0 etisserant@0: if element: etisserant@0: filePath = element.getFilePath() etisserant@0: if endTag: etisserant@0: lineNo = element.getEndLineNumber() etisserant@0: else: etisserant@0: lineNo = element.getStartLineNumber() etisserant@0: self.errorList.append ((filePath, lineNo, "ERROR", "%s" %(errstr))) etisserant@0: self.noOfErrors += 1 etisserant@0: if self.noOfErrors == self.errorLimit: etisserant@0: self._raiseXsvalException ("\nError Limit reached!!") etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # add warning to warningList etisserant@0: etisserant@0: def addWarning (self, warnstr, element=None): etisserant@0: filePath = "" etisserant@0: lineNo = 0 etisserant@0: if element: etisserant@0: filePath = element.getFilePath() etisserant@0: lineNo = element.getStartLineNumber() etisserant@0: self.warningList.append ((filePath, lineNo, "WARNING", warnstr)) etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # add info string to errorList etisserant@0: etisserant@0: def addInfo (self, infostr, element=None): etisserant@0: filePath = "" etisserant@0: lineNo = 0 etisserant@0: if element: etisserant@0: filePath = element.getFilePath() etisserant@0: lineNo = element.getStartLineNumber() etisserant@0: self.errorList.append ((filePath, lineNo, "INFO", infostr)) etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # add error to errorList (if given) and raise exception etisserant@0: etisserant@0: def raiseError (self, errstr, element=None): etisserant@0: self.addError (errstr, element) etisserant@0: self._raiseXsvalException () etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # raise exception with complete errorList as exception string etisserant@0: # (only if errors occurred) etisserant@0: etisserant@0: def flushOutput (self): etisserant@0: if self.warningProc == PRINT_WARNINGS and self.warningList != []: etisserant@0: print self._assembleSortedOutputList(self.warningList) etisserant@0: self.warningList = [] etisserant@0: elif self.warningProc == STOP_ON_WARNINGS: etisserant@0: self.errorList.extend (self.warningList) etisserant@0: etisserant@0: if self.errorList != []: etisserant@0: self._raiseXsvalException () etisserant@0: etisserant@0: etisserant@0: ######################################## etisserant@0: # Private methods etisserant@0: etisserant@0: def _raiseXsvalException (self, additionalInfo=""): etisserant@0: output = self._assembleSortedOutputList(self.errorList) + additionalInfo etisserant@0: self.errorList = self.warningList = [] etisserant@0: raise XsvalError (output) etisserant@0: etisserant@0: etisserant@0: def _assembleSortedOutputList (self, outputList): etisserant@0: outputList.sort() etisserant@0: outputStrList = [] etisserant@0: for outElement in outputList: etisserant@0: outputStrList.append (self._assembleOutString(outElement)) etisserant@0: return string.join (outputStrList, "\n") etisserant@0: etisserant@0: etisserant@0: def _assembleOutString (self, listElement): etisserant@0: fileStr = "" etisserant@0: lineStr = "" etisserant@0: if listElement[0] != "": etisserant@0: if self.verbose: etisserant@0: fileStr = "%s: " %(listElement[0]) etisserant@0: else: etisserant@0: fileStr = "%s: " %(os.path.basename(listElement[0])) etisserant@0: if listElement[1] != 0: etisserant@0: lineStr = "line %d: " %(listElement[1]) etisserant@0: return "%s: %s%s%s" %(listElement[2], fileStr, lineStr, listElement[3]) etisserant@0: etisserant@0: etisserant@0: class XsvalError (StandardError): etisserant@0: pass etisserant@0: