示例#1
0
def convert_xml_import(cr, module, xmlfile, idref=None, mode='init', noupdate=False, report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'],'import_xml.rng' )))
    try:
        relaxng.assert_(doc)
    except Exception:
        _logger.info("The XML file '%s' does not fit the required schema !", xmlfile.name, exc_info=True)
        _logger.info(ustr(relaxng.error_log.last_error))
        raise

    if idref is None:
        idref={}
    if isinstance(xmlfile, pycompat.string_types):
        xml_filename = xmlfile
    else:
        xml_filename = xmlfile.name
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate, xml_filename=xml_filename)
    obj.parse(doc.getroot(), mode=mode)
    return True
示例#2
0
 def test_xml_files(self):
     "Test validity of the xml files of the module"
     config = ConfigParser()
     with file_open('%s/tryton.cfg' % self.module,
                    subdir='modules',
                    mode='r',
                    encoding='utf-8') as fp:
         config.read_file(fp)
     if not config.has_option('tryton', 'xml'):
         return
     with file_open('tryton.rng', subdir='', mode='rb') as fp:
         rng = etree.parse(fp)
     validator = etree.RelaxNG(etree=rng)
     for xml_file in filter(None, config.get('tryton', 'xml').splitlines()):
         with self.subTest(xml=xml_file):
             with file_open('%s/%s' % (self.module, xml_file),
                            subdir='modules',
                            mode='rb') as fp:
                 tree = etree.parse(fp)
             validator.assertValid(tree)
def xml_validate(parent, xmlns, xpath=None, rng=None, sch=None):
    """Validate xml by rng and sch"""

    if xpath:

        # rng rules
        relaxng = None
        if rng:
            rng_node = etree.XML(rng)
            relaxng = etree.RelaxNG(rng_node)

        # schematron rules
        schematron = None
        if sch:
            sch_node = etree.XML(sch)
            schematron = isoschematron.Schematron(sch_node)

        # run validation selected by xpath nodes
        for node in parent.xpath(xpath, namespaces=xmlns):
            _xml_validate_node(node, relaxng, schematron)
示例#4
0
def schema_graph(arch):
    """ Check the graph view against its schema

    :type arch: etree._Element
    """
    global _graph_validator

    _graph_validator = view_validation._validators

    if _graph_validator is None:
        with misc.file_open(
                os.path.join('deltatech_graph', 'views',
                             'graph_view.rng')) as f:
            _grid_validator = etree.RelaxNG(etree.parse(f))

    if _graph_validator.validate(arch):
        return True

    for error in _graph_validator.error_log:
        _logger.error(ustr(error))
    return False
示例#5
0
文件: xml.py 项目: basaks/qmlutil
def validate(f, schema="BED"):
    """
    Validate a QuakeML file using a given RelaxNG schema
    """
    from lxml import etree

    # Load schema
    schemafile = VALIDATION_SCHEMAS[schema]
    schema = etree.parse(schemafile)
    rng = etree.RelaxNG(schema)
    
    # Load QuakeML
    if isinstance(f, file):
        qml = etree.parse(f)
    elif isinstance(f, unicode):
        qml = etree.fromstring(f.encode())
    elif isinstance(f, str):
        qml = etree.fromstring(f)

    is_valid = rng.validate(qml)
    return is_valid
示例#6
0
def convert_xml_import(cr,
                       module,
                       xmlfile,
                       idref=None,
                       mode='init',
                       noupdate=False,
                       report=None):
    doc = etree.parse(xmlfile)
    schema = os.path.join(config['root_path'], 'import_xml.rng')
    relaxng = etree.RelaxNG(etree.parse(schema))
    try:
        relaxng.assert_(doc)
    except Exception:
        _logger.exception(
            "The XML file '%s' does not fit the required schema !",
            xmlfile.name)
        if jingtrang:
            p = subprocess.run(['pyjing', schema, xmlfile.name],
                               stdout=subprocess.PIPE)
            _logger.warn(p.stdout.decode())
        else:
            for e in relaxng.error_log:
                _logger.warn(e)
            _logger.info(
                "Install 'jingtrang' for more precise and useful validation messages."
            )
        raise

    if isinstance(xmlfile, str):
        xml_filename = xmlfile
    else:
        xml_filename = xmlfile.name
    obj = xml_import(cr,
                     module,
                     idref,
                     mode,
                     report=report,
                     noupdate=noupdate,
                     xml_filename=xml_filename)
    obj.parse(doc.getroot())
