summaryrefslogtreecommitdiff
path: root/aifverify.py
blob: 74b7996ed3524ae68b554bd9f7af03f42ef6741d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3


import re
import os
import io
from lxml import etree
from urllib.request import urlopen

cwd = os.path.dirname(os.path.realpath(__file__))

# Validate in the form of file:XSD/namespace.
xmlfiles = {}
#xmlfiles['aif.xml'] = 'https://aif.square-r00t.net/aif.xsd'
xmlfiles['aif.xml'] = 'aif.xsd'

def validXSD(xsdfile):
    print("Checking XSD: ", xsdfile)
    webres = False
    if re.match('^(https?|ftp)', xsdfile, re.IGNORECASE):
        webres = True
    if not webres:
        with open('{0}/{1}'.format(cwd, xsdfile), 'rb') as f:
            xsd_in = f.read()
    else:
        with urlopen(xsdfile) as f:
            xsd_in = f.read()
    xsd = False
    try:
        xsd_in = io.BytesIO(xsd_in)
        xmlschema_doc = etree.parse(xsd_in)
        xsd = etree.XMLSchema(xmlschema_doc)
    except:
        print('XSD: {0} failed.'.format(xsdfile))
    return(xsd)

def validXML(xml, xsd):
    print("Checking XML: ", xml)
    xmlfile = xml
    with open('{0}/{1}'.format(cwd, xml), 'rb') as f:
        xml_in = f.read()
    valid = False
    try:
        xml_in = io.BytesIO(xml_in)
        xml = etree.parse(xml_in)
        valid = xsd.validate(xml)
    except:
        print('XML: {0} failed.'.format(xmlfile))
    return(valid)

def allValidXML(xmlfiles):
    for key,value in xmlfiles.items():
        xmlfile = key
        xsdfile = xmlfiles[xmlfile]
        xml = False
        xsdobj = validXSD(xsdfile)
        xml = validXML(xmlfile, xsdobj)
    return(xml)


if __name__ == '__main__':
    allValidXML(xmlfiles)