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 __authors__ = [ "Karl Levik", "Marie-Francoise Incardona", "Olof Svensson" ]
31 __contact__ = "karl.levik@diamond.ac.uk"
32 __license__ = "LGPLv3+"
33 __copyright__ = "Diamond Light Source, Chilton, Didcot, UK"
34
35 import StringIO, re, xml.dom.minidom
39
40 @staticmethod
42 """
43 Returns a DNA compatible XML representation of an object derived from XSData.
44 DNA compatible XML has no whitespace or newlines etc. between elements and single-quotes must be escaped.
45 In addition, booleans must be either "True" or "False". Anything but "True" is interpreted as "False".
46 """
47 stringIO = StringIO.StringIO()
48 stringIO.write('<?xml version="1.0" encoding="ISO-8859-1"?>')
49
50 regexp = re.compile('XSDataISPyB(.*)')
51 matchObject = regexp.match(_xsDataObject.__class__.__name__)
52 strDNAClassName = matchObject.group(1)
53
54 _xsDataObject.export(stringIO, 0, name_=strDNAClassName)
55 strXML = stringIO.getvalue()
56 stringIO.close()
57
58
59 strParsed = xml.dom.minidom.parseString(strXML)
60 regexpXMLHeader = re.compile('\<\?xml version="1.0" \?\>(.*)')
61
62 regexpValueTags = re.compile('\<value\>(.*)\</value\>')
63 regexpOtherTag = re.compile('\<(.*)\>')
64 strXML = ''
65 bIsBoolean = False
66
67 for strLine in strParsed.toxml().splitlines():
68 if strLine.startswith('<?xml version="1.0" ?>'):
69 strXML += '<?xml version="1.0" ?>'
70 strLine = strLine[22:].strip()
71 if len(strLine) == 0:
72 continue
73 if strLine.startswith('</%s>' % strDNAClassName):
74 strXML += '</%s>' % strDNAClassName
75 break
76 if strLine.startswith('<%s>' % strDNAClassName):
77 strXML += '<%s>' % strDNAClassName
78 strLine = strLine[len(strDNAClassName) + 2:]
79 if len(strLine) == 0:
80 continue
81
82 m = regexpValueTags.match(strLine.strip())
83 if m != None:
84 strValue = m.group(1)
85 strValue = strValue.replace('\'', '\\\'')
86 if bIsBoolean:
87 strValue = strValue.replace('1', 'True')
88 strXML += strValue
89 else:
90 strXML += strLine.strip()
91 m2 = regexpOtherTag.match(strLine.strip())
92 if m2 != None:
93 strValue = m2.group(1)
94 if not strValue.startswith("/"):
95 method = getattr(_xsDataObject, 'get%s' % EDUtilsXML.capitalizeFirstLetter(strValue))
96 o = method()
97 if o.__class__.__name__ == 'XSDataBoolean':
98 bIsBoolean = True
99 else:
100 bIsBoolean = False
101 return strXML
102
103
104 @staticmethod
106 """
107 Capitalizes the first letter of a string, leaving the rest untouched
108 """
109 return (_str[0].capitalize() + _str[1:])
110