示例#7
0
    def validate_xml(self, dict_file, parsed_xml_tree, validator_type, validator_name):
        # Check that validator is valid
        if (
            not validator_type in self.Config
            or not validator_name in self.Config[validator_type]
        ):
            msg = (
                "XML Validator type "
                + validator_type
                + " not found in ConfigManager instance"
            )
            raise FprimeXmlException(msg)

        # Create proper xml validator tool
        validator_file_handler = open(
            ROOTDIR + self.Config.get(validator_type, validator_name), "r"
        )
        validator_parsed = etree.parse(validator_file_handler)
        validator_file_handler.close()
        if validator_type == "schema":
            validator_compiled = etree.RelaxNG(validator_parsed)
        elif validator_type == "schematron":
            validator_compiled = isoschematron.Schematron(validator_parsed)

        # Validate XML file
        if not validator_compiled.validate(parsed_xml_tree):
            if validator_type == "schema":
                msg = "XML file {} is not valid according to {} {}.".format(
                    dict_file,
                    validator_type,
                    ROOTDIR + self.Config.get(validator_type, validator_name),
                )
                raise FprimeXmlException(msg)
            elif validator_type == "schematron":
                msg = "WARNING: XML file {} is not valid according to {} {}.".format(
                    dict_file,
                    validator_type,
                    ROOTDIR + self.Config.get(validator_type, validator_name),
                )
                PRINT.info(msg)
示例#8
0
def validate_RNG(teipath, rngfile, logfile):
	"""
	Arguments:
	teipath (str): path to the TEI files, e.g. /home/ulrike/Git/conha19/tei/*.xml
	rngfile (str): path to the RELAX NG schema file, e.g. /home/ulrike/Git/reference/tei/cligs.rng
	logfile (str): path to an output text file, in which the results of the validation are stored, e.g. /home/ulrike/Git/conha19/schema/log-rng.txt
	
	Example call:
    from corpus.metadata_encoding import validate_tei
    validate_tei.validate_RNG("/home/ulrike/Git/conha19/tei/*.xml", "/home/ulrike/Git/reference/tei/cligs.rng", "/home/ulrike/Git/conha19/schema/log-rng.txt")
	"""
	print("starting validation...")
	
	# delete old log, if present
	if os.path.isfile(logfile):
		os.remove(logfile)
		print("old logfile removed")
	
	for teifile in glob.glob(teipath): 
		
		idno = os.path.basename(teifile)
		print("doing " + idno + "...")
        
		parser = etree.XMLParser(recover=True)
		teiparsed = etree.parse(teifile, parser)

		# RelaxNG validation
		rngparsed = etree.parse(rngfile)
		rngvalidator = etree.RelaxNG(rngparsed)
		validation_rng = rngvalidator.validate(teiparsed)
		log_rng = rngvalidator.error_log
		
		with open(logfile, "a", encoding="UTF-8") as outfile:
			if validation_rng == True: 
				outfile.write(idno + ": valid with RNG\n")
			else:
				outfile.write(idno + ": not valid with RNG\n")
				outfile.write(str(log_rng) + "\n")
	print("Done!")
示例#9
0
 def import_help(self):
     for this in self:
         xmlfile = StringIO(base64.decodestring(this.source_file))
         doc = etree.parse(xmlfile)
         relaxng = etree.RelaxNG(
             etree.parse(os.path.join(config['root_path'],
                                      'import_xml.rng')))
         try:
             relaxng.assert_(doc)
         except Exception:
             _logger.info('The XML file does not fit the required schema !',
                          exc_info=True)
             _logger.info(misc.ustr(relaxng.error_log.last_error))
             raise
         obj = XmlImport(self.env.cr,
                         self._module,
                         idref={},
                         mode='init',
                         report=None,
                         noupdate=False,
                         xml_filename=None)
         obj.parse(doc.getroot(), mode='init')
示例#10
0
def convert_xml_import(cr,
                       module,
                       xmlfile,
                       idref=None,
                       mode='init',
                       noupdate=False,
                       report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'], 'import_xml.rng')))
    try:
        relaxng.assert_(doc)
    except Exception:
        _logger.error('The XML file does not fit the required schema !')
        _logger.error(misc.ustr(relaxng.error_log.last_error))
        raise

    if idref is None:
        idref = {}
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate)
    obj.parse(doc.getroot())
    return True
