-
Notifications
You must be signed in to change notification settings - Fork 4
/
xml.py
executable file
·225 lines (199 loc) · 6.98 KB
/
xml.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from Peach.analyzer import *
from Peach.Engine.dom import *
from Peach.Engine.common import *
from Peach.Engine.parser import PeachResolver
from lxml import etree
class XmlAnalyzer(Analyzer):
"""
Produces data models or PeachPits from XML documents.
"""
supportDataElement = True
supportCommandLine = True
supportTopLevel = True
def __init__(self):
pass
def asDataElement(self, parent, args, dataBuffer):
"""
Called when Analyzer is used in a data model.
Should return a DataElement such as Block, Number or String.
"""
if len(dataBuffer) == 0:
return
dom = _Xml2Dom().xml2Dom(dataBuffer)
# replace parent with new dom
dom.name = parent.name
parentOfParent = parent.parent
indx = parentOfParent.index(parent)
del parentOfParent[parent.name]
parentOfParent.insert(indx, dom)
def asCommandLine(self, args):
"""
Called when Analyzer is used from command line.
Analyzer should produce PeachPit XML as output.
"""
try:
inFile = args["xmlfile"]
outFile = args["out"]
except:
raise PeachException("XmlAnalyzer requires two parameters, xmlfile and out.")
xml = _Xml2Peach().xml2Peach("file:" + inFile)
with open(outFile, "wb+") as fo:
fo.write(xml)
def asTopLevel(self, peach, args):
"""
Called when Analyzer is used from top level.
From the top level producing zero or more data models and state models is possible.
"""
raise Exception("asTopLevel not supported")
class _Xml2Peach(object):
XmlContainer = """
<?xml version="1.0" encoding="utf-8"?>
<Peach>
<Include ns="default" src="file:defaults.xml" />
<DataModel name="TheDataModel">
%s
</DataModel>
<StateModel name="TheState" initialState="Initial">
<State name="Initial">
<Action type="output">
<DataModel ref="TheDataModel" />
</Action>
</State>
</StateModel>
<Agent name="LocalAgent" location="http://127.0.0.1:9000">
<Monitor class="test.TestStopOnFirst" />
</Agent>
-->
<Test name="TheTest">
<!-- <Agent ref="LocalAgent"/> -->
<StateModel ref="TheState"/>
<!-- TODO: Complete publisher -->
<Publisher class="stdout.Stdout" />
</Test>
<Run name="DefaultRun">
<Test ref="TheTest" />
</Run>
</Peach>
"""
def xml2Peach(self, url):
parser = etree.XMLParser()
parser.resolvers.add(PeachResolver())
doc = etree.parse(url, parser=parser)
peachDoc = etree.Element("DEADBEEF")
self.handleElement(doc, peachDoc)
# Get the string representation
# TODO: make it better
value = etree.tostring(peachDoc, pretty_print=True).strip()
deadbeef, value = value[:10], value[10:]
assert deadbeef == "<DEADBEEF>"
value, deadbeef = value[:-11], value[-11:]
assert deadbeef == "</DEADBEEF>"
return self.XmlContainer % value
def handleElement(self, node, parent):
"""
Handle an XML element, children and attributes. Returns an XmlElement object.
"""
if parent is None:
return None
# Element
element = etree.Element("XmlElement")
ns, tag = split_ns(node.tag)
element.set("elementName", tag)
if ns is not None:
element.set("ns", ns)
parent.append(element)
# Element attributes
for attrib in node.keys():
attribElement = self.handleAttribute(attrib, node.get(attrib), element)
element.append(attribElement)
# Element children
self._handleText(node.text, element)
for child in node.iterchildren():
if etree.iselement(child): # TODO: skip comments
self.handleElement(child, element)
self._handleText(child.tail, element)
return element
def _handleText(self, text, parent):
if text is not None and len(text.strip('\n\r\t\x10 ')) > 0:
string = etree.Element("String")
string.set("value", text)
string.set("type", "utf8")
parent.append(string)
def handleAttribute(self, attrib, attribObj, parent):
"""
Handle an XML attribute. Returns an XmlAttribute object.
"""
# Attribute
element = etree.Element("XmlAttribute")
ns, attrib = split_ns(attrib)
if ns is not None:
element.set("ns", ns)
element.set("attributeName", attrib)
# Attribute value
string = etree.Element("String")
string.set("value", attribObj)
string.set("type", "utf8")
element.append(string)
return element
class _Xml2Dom(object):
"""
Convert an XML Document into a Peach DOM.
"""
def xml2Dom(self, data):
child = etree.XML(data)
doc = child.getroottree()
root = self.handleElement(child, None)
return root
def handleElement(self, node, parent):
"""
Handle an XML element, children and attributes. Returns an XmlElement object.
"""
doc = node.getroottree()
# Element
element = XmlElement(None, parent)
ns, tag = split_ns(node.tag)
if ns is not None:
element.xmlNamespace = ns
element.elementName = tag
# Element attributes
for attrib in node.keys():
attribElement = self.handleAttribute(attrib, node.get(attrib), element)
element.append(attribElement)
# Element children
self._handleText(node.text, element)
for child in node.iterchildren():
if etree.iselement(child): # TODO: skip comments
childElement = self.handleElement(child, element)
element.append(childElement)
self._handleText(child.tail, element)
return element
def _handleText(self, text, parent):
if text is not None and len(text.strip('\n\r\t\x10 ')) > 0:
string = String(None, parent)
string.defaultValue = text
parent.append(string)
try:
_ = int(string.defaultValue)
hint = Hint("NumericalString", string)
hint.value = "true"
string.hints.append(hint)
except ValueError:
pass
def handleAttribute(self, attrib, attribObj, parent):
"""
Handle an XML attribute. Returns an XmlAttribute object.
"""
# Attribute
element = XmlAttribute(None, parent)
ns, attrib = split_ns(attrib)
if ns is not None:
element.xmlNamespace = ns
element.attributeName = attrib
# Attribute value
string = String(None, element)
string.defaultValue = attribObj
element.append(string)
return element