minixsv/xsvalErrorHandler.py
changeset 0 b622defdfd98
equal deleted inserted replaced
-1:000000000000 0:b622defdfd98
       
     1 #
       
     2 # minixsv, Release 0.3
       
     3 # file: xsvalErrorHandler.py
       
     4 #
       
     5 # XML schema validator classes
       
     6 #
       
     7 # history:
       
     8 # 2004-09-23 rl   created
       
     9 #
       
    10 # Copyright (c) 2004 by Roland Leuthe.  All rights reserved.
       
    11 #
       
    12 # --------------------------------------------------------------------
       
    13 # The minixsv XML schema validator is
       
    14 #
       
    15 # Copyright (c) 2004 by Roland Leuthe
       
    16 #
       
    17 # By obtaining, using, and/or copying this software and/or its
       
    18 # associated documentation, you agree that you have read, understood,
       
    19 # and will comply with the following terms and conditions:
       
    20 #
       
    21 # Permission to use, copy, modify, and distribute this software and
       
    22 # its associated documentation for any purpose and without fee is
       
    23 # hereby granted, provided that the above copyright notice appears in
       
    24 # all copies, and that both that copyright notice and this permission
       
    25 # notice appear in supporting documentation, and that the name of
       
    26 # the author not be used in advertising or publicity
       
    27 # pertaining to distribution of the software without specific, written
       
    28 # prior permission.
       
    29 #
       
    30 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
       
    31 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
       
    32 # ABILITY AND FITNESS.  IN NO EVENT SHALL THE AUTHOR
       
    33 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
       
    34 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
       
    35 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
       
    36 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
       
    37 # OF THIS SOFTWARE.
       
    38 # --------------------------------------------------------------------
       
    39 
       
    40 import string
       
    41 import os
       
    42 
       
    43 IGNORE_WARNINGS   = 0
       
    44 PRINT_WARNINGS    = 1
       
    45 STOP_ON_WARNINGS  = 2
       
    46 
       
    47 
       
    48 ########################################
       
    49 # Error-Handler class for XML schema validator
       
    50 # handles only validator errors, no parser errors!
       
    51 
       
    52 class ErrorHandler:
       
    53 
       
    54     def __init__(self, errorLimit, warningProc, verbose):
       
    55         self.errorLimit  = errorLimit
       
    56         self.warningProc = warningProc
       
    57         self.verbose     = verbose
       
    58         
       
    59         self.errorList = []
       
    60         self.noOfErrors = 0
       
    61         self.warningList = []
       
    62 
       
    63         
       
    64     ########################################
       
    65     # add error to errorList (raise exception only if error limit is reached)
       
    66 
       
    67     def addError (self, errstr, element=None, endTag=0):
       
    68         filePath = ""
       
    69         lineNo = 0
       
    70         if element:
       
    71             filePath = element.getFilePath()
       
    72             if endTag:
       
    73                 lineNo = element.getEndLineNumber()
       
    74             else:
       
    75                 lineNo = element.getStartLineNumber()
       
    76         self.errorList.append ((filePath, lineNo, "ERROR", "%s" %(errstr)))
       
    77         self.noOfErrors += 1
       
    78         if self.noOfErrors == self.errorLimit:
       
    79             self._raiseXsvalException ("\nError Limit reached!!")
       
    80 
       
    81 
       
    82     ########################################
       
    83     # add warning to warningList
       
    84 
       
    85     def addWarning (self, warnstr, element=None):
       
    86         filePath = ""
       
    87         lineNo = 0
       
    88         if element:
       
    89             filePath = element.getFilePath()
       
    90             lineNo = element.getStartLineNumber()
       
    91         self.warningList.append ((filePath, lineNo, "WARNING", warnstr))
       
    92 
       
    93 
       
    94     ########################################
       
    95     # add info string to errorList
       
    96 
       
    97     def addInfo (self, infostr, element=None):
       
    98         filePath = ""
       
    99         lineNo = 0
       
   100         if element:
       
   101             filePath = element.getFilePath()
       
   102             lineNo = element.getStartLineNumber()
       
   103         self.errorList.append ((filePath, lineNo, "INFO", infostr))
       
   104 
       
   105 
       
   106     ########################################
       
   107     # add error to errorList (if given) and raise exception
       
   108 
       
   109     def raiseError (self, errstr, element=None):
       
   110         self.addError (errstr, element)
       
   111         self._raiseXsvalException ()
       
   112 
       
   113 
       
   114     ########################################
       
   115     # raise exception with complete errorList as exception string
       
   116     # (only if errors occurred)
       
   117 
       
   118     def flushOutput (self):
       
   119         if self.warningProc == PRINT_WARNINGS and self.warningList != []:
       
   120             print self._assembleSortedOutputList(self.warningList)
       
   121             self.warningList = []
       
   122         elif self.warningProc == STOP_ON_WARNINGS:
       
   123             self.errorList.extend (self.warningList)
       
   124 
       
   125         if self.errorList != []:
       
   126             self._raiseXsvalException ()
       
   127 
       
   128 
       
   129     ########################################
       
   130     # Private methods
       
   131 
       
   132     def _raiseXsvalException (self, additionalInfo=""):
       
   133         output = self._assembleSortedOutputList(self.errorList) + additionalInfo
       
   134         self.errorList = self.warningList = []
       
   135         raise XsvalError (output)
       
   136 
       
   137 
       
   138     def _assembleSortedOutputList (self, outputList):
       
   139         outputList.sort()
       
   140         outputStrList = []
       
   141         for outElement in outputList:
       
   142             outputStrList.append (self._assembleOutString(outElement))
       
   143         return string.join (outputStrList, "\n")
       
   144         
       
   145         
       
   146     def _assembleOutString (self, listElement):
       
   147         fileStr = ""
       
   148         lineStr = ""
       
   149         if listElement[0] != "":
       
   150             if self.verbose:
       
   151                 fileStr = "%s: " %(listElement[0])
       
   152             else:
       
   153                 fileStr = "%s: " %(os.path.basename(listElement[0]))
       
   154         if listElement[1] != 0:
       
   155             lineStr = "line %d: " %(listElement[1])
       
   156         return "%s: %s%s%s" %(listElement[2], fileStr, lineStr, listElement[3])
       
   157     
       
   158 
       
   159 class XsvalError (StandardError):
       
   160     pass
       
   161