示例#11
0
    def _read(self, file_content):
        # read content, raise an error if
        # - wrong format (decompression problem)
        # - XML not valid
        # - invalid signature
        try:
            signed_content_str = server_command.decompress(file_content)
        except:
            logger.error("Error reading uploaded license file: {}".format(
                process_tools.get_except_info()))
            raise LicenseFileReader.InvalidLicenseFile()

        signed_content_xml = etree.fromstring(signed_content_str)

        # noinspection PyUnresolvedReferences
        ng = etree.RelaxNG(etree.fromstring(LIC_FILE_RELAX_NG_DEFINITION))
        if not ng.validate(signed_content_xml):
            raise LicenseFileReader.InvalidLicenseFile(
                "Invalid license file structure")

        content_xml = signed_content_xml.find('icsw:license-file',
                                              ICSW_XML_NS_MAP)
        signature_xml = signed_content_xml.find('icsw:signature',
                                                ICSW_XML_NS_MAP)

        signature_ok = self.verify_signature(content_xml, signature_xml)

        if not signature_ok:
            raise LicenseFileReader.InvalidLicenseFile("Invalid signature")

        # notes about the XML:
        # - one or more packages
        # - packages may have the same UUID
        # - only the packags with the highest date (when having the same UUID) is valid
        # print(etree.tostring(content_xml))
        # print(ICSW_XML_NS_MAP)
        # print(content_xml.xpath(".//icsw:package-name/text()", namespaces=ICSW_XML_NS_MAP))
        # print(content_xml.xpath(".//icsw:package-uuid/text()", namespaces=ICSW_XML_NS_MAP))
        return content_xml
示例#12
0
class GetValidResponsesTest(OpenErpProxyTest):
    def _checkBody(self, response, callback):
        whenFinished = Deferred()
        response.deliverBody(PrinterClient(whenFinished))
        whenFinished.addCallback(callback)
        return whenFinished

    def _checkBodies(self, responses, callback, *params):
        deferreds = [Deferred() for r in responses]
        for i, (s, r) in enumerate(responses):
            r.deliverBody(PrinterClient(deferreds[i]))
        dl = DeferredList(deferreds)
        dl.addCallback(callback, *params)
        return dl

    def _isValidFeed(self, s):
        events = feedvalidator.validateString(s)['loggedEvents']
        fil = "A"
        filterFunc = getattr(compatibility, fil)
        events = filterFunc(events)
        output = Formatter(events)
        if output:
            print "\n".join(output)
        self.assertEqual(len(output), 0)

    def _isValidRelaxNg(self, s):
        doc = etree.fromstring(s)
        relaxng = etree.RelaxNG(doc)

    def _isValidXml(self, ((s1, schemaxml), (s2, docxml)), node):
        schema = etree.fromstring(schemaxml)
        relaxng = etree.RelaxNG(schema)
        doc = etree.fromstring(docxml).find(
            "{http://www.w3.org/2005/Atom}content").find(node)
        valid = relaxng.validate(doc)
        if not valid:
            log = relaxng.error_log
            print log.last_error
        self.assertTrue(valid)
示例#13
0
    def validate_document(self, rng_schema_path):
        # Construct the RelaxNG validator
        schema = etree.RelaxNG(file=rng_schema_path)

        # Parse the XML file
        parser = etree.XMLParser(**self.PARSER_OPTIONS)
        doc = etree.parse(self.source, parser=parser)

        logger.debug("Validating document '%s' with RNG '%s'", self.source,
                     rng_schema_path)

        error_log = None
        if not schema.validate(doc):
            logger.warning("Validation of document '%s' failed", self.source)
            self.rfc7940_checks.error('schema')
            error_log = schema.error_log
            if len(error_log) == 0:
                # Bug in LXML, see https://bugs.launchpad.net/lxml/+bug/1526522
                error_log = "CANNOT VALIDATE XML"

        self.rfc7940_checks.tested('schema')
        return error_log
示例#14
0
def schema_gantt(arch, **kwargs):
    """ Check the gantt view against its schema

    :type arch: etree._Element
    """
    global _gantt_validator

    if _gantt_validator is None:
        with misc.file_open(os.path.join('web_gantt', 'views',
                                         'gantt.rng')) as f:
            # gantt.rng needs to include common.rng from the `base/rng/` directory. The idea
            # here is to set the base url of lxml lib in order to load relative file from the
            # `base/rng` directory.
            base_url = os.path.join(get_resource_path('base', 'rng'), '')
            _gantt_validator = etree.RelaxNG(etree.parse(f, base_url=base_url))

    if _gantt_validator.validate(arch):
        return True

    for error in _gantt_validator.error_log:
        _logger.error(ustr(error))
    return False
