-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodsXML.py
More file actions
120 lines (98 loc) · 3.33 KB
/
odsXML.py
File metadata and controls
120 lines (98 loc) · 3.33 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Data:
def __init__(self, data):
self.type = 'Data'
self.data = self.cleanString(data)
def setData(self, data):
self.data = self.cleanString(data)
def getData(self):
return self.data
def cleanString(self, string):
string = string.replace("&", "&")
string = string.replace('"', """)
string = string.replace("'", "'")
string = string.replace("<", "<")
string = string.replace(">", ">")
return string
def toString(self):
return self.data
class Element:
def __init__(self, element, data=None):
self.type = 'Element'
self.element = ''
self.dataChild = None
self.setElement(element)
self.attributeValues = {}
self.childrenList = []
if not (data == None):
self.dataChild = Data(data)
self.addChild(self.dataChild)
def cleanString(self, string):
string = str(string)
string = string.replace("&", "&")
string = string.replace('"', """)
string = string.replace("'", "'")
string = string.replace("<", "<")
string = string.replace(">", ">")
return string
def setElement(self, element):
self.element = self.cleanString(element)
def getElement(self):
return self.element
def setData(self, data):
if not (self.dataChild == None):
self.dataChild.setData(data)
return self.dataChild
return None
def getData(self):
if not (self.dataChild == None):
return self.dataChild.getData()
return None
def setAttribute(self, name, value):
self.attributeValues[name] = self.cleanString(value)
def getAttribute(self, name):
if name in self.attributeValues:
return self.attributeValues[name]
return None
def removeAttribute(self, name):
if name in self.attributeValues:
self.attributeValues.pop(name)
def getAttributeString(self):
rlist = []
rline = ''
for key in self.attributeValues:
value = self.attributeValues[key]
rlist.append("%s='%s'" % (key, value))
rline = ' '.join(rlist)
return rline
def addChild(self, child):
self.childrenList.append(child)
return child
def removeChild(self, index):
if index < len(self.childrenList):
self.childrenList.pop(index)
def removeChildren(self):
self.childrenList = []
def getChild(self, index):
if index < len(self.childrenList):
return self.childrenList[index]
return None
def childCount(self):
return len(self.childrenList)
def toString(self):
# Open the element
if self.attributeValues:
# Display attributes
rstring = '<%s %s' % (self.element, self.getAttributeString())
else:
rstring = '<%s' % self.element
# Display children if they exist
if self.childrenList:
rstring += '>'
for child in self.childrenList:
rstring += child.toString()
# close with children
rstring += '</%s>' % self.element
else:
# close without children
rstring += '/>'
return rstring