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 | def __init__(self, xml = None, schema = None, root = None): |
---|
21 | self.xml = xml |
---|
22 | self.schema = schema |
---|
23 | if xml is not None: |
---|
24 | self.setXMLFile(xml, root) |
---|
25 | else: |
---|
26 | self.xmldoc = None |
---|
27 | self.xmlroot = None |
---|
28 | if schema is not None: |
---|
29 | self.setSchema(schema) |
---|
30 | else: |
---|
31 | self.schemadoc = None |
---|
32 | |
---|
33 | def reader(self): |
---|
34 | """ |
---|
35 | Read in an XML file into memory and return an lxml dictionary |
---|
36 | """ |
---|
37 | if self.validateXML(): |
---|
38 | self.xmldoc = etree.parse(self.xml, parser = parser) |
---|
39 | else: |
---|
40 | raise etree.ValidationError(self, self.findInvalidXML()) |
---|
41 | return self.xmldoc |
---|
42 | |
---|
43 | def setXMLFile(self, xml, root = None): |
---|
44 | try: |
---|
45 | self.xml = xml |
---|
46 | self.xmldoc = etree.parse(self.xml, parser = parser) |
---|
47 | self.xmlroot = self.xmldoc.getroot() |
---|
48 | except Exception: |
---|
49 | ##!TODO: raise exception if no xml is passed to this function |
---|
50 | print "No xml file was found!" |
---|
51 | |
---|
52 | def setSchema(self, schema): |
---|
53 | try: |
---|
54 | self.schema = schema |
---|
55 | self.schemadoc = etree.parse(self.schema, parser = parser) |
---|
56 | except Exception: |
---|
57 | ##!TODO: raise exception if no schema is passed to this function |
---|
58 | print "No schema file was found!" |
---|
59 | |
---|
60 | def validateXML(self): |
---|
61 | """ |
---|
62 | Checks to see if the XML file meets the schema |
---|
63 | """ |
---|
64 | valid = True |
---|
65 | if self.schema is not None: |
---|
66 | self.parseSchemaAndDoc() |
---|
67 | schemaCheck = etree.XMLSchema(self.schemadoc) |
---|
68 | valid = schemaCheck.validate(self.xmldoc) |
---|
69 | return valid |
---|
70 | |
---|
71 | def findInvalidXML(self): |
---|
72 | """ |
---|
73 | Finds the first offending element that should not be present in an XML file |
---|
74 | """ |
---|
75 | firstError = "" |
---|
76 | self.parseSchemaAndDoc() |
---|
77 | schema = etree.XMLSchema(self.schemadoc) |
---|
78 | try: |
---|
79 | firstError = schema.assertValid(self.xmldoc) |
---|
80 | except etree.DocumentInvalid as e: |
---|
81 | firstError = str(e) |
---|
82 | return firstError |
---|
83 | |
---|
84 | def parseSchemaAndDoc(self): |
---|
85 | """ |
---|
86 | Creates a dictionary of the parsed schema and xml files. |
---|
87 | """ |
---|
88 | self.setXMLFile(self.xml) |
---|
89 | self.setSchema(self.schema) |
---|
90 | |
---|