示例#15
0
        def get_text(format, request):
            '''

            @param format:
            @type format: string
            @param request:
            @type request: string
            @return:
            @rtype: string
            '''
            print "get text"
            params = request.form if request.method == 'POST' else request.args
            files = request.files
            if format == 'json':
                if 'file' in files:
                    return files['file'].read()
                else:
                    return params.get('text')
            elif format == 'tcf':
                with open('assets/tcfschema/d-spin-local_0_4.rng', 'r') as f:

                    if 'file' in files:
                        text = files['file'].read()
                    else:
                        text = params.get('text').encode('utf-8')
                    relaxng_doc = etree.parse(f)
                    relaxng = etree.RelaxNG(relaxng_doc)
                    inputXml = re.sub(">\\s*<", "><", text)
                    inputXml = re.sub("^\\s*<", "<", inputXml)

                    doc = etree.parse(StringIO(inputXml))
                    try:
                        relaxng.assertValid(doc)
                        return doc.getroot()[1][0].text
                    except Exception as e:
                        raise InvalidUsage(e.message)
            else:
                raise InvalidUsage('Unknown format ' + format)
示例#16
0
def validate_xml_schema(xml_file):
    """Validates XML file based on RNG schema."""

    if not xml_file.name.endswith('.xml'):
        return  # Skip validation for other format files.

    is_valid = False
    try:
        # Could not make it working with a RNC schema, so using RNG instead.
        # lxml did not use rnc2rng as described in the documentation:
        # https://lxml.de/validation.html#relaxng
        schema = ET.fromstring(XML_RNG_SCHEMA.encode())
        relaxng = ET.RelaxNG(schema)
        hyp_doc = ET.parse(xml_file)
        is_valid = relaxng.validate(hyp_doc)
    except Exception as error:
        _msg = 'XML file invalid: {0}'.format(error)
        raise ValidationError(_msg)

    if not is_valid:
        _msg = 'XML file invalid: {0}. It does not validate against the XML Schema'.format(
            xml_file)
        raise ValidationError(_msg)
示例#17
0
def test_correct():
    """Tests the valid overall process."""
    tmp = NamedTemporaryFile(mode="w+")
    tmp.write("ValidFaultTree\n\n")
    tmp.write("root := g1 | g2 | g3 | g4 | g7 | g9 | g10 | g11 | e1\n")
    tmp.write("g1 := e2 & g3 & g5\n")
    tmp.write("g2 := h1 & g6\n")
    tmp.write("g3 := (g6 ^ e2)\n")
    tmp.write("g4 := @(2, [g5, e3, e4])\n")
    tmp.write("g5 := ~(e3)\n")
    tmp.write("g6 := (e3 | e4)\n\n")
    tmp.write("g7 := g8\n\n")
    tmp.write("g8 := ~e2 & ~e3\n\n")
    tmp.write("g9 := (g8 => g2)\n")
    tmp.write("g10 := e1 <=> e2\n")
    tmp.write("g11 := #(2, 4, [e1, e2, e3, e4, g5])\n")
    tmp.write("p(e1) = 0.1\n")
    tmp.write("p(e2) = 0.2\n")
    tmp.write("p(e3) = 0.3\n")
    tmp.write("s(h1) = true\n")
    tmp.write("s(h2) = false\n")
    tmp.flush()
    fault_tree = parse_input_file(tmp.name)
    assert fault_tree is not None
    assert len(fault_tree.gates) == 12
    assert len(fault_tree.basic_events) == 3
    assert len(fault_tree.house_events) == 2
    assert len(fault_tree.undefined_events()) == 1
    out = NamedTemporaryFile(mode="w+")
    out.write("<?xml version=\"1.0\"?>\n")
    out.write(fault_tree.to_xml())
    out.flush()
    relaxng_doc = etree.parse("schemas/2.0d/mef.rng")
    relaxng = etree.RelaxNG(relaxng_doc)
    with open(out.name, "r") as test_file:
        doc = etree.parse(test_file)
        assert relaxng.validate(doc)
