Beispiel #1
0
    def __init__(self, xmlIfClass=XMLIF_MINIDOM,
                 elementWrapperClass=XsvXmlElementWrapper,
                 warningProc=IGNORE_WARNINGS, errorLimit=_XS_VAL_DEFAULT_ERROR_LIMIT, 
                 verbose=0, useCaching=1, processXInclude=1):

        self.warningProc    = warningProc
        self.errorLimit     = errorLimit
        self.verbose        = verbose

        # select XML interface class
        self.xmlIf = _xmlIfDict[xmlIfClass](verbose, useCaching, processXInclude)
        self.xmlIf.setElementWrapperClass (elementWrapperClass)

        # create error handler
        self.errorHandler  = ErrorHandler (errorLimit, warningProc, verbose)

        self.schemaDependancyList = []
Beispiel #2
0
    def __init__(self, xmlIfClass=XMLIF_MINIDOM,
                 elementWrapperClass=XsvXmlElementWrapper,
                 warningProc=IGNORE_WARNINGS, errorLimit=_XS_VAL_DEFAULT_ERROR_LIMIT, 
                 verbose=0, useCaching=1, processXInclude=1):

        self.warningProc    = warningProc
        self.errorLimit     = errorLimit
        self.verbose        = verbose

        # select XML interface class
        self.xmlIf = _xmlIfDict[xmlIfClass](verbose, useCaching, processXInclude)
        self.xmlIf.setElementWrapperClass (elementWrapperClass)

        # create error handler
        self.errorHandler  = ErrorHandler (errorLimit, warningProc, verbose)

        self.schemaDependancyList = []
Beispiel #3
0
class XsValidator:
    def __init__(self, xmlIfClass=XMLIF_MINIDOM,
                 elementWrapperClass=XsvXmlElementWrapper,
                 warningProc=IGNORE_WARNINGS, errorLimit=_XS_VAL_DEFAULT_ERROR_LIMIT, 
                 verbose=0, useCaching=1, processXInclude=1):

        self.warningProc    = warningProc
        self.errorLimit     = errorLimit
        self.verbose        = verbose

        # select XML interface class
        self.xmlIf = _xmlIfDict[xmlIfClass](verbose, useCaching, processXInclude)
        self.xmlIf.setElementWrapperClass (elementWrapperClass)

        # create error handler
        self.errorHandler  = ErrorHandler (errorLimit, warningProc, verbose)

        self.schemaDependancyList = []


    ########################################
    # retrieve current version
    #
    def getVersion (self):
        return __version__
        

    ########################################
    # parse XML file
    # 'file' may be a filepath or an URI
    #
    def parse (self, file, baseUrl="", ownerDoc=None):
        self._verbosePrint ("Parsing %s..." %(file))
        return self.xmlIf.parse(file, baseUrl, ownerDoc)


    ########################################
    # parse text string containing XML
    #
    def parseString (self, text, baseUrl=""):
        self._verbosePrint ("Parsing XML text string...")
        return self.xmlIf.parseString(text, baseUrl)


    ########################################
    # validate XML input
    #
    def validateXmlInput (self, xmlInputFile, inputTreeWrapper, xsdFile=None, validateSchema=0):
        # if the input file contains schema references => use these
        xsdTreeWrapperList = self._readReferencedXsdFiles(inputTreeWrapper, validateSchema)
        if xsdTreeWrapperList == []:
            # if the input file doesn't contain schema references => use given xsdFile
            if xsdFile != None:
                xsdTreeWrapper = self.parse (xsdFile)
                xsdTreeWrapperList.append(xsdTreeWrapper)
                # validate XML schema file if requested
                if validateSchema:
                    self.validateXmlSchema (xsdFile, xsdTreeWrapper)
            else:
                self.errorHandler.raiseError ("No schema file specified!")

        self._validateXmlInput (xmlInputFile, inputTreeWrapper, xsdTreeWrapperList)
        for xsdTreeWrapper in xsdTreeWrapperList:
            xsdTreeWrapper.unlink()
        return inputTreeWrapper

    ########################################
    # validate XML input
    #
    def validateXmlInputString (self, inputTreeWrapper, xsdText=None, validateSchema=0):
        # if the input file contains schema references => use these
        xsdTreeWrapperList = self._readReferencedXsdFiles(inputTreeWrapper, validateSchema)
        if xsdTreeWrapperList == []:
            # if the input file doesn't contain schema references => use given xsdText
            if xsdText != None:
                xsdFile = "schema text"
                xsdTreeWrapper = self.parseString (xsdText)
                xsdTreeWrapperList.append(xsdTreeWrapper)
                # validate XML schema file if requested
                if validateSchema:
                    self.validateXmlSchema (xsdFile, xsdTreeWrapper)
            else:
                self.errorHandler.raiseError ("No schema specified!")

        self._validateXmlInput ("input text", inputTreeWrapper, xsdTreeWrapperList)
        for xsdTreeWrapper in xsdTreeWrapperList:
            xsdTreeWrapper.unlink()
        return inputTreeWrapper


    ########################################
    # validate XML schema separately
    #
    def validateXmlSchema (self, xsdFile, xsdTreeWrapper):
        # parse minixsv internal schema
        global rulesTreeWrapper
        if rulesTreeWrapper == None:
            rulesTreeWrapper = self.parse(os.path.join (MINIXSV_DIR, "XMLSchema.xsd"))

        self._verbosePrint ("Validating %s..." %(xsdFile))
        xsvGivenXsdFile = XsValSchema (self.xmlIf, self.errorHandler, self.verbose)
        xsvGivenXsdFile.validate(xsdTreeWrapper, [rulesTreeWrapper,])
        self.schemaDependancyList.append (xsdFile)
        self.schemaDependancyList.extend (xsvGivenXsdFile.xsdIncludeDict.keys())
        xsvGivenXsdFile.unlink()
        self.errorHandler.flushOutput()
        return xsdTreeWrapper


    ########################################
    # validate XML input tree and xsd tree if requested
    #
    def _validateXmlInput (self, xmlInputFile, inputTreeWrapper, xsdTreeWrapperList):
        self._verbosePrint ("Validating %s..." %(xmlInputFile))
        xsvInputFile = XsValBase (self.xmlIf, self.errorHandler, self.verbose)
        xsvInputFile.validate(inputTreeWrapper, xsdTreeWrapperList)
        xsvInputFile.unlink()
        self.errorHandler.flushOutput()


    ########################################
    # retrieve XML schema location from XML input tree
    #
    def _readReferencedXsdFiles (self, inputTreeWrapper, validateSchema):
        xsdTreeWrapperList = []
        # a schemaLocation attribute is expected in the root tag of the XML input file
        xsdFileList = self._retrieveReferencedXsdFiles (inputTreeWrapper)
        for namespace, xsdFile in xsdFileList:
            try:
                xsdTreeWrapper = self.parse (xsdFile, inputTreeWrapper.getRootNode().getAbsUrl())
            except IOError, e:
                if e.errno == 2: # catch IOError: No such file or directory
                    self.errorHandler.raiseError ("XML schema file %s not found!" %(xsdFile), inputTreeWrapper.getRootNode())
                else:
                    raise IOError(e.errno, e.strerror, e.filename)

            xsdTreeWrapperList.append(xsdTreeWrapper)
            # validate XML schema file if requested
            if validateSchema:
                self.validateXmlSchema (xsdFile, xsdTreeWrapper)

            if namespace != xsdTreeWrapper.getRootNode().getAttributeOrDefault("targetNamespace", None):
                self.errorHandler.raiseError ("Namespace of 'schemaLocation' attribute doesn't match target namespace of %s!" %(xsdFile), inputTreeWrapper.getRootNode())
            
        return xsdTreeWrapperList
