1 | """ |
---|
2 | Generic XML reader |
---|
3 | """ |
---|
4 | ############################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | #If you use DANSE applications to do scientific research that leads to |
---|
9 | #publication, we ask that you acknowledge the use of the software with the |
---|
10 | #following sentence: |
---|
11 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
12 | #copyright 2008,2009 University of Tennessee |
---|
13 | ############################################################################# |
---|
14 | |
---|
15 | from lxml import etree |
---|
16 | parser = etree.ETCompatXMLParser() |
---|
17 | |
---|
18 | class XMLreader(): |
---|
19 | |
---|
20 | xml = None |
---|
21 | xmldoc = None |
---|
22 | xmlroot = None |
---|
23 | schema = None |
---|
24 | schemadoc = None |
---|
25 | |
---|
26 | def __init__(self, xml = None, schema = None, root = None): |
---|
27 | self.xml = xml |
---|
28 | self.schema = schema |
---|
29 | if xml is not None: |
---|
30 | self.setXMLFile(xml, root) |
---|
31 | else: |
---|
32 | self.xmldoc = None |
---|
33 | self.xmlroot = None |
---|
34 | if schema is not None: |
---|
35 | self.setSchema(schema) |
---|
36 | else: |
---|
37 | self.schemadoc = None |
---|
38 | |
---|
39 | def reader(self): |
---|
40 | """ |
---|
41 | Read in an XML file into memory and return an lxml dictionary |
---|
42 | """ |
---|
43 | if self.validateXML(): |
---|
44 | self.xmldoc = etree.parse(self.xml, parser = parser) |
---|
45 | else: |
---|
46 | raise etree.ValidationError(self, self.findInvalidXML()) |
---|
47 | return self.xmldoc |
---|
48 | |
---|
49 | def setXMLFile(self, xml, root = None): |
---|
50 | try: |
---|
51 | self.xml = xml |
---|
52 | self.xmldoc = etree.parse(self.xml, parser = parser) |
---|
53 | self.xmlroot = self.xmldoc.getroot() |
---|
54 | except Exception: |
---|
55 | self.xml = None |
---|
56 | self.xmldoc = None |
---|
57 | self.xmlroot = None |
---|
58 | |
---|
59 | def setSchema(self, schema): |
---|
60 | try: |
---|
61 | self.schema = schema |
---|
62 | self.schemadoc = etree.parse(self.schema, parser = parser) |
---|
63 | except Exception: |
---|
64 | self.schema = None |
---|
65 | self.schemadoc = None |
---|
66 | |
---|
67 | def validateXML(self): |
---|
68 | """ |
---|
69 | Checks to see if the XML file meets the schema |
---|
70 | """ |
---|
71 | valid = True |
---|
72 | if self.schema is not None: |
---|
73 | self.parseSchemaAndDoc() |
---|
74 | schemaCheck = etree.XMLSchema(self.schemadoc) |
---|
75 | valid = schemaCheck.validate(self.xmldoc) |
---|
76 | return valid |
---|
77 | |
---|
78 | def findInvalidXML(self): |
---|
79 | """ |
---|
80 | Finds the first offending element that should not be present in an XML file |
---|
81 | """ |
---|
82 | firstError = "" |
---|
83 | self.parseSchemaAndDoc() |
---|
84 | schema = etree.XMLSchema(self.schemadoc) |
---|
85 | try: |
---|
86 | firstError = schema.assertValid(self.xmldoc) |
---|
87 | except etree.DocumentInvalid as e: |
---|
88 | firstError = str(e) |
---|
89 | return firstError |
---|
90 | |
---|
91 | def parseSchemaAndDoc(self): |
---|
92 | """ |
---|
93 | Creates a dictionary of the parsed schema and xml files. |
---|
94 | """ |
---|
95 | self.setXMLFile(self.xml) |
---|
96 | self.setSchema(self.schema) |
---|
97 | |
---|