示例#18
0
 def init_ag_xml(self):
     # validator
     _ng = etree.RelaxNG(etree.fromstring(AGGREGATE_NG))  # @UndefinedVariable
     _ag_dir = global_config["AGGREGATE_DIR"]
     tls = AGTopLevelStruct(self.log)
     for _dir, _dirs, _files in os.walk(_ag_dir):
         for _file in [_entry for _entry in _files if _entry.startswith("agg") and _entry.endswith(".xml")]:
             _file = os.path.join(_dir, _file)
             try:
                 cur_xml = etree.fromstring(file(_file, "rb").read())
             except:
                 self.log(
                     "error interpreting aggregate file {}: {}".format(
                         _file,
                         process_tools.get_except_info(),
                     )
                 )
             else:
                 for _xml_num, _xml in enumerate(cur_xml, 1):
                     _valid = _ng.validate(_xml)
                     if _valid:
                         self.log(
                             "added aggregate #{:d} from {}".format(
                                 _xml_num,
                                 _file,
                             )
                         )
                         tls.add_aggregate(AGObj(_xml))
                     else:
                         self.log(
                             "aggregate #{:d} from {} is invalid: {}".format(
                                 _xml_num,
                                 _file,
                                 str(_ng.error_log),
                             )
                         )
     self.ag_tls = tls
示例#19
0
    def load(self):
        """
        Read XML description, pass it along to the XSLT processor,
        validate it against the schema and finally pass it to the
        autogenerated(generateDS) parser.

        :return: instance of XML toplevel domain (image)
        :rtype: object
        """
        self._xsltproc()
        try:
            schema_doc = etree.parse(Defaults.get_schema_file())
            relaxng = etree.RelaxNG(schema_doc)
            schematron = isoschematron.Schematron(schema_doc,
                                                  store_report=True)
        except Exception as e:
            raise KiwiSchemaImportError('%s: %s' %
                                        (type(e).__name__, format(e)))
        try:
            description = etree.parse(self.description_xslt_processed.name)
            validation_rng = relaxng.validate(description)
            validation_sch = schematron.validate(description)
        except Exception as e:
            raise KiwiValidationError('%s: %s' % (type(e).__name__, format(e)))
        if not validation_rng:
            self._get_relaxng_validation_details()
        if not validation_sch:
            self._get_schematron_validation_details(
                schematron.validation_report)
        if not validation_rng or not validation_sch:
            if self.description:
                message = 'Schema validation for {description} failed'.format(
                    description=self.description)
            else:
                message = 'Schema validation for XML content failed'
            raise KiwiDescriptionInvalid(message)
        return self._parse()
示例#20
0
    def test_view(self):
        'Test validity of all views of the module'
        pool = Pool()
        View = pool.get('ir.ui.view')
        views = View.search([
                ('module', '=', self.module),
                ('model', '!=', ''),
                ])
        for view in views:
            if view.inherit and view.inherit.model == view.model:
                view_id = view.inherit.id
            else:
                view_id = view.id
            model = view.model
            Model = pool.get(model)
            res = Model.fields_view_get(view_id)
            assert res['model'] == model
            tree = etree.fromstring(res['arch'])

            validator = etree.RelaxNG(etree=View.get_rng(res['type']))
            # Don't use assert_ because 2to3 convert to assertTrue
            validator.assertValid(tree)

            tree_root = tree.getroottree().getroot()

            for element in tree_root.iter():
                if element.tag in ('field', 'label', 'separator', 'group'):
                    for attr in ('name', 'icon'):
                        field = element.get(attr)
                        if field:
                            assert field in res['fields'], (
                                'Missing field: %s' % field)
                if element.tag == 'button':
                    button_name = element.get('name')
                    assert button_name in Model._buttons, (
                        "Button '%s' is not in %s._buttons"
                        % (button_name, Model.__name__))