Beispiel #4
0
class XsValidator:
    def __init__(self,
                 xmlIfClass=XMLIF_MINIDOM,
                 elementWrapperClass=XsvXmlElementWrapper,
                 warningProc=IGNORE_WARNINGS,
                 errorLimit=_XS_VAL_DEFAULT_ERROR_LIMIT,
                 verbose=0,
                 useCaching=1,
                 processXInclude=1):

        self.warningProc = warningProc
        self.errorLimit = errorLimit
        self.verbose = verbose

        # select XML interface class
        self.xmlIf = _xmlIfDict[xmlIfClass](verbose, useCaching,
                                            processXInclude)
        self.xmlIf.setElementWrapperClass(elementWrapperClass)

        # create error handler
        self.errorHandler = ErrorHandler(errorLimit, warningProc, verbose)

        self.schemaDependancyList = []

    ########################################
    # retrieve current version
    #
    def getVersion(self):
        return __version__

    ########################################
    # parse XML file
    # 'file' may be a filepath or an URI
    #
    def parse(self, file, baseUrl="", ownerDoc=None):
        self._verbosePrint("Parsing %s..." % (file))
        return self.xmlIf.parse(file, baseUrl, ownerDoc)

    ########################################
    # parse text string containing XML
    #
    def parseString(self, text, baseUrl=""):
        self._verbosePrint("Parsing XML text string...")
        return self.xmlIf.parseString(text, baseUrl)

    ########################################
    # validate XML input
    #
    def validateXmlInput(self,
                         xmlInputFile,
                         inputTreeWrapper,
                         xsdFile=None,
                         validateSchema=0):
        # if the input file contains schema references => use these
        xsdTreeWrapperList = self._readReferencedXsdFiles(
            inputTreeWrapper, validateSchema)
        if xsdTreeWrapperList == []:
            # if the input file doesn't contain schema references => use given xsdFile
            if xsdFile != None:
                xsdTreeWrapper = self.parse(xsdFile)
                xsdTreeWrapperList.append(xsdTreeWrapper)
                # validate XML schema file if requested
                if validateSchema:
                    self.validateXmlSchema(xsdFile, xsdTreeWrapper)
            else:
                self.errorHandler.raiseError("No schema file specified!")

        self._validateXmlInput(xmlInputFile, inputTreeWrapper,
                               xsdTreeWrapperList)
        for xsdTreeWrapper in xsdTreeWrapperList:
            xsdTreeWrapper.unlink()
        return inputTreeWrapper

    ########################################
    # validate XML input
    #
    def validateXmlInputString(self,
                               inputTreeWrapper,
                               xsdText=None,
                               validateSchema=0):
        # if the input file contains schema references => use these
        xsdTreeWrapperList = self._readReferencedXsdFiles(
            inputTreeWrapper, validateSchema)
        if xsdTreeWrapperList == []:
            # if the input file doesn't contain schema references => use given xsdText
            if xsdText != None:
                xsdFile = "schema text"
                xsdTreeWrapper = self.parseString(xsdText)
                xsdTreeWrapperList.append(xsdTreeWrapper)
                # validate XML schema file if requested
                if validateSchema:
                    self.validateXmlSchema(xsdFile, xsdTreeWrapper)
            else:
                self.errorHandler.raiseError("No schema specified!")

        self._validateXmlInput("input text", inputTreeWrapper,
                               xsdTreeWrapperList)
        for xsdTreeWrapper in xsdTreeWrapperList:
            xsdTreeWrapper.unlink()
        return inputTreeWrapper

    ########################################
    # validate XML schema separately
    #
    def validateXmlSchema(self, xsdFile, xsdTreeWrapper):
        # parse minixsv internal schema
        global rulesTreeWrapper
        if rulesTreeWrapper == None:
            rulesTreeWrapper = self.parse(
                os.path.join(MINIXSV_DIR, "XMLSchema.xsd"))

        self._verbosePrint("Validating %s..." % (xsdFile))
        xsvGivenXsdFile = XsValSchema(self.xmlIf, self.errorHandler,
                                      self.verbose)
        xsvGivenXsdFile.validate(xsdTreeWrapper, [
            rulesTreeWrapper,
        ])
        self.schemaDependancyList.append(xsdFile)
        self.schemaDependancyList.extend(xsvGivenXsdFile.xsdIncludeDict.keys())
        xsvGivenXsdFile.unlink()
        self.errorHandler.flushOutput()
        return xsdTreeWrapper

    ########################################
    # validate XML input tree and xsd tree if requested
    #
    def _validateXmlInput(self, xmlInputFile, inputTreeWrapper,
                          xsdTreeWrapperList):
        self._verbosePrint("Validating %s..." % (xmlInputFile))
        xsvInputFile = XsValBase(self.xmlIf, self.errorHandler, self.verbose)
        xsvInputFile.validate(inputTreeWrapper, xsdTreeWrapperList)
        xsvInputFile.unlink()
        self.errorHandler.flushOutput()

    ########################################
    # retrieve XML schema location from XML input tree
    #
    def _readReferencedXsdFiles(self, inputTreeWrapper, validateSchema):
        xsdTreeWrapperList = []
        # a schemaLocation attribute is expected in the root tag of the XML input file
        xsdFileList = self._retrieveReferencedXsdFiles(inputTreeWrapper)
        for namespace, xsdFile in xsdFileList:
            try:
                xsdTreeWrapper = self.parse(
                    xsdFile,
                    inputTreeWrapper.getRootNode().getAbsUrl())
            except IOError, e:
                if e.errno == 2:  # catch IOError: No such file or directory
                    self.errorHandler.raiseError(
                        "XML schema file %s not found!" % (xsdFile),
                        inputTreeWrapper.getRootNode())
                else:
                    raise IOError(e.errno, e.strerror, e.filename)

            xsdTreeWrapperList.append(xsdTreeWrapper)
            # validate XML schema file if requested
            if validateSchema:
                self.validateXmlSchema(xsdFile, xsdTreeWrapper)

            if namespace != xsdTreeWrapper.getRootNode().getAttributeOrDefault(
                    "targetNamespace", None):
                self.errorHandler.raiseError(
                    "Namespace of 'schemaLocation' attribute doesn't match target namespace of %s!"
                    % (xsdFile), inputTreeWrapper.getRootNode())

        return xsdTreeWrapperList