Next / Previous / Contents / Shipman's homepage

16.7. rnck: validateFile()

rnck
# - - -   v a l i d a t e F i l e

def validateFile(validator, fileName):
    '''Validate one file against the schema.

      [ validator is an rnc_validate.RelaxValidator instance ->
          if fileName is readable and valid against validator ->
            I
          else ->
            sys.stderr  +:=  error message ]
    '''
    #-- 1 --
    # [ if fileName names a readable, well-formed XML file ->
    #     doc  :=  an et.ElementTree instance representing that file
    #   else ->
    #     sys.stderr  +:=  error message
    #     return ]
    try:
        doc = et.parse(fileName)
    except et.XMLSyntaxError, details:
        message("*** File '%s' not well-formed: %s" %
                 (fileName, str(details)))
        return
    except IOError, details:
        message("*** Can't read file '%s': %s" %
                  (fileName, str(details)))
        return
                  
    #-- 2 --
    # [ if doc is valid against validator ->
    #     I
    #   else ->
    #     sys.stdout  +:=  failure report ]
    try:
        validator.validate(doc)
    except rnc_validate.RelaxException, details:
        message("*** File '%s' is not valid:\n%s" %
                  (fileName, details))