示例#21
0
    def test_assertXmlValidRelaxNG_relaxng(self):
        """Asserts assertXmlValidRelaxNG raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')

        relaxng = b"""<?xml version="1.0" encoding="utf-8"?>
        <rng:element name="root" xmlns:rng="http://relaxng.org/ns/structure/1.0">
            <rng:element name="child">
                <rng:attribute name="id">
                    <rng:text />
                </rng:attribute>
            </rng:element>
        </rng:element>
        """

        xml_relaxng = etree.RelaxNG(etree.XML(relaxng))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        test_case.assertXmlValidRelaxNG(root, xml_relaxng)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidRelaxNG(root, xml_relaxng)
示例#22
0
 def __init__(self, log_com):
     self.__compounds = []
     self.__log_com = log_com
     _ng = etree.RelaxNG(
         etree.fromstring(COMPOUND_NG))  # @UndefinedVariable
     compound_xml = E.compounds()
     _comp_dir = global_config["COMPOUND_DIR"]
     for _dir, _dirs, _files in os.walk(_comp_dir):
         for _file in [
                 _entry for _entry in _files
                 if _entry.startswith("comp") and _entry.endswith(".xml")
         ]:
             _file = os.path.join(_dir, _file)
             try:
                 cur_xml = etree.fromstring(file(_file, "rb").read())
             except:
                 self.log("error interpreting compound file {}: {}".format(
                     _file,
                     process_tools.get_except_info(),
                 ))
             else:
                 for _xml_num, _xml in enumerate(cur_xml, 1):
                     _valid = _ng.validate(_xml)
                     if _valid:
                         self.log("added compound #{:d} from {}".format(
                             _xml_num,
                             _file,
                         ))
                         self.__compounds.append(CompoundEntry(_xml))
                     else:
                         self.log(
                             "compound #{:d} from {} is invalid: {}".format(
                                 _xml_num,
                                 _file,
                                 str(_ng.error_log),
                             ))
示例#23
0
    def validate_structure(self):
        relaxng = None

        for path in Specfile.RELAXNG_SCHEMA_SEARCH_PATH:
            if os.path.exists(path):
                relaxng = etree.RelaxNG(file=path)
                break
        #end for

        if relaxng is None:
            raise RuntimeError("RELAX NG ruleset not found.")

        if not relaxng.validate(self.xml_doc):
            errors = []
            for err in relaxng.error_log:
                errors.append(
                    "* %s on line %d, column %d: %s" % (os.path.basename(
                        err.filename), err.line, err.column, err.message))
            #end for
            msg = "RELAX NG validation failed:\n" + "\n".join(errors)
            raise MalformedSpecfile(msg)
        #end if

        return True
示例#24
0
def validate_tei(teipath, rngfile):
    valid = 0
    invalid = 0
    for teifile in glob.glob(teipath): 
        idno = os.path.basename(teifile)
        print(idno)
        rngparsed = etree.parse("cligs.rng")
        rngvalidator = etree.RelaxNG(rngparsed)
        parser = etree.XMLParser(recover=True)
        teiparsed = etree.parse(teifile, parser)
        #teiparsed = etree.parse(teifile)
        validation = rngvalidator.validate(teiparsed)
        #log = rngvalidator.error_log
        if validation == True:
            valid +=1
            #print(idno, "valid!")
        else:
            invalid +=1
            print(idno, "sorry, not valid!")
            #print(log.last_error)
            #print(log.last_error.domain_name)
    print("valid:", valid, "-- invalid:", invalid)
    if invalid == 0: 
        print("Congratulations, all of your "+str(valid)+" files are valid!")
示例#25
0
 def __init__(self):
     _inst = InstanceXML(quiet=True)
     all_instances = sum([
         _inst.xpath(".//config-enums/config-enum/text()")
         for _inst in _inst.get_all_instances()
     ], [])
     all_perms = [
         _perm.perm_name for _perm in csw_permission.objects.all()
     ] + ["$$CHECK_FOR_SUPERUSER"]
     _content = file(
         "{}/menu_relax.xml".format(os.path.join(settings.FILE_ROOT,
                                                 "menu")),
         "r",
     ).read()
     _content = _content.replace(
         "<value>RIGHTSLIST</value>",
         "".join(["<value>{}</value>".format(_pn)
                  for _pn in all_perms])).replace(
                      "<value>SERVICETYPESLIST</value>", "".join([
                          "<value>{}</value>".format(_stn)
                          for _stn in all_instances
                      ]))
     # sys.exit(0)
     self.ng = etree.RelaxNG(etree.fromstring(_content, ))
示例#26
0
 def is_schema_valid(self, cr, uid, id, xml) :
     """Checks the validity of the provided XML according to the specified schema
     
     :param xml: root of XML-structure to be checked 
     :return: Boolean
     """
     obj = self.browse(cr, uid, id)
     if obj and obj.schema :
         parser = etree.XMLParser(no_network=False)
         schema_root = etree.parse(obj.schema, parser)
         if ".xsd" in obj.schema.lower() :
             schema = etree.XMLSchema(schema_root)
         elif ".rng" in obj.schema.lower() :
             schema = etree.RelaxNG(schema_root)
         else :
             raise osv.except_osv \
                 ( _("Data Error !")
                 , _("Unknown schema type: %s" % obj.schema)
                 )
         return schema.validate(xml)
     raise osv.except_osv \
         ( _("Data Error !")
         , _("Invalid Template with ID: %s" % id)
         )
示例#27
0
                 'iso_dsdl_include.xsl')))
iso_abstract_expand = _etree.XSLT(_etree.parse(
    os.path.join(_resources_dir, 'xsl', 'iso-schematron-xslt1',
                 'iso_abstract_expand.xsl')))
iso_svrl_for_xslt1 = _etree.XSLT(_etree.parse(
    os.path.join(_resources_dir,
                 'xsl', 'iso-schematron-xslt1', 'iso_svrl_for_xslt1.xsl')))


# svrl result accessors
svrl_validation_errors = _etree.XPath(
    '//svrl:failed-assert', namespaces={'svrl': SVRL_NS})


# RelaxNG validator for schematron schemas
schematron_schema_valid = _etree.RelaxNG(
    file=os.path.join(_resources_dir, 'rng', 'iso-schematron.rng'))


def stylesheet_params(**kwargs):
    """Convert keyword args to a dictionary of stylesheet parameters.
    XSL stylesheet parameters must be XPath expressions, i.e.:

    * string expressions, like "'5'"
    * simple (number) expressions, like "5"
    * valid XPath expressions, like "/a/b/text()"

    This function converts native Python keyword arguments to stylesheet
    parameters following these rules:
    If an arg is a string wrap it with XSLT.strparam().
    If an arg is an XPath object use its path string.
    If arg is None raise TypeError.
示例#28
0
    def load(self):  # noqa: C901
        """
        Read XML description, pass it along to the XSLT processor,
        validate it against the schema and finally pass it to the
        autogenerated(generateDS) parser.

        :return: instance of XML toplevel domain (image)

        :rtype: object
        """
        try:
            self._xsltproc()
            schema_doc = etree.parse(Defaults.get_schema_file())
            relaxng = etree.RelaxNG(schema_doc)
            schematron = isoschematron.Schematron(schema_doc,
                                                  store_report=True)
        except Exception as e:
            raise KiwiSchemaImportError('%s: %s' %
                                        (type(e).__name__, format(e)))
        try:
            description = etree.parse(self.description_xslt_processed.name)
            validation_rng = relaxng.validate(description)
            validation_sch = schematron.validate(description)
        except Exception as e:
            raise KiwiValidationError('%s: %s' % (type(e).__name__, format(e)))
        if not validation_rng:
            self._get_relaxng_validation_details(
                Defaults.get_schema_file(),
                self.description_xslt_processed.name)
        if not validation_sch:
            self._get_schematron_validation_details(
                schematron.validation_report)
        if not validation_rng or not validation_sch:
            if self.description:
                message = 'Schema validation for {description} failed'.format(
                    description=self.description)
            else:
                message = 'Schema validation for XML content failed'
            raise KiwiDescriptionInvalid(message)

        parse_result = self._parse()

        if parse_result.get_extension():
            extension_namespace_map = \
                description.getroot().xpath('extension')[0].nsmap

            for namespace_name in extension_namespace_map:
                extensions_for_namespace = description.getroot().xpath(
                    'extension/{namespace}:*'.format(namespace=namespace_name),
                    namespaces=extension_namespace_map)
                if extensions_for_namespace:
                    # one toplevel entry point per extension via xmlns
                    if len(extensions_for_namespace) > 1:
                        raise KiwiExtensionError(
                            'Multiple toplevel sections for "{0}" found'.
                            format(namespace_name))

                    # store extension xml data parse tree for this namespace
                    self.extension_data[namespace_name] = \
                        etree.ElementTree(extensions_for_namespace[0])

                    # validate extension xml data
                    try:
                        xml_catalog = Command.run([
                            'xmlcatalog', '/etc/xml/catalog',
                            extension_namespace_map[namespace_name]
                        ])
                        extension_schema = xml_catalog.output.rstrip().replace(
                            'file://', '')
                        extension_relaxng = etree.RelaxNG(
                            etree.parse(extension_schema))
                    except Exception as e:
                        raise KiwiExtensionError(
                            'Extension schema error: {0}: {1}'.format(
                                type(e).__name__, e))
                    validation_result = extension_relaxng.validate(
                        self.extension_data[namespace_name])
                    if not validation_result:
                        xml_data_unformatted = etree.tostring(
                            self.extension_data[namespace_name],
                            encoding='utf-8')
                        xml_data_domtree = minidom.parseString(
                            xml_data_unformatted)
                        extension_file = NamedTemporaryFile()
                        with open(extension_file.name, 'w') as xml_data:
                            xml_data.write(xml_data_domtree.toprettyxml())
                        self._get_relaxng_validation_details(
                            extension_schema, extension_file.name)
                        raise KiwiExtensionError(
                            'Schema validation for extension XML data failed')

        return parse_result
示例#29
0
文件: Validate.py 项目: j105rob/ipfix
# RelaxNG validator utility
#
# This script will validate every XML file found inside of the current Splunk
# instance's /etc/apps directory
#
# See README.txt for setup information
#

import lxml.etree as et
import os, re

FOLDER = os.path.dirname(os.path.abspath(__file__))

f = open(os.path.join(FOLDER,'../_support/ipfix.rng'), 'r')
schema = et.parse(f)
relaxng = et.RelaxNG(schema)
f.close()

for root, dirs, files in os.walk(FOLDER):
    for name in files:
        if name.endswith('.xml'):
            print '=' * 80
            fullname = os.path.join(root, name).replace("\\","/")
            print "Validating file: %s" % fullname
            f = open(fullname, 'r')
            rootNode = et.parse(f)
            isValid = relaxng.validate(rootNode)
            if not isValid:
                print "Validation error: %s" % name
                print str(relaxng.error_log).replace("file:///" + fullname + ":","")
                print ''
示例#30
0
    def load(self) -> Any:
        """
        Read XML description, validate it against the schema
        and the schematron rules and pass it to the
        autogenerated(generateDS) parser.

        :return: instance of XML toplevel domain (image)

        :rtype: object
        """
        try:
            schema_doc = etree.parse(Defaults.get_schema_file())
            relaxng = etree.RelaxNG(schema_doc)
            schematron = isoschematron.Schematron(
                schema_doc, store_report=True
            )
        except Exception as issue:
            raise KiwiSchemaImportError(issue)
        try:
            description = etree.parse(self.description)
            validation_rng = relaxng.validate(description)
            validation_schematron = schematron.validate(description)
        except Exception as issue:
            raise KiwiValidationError(issue)
        if not validation_rng:
            XMLDescription._get_relaxng_validation_details(
                Defaults.get_schema_file(),
                self.description,
                relaxng.error_log
            )
        if not validation_schematron:
            XMLDescription._get_schematron_validation_details(
                schematron.validation_report
            )
        if not validation_rng or not validation_schematron:
            raise KiwiDescriptionInvalid(
                'Failed to validate schema and/or schematron rules'
            )

        parse_result = self._parse()

        if parse_result.get_extension():
            extension_namespace_map = \
                description.getroot().xpath('extension')[0].nsmap

            for namespace_name in extension_namespace_map:
                extensions_for_namespace = description.getroot().xpath(
                    'extension/{namespace}:*'.format(namespace=namespace_name),
                    namespaces=extension_namespace_map
                )
                if extensions_for_namespace:
                    # one toplevel entry point per extension via xmlns
                    if len(extensions_for_namespace) > 1:
                        raise KiwiExtensionError(
                            'Multiple toplevel sections for "{0}" found'.format(
                                namespace_name
                            )
                        )

                    # store extension xml data parse tree for this namespace
                    self.extension_data[namespace_name] = \
                        etree.ElementTree(extensions_for_namespace[0])

                    # validate extension xml data
                    try:
                        xml_catalog = Command.run(
                            [
                                'xmlcatalog', '/etc/xml/catalog',
                                extension_namespace_map[namespace_name]
                            ]
                        )
                        extension_schema = xml_catalog.output.rstrip().replace(
                            'file://', ''
                        )
                        extension_relaxng = etree.RelaxNG(
                            etree.parse(extension_schema)
                        )
                    except Exception as issue:
                        raise KiwiExtensionError(
                            'Extension schema error: {0}'.format(issue)
                        )
                    validation_result = extension_relaxng.validate(
                        self.extension_data[namespace_name]
                    )
                    if not validation_result:
                        xml_data_unformatted = etree.tostring(
                            self.extension_data[namespace_name],
                            encoding='utf-8'
                        )
                        xml_data_domtree = minidom.parseString(
                            xml_data_unformatted
                        )
                        extension_file = Temporary().new_file()
                        with open(extension_file.name, 'w') as xml_data:
                            xml_data.write(xml_data_domtree.toprettyxml())
                        XMLDescription._get_relaxng_validation_details(
                            extension_schema,
                            extension_file.name,
                            extension_relaxng.error_log
                        )
                        raise KiwiExtensionError(
                            'Schema validation for extension XML data failed'
                        )

        return parse_result