1
|
# XPath parsing
|
2
|
|
3
|
import copy
|
4
|
import warnings
|
5
|
|
6
|
from Parser import Parser
|
7
|
import util
|
8
|
import xml_dom
|
9
|
|
10
|
##### Path elements
|
11
|
|
12
|
class XpathElem:
|
13
|
def __init__(self, name='', value=None, is_attr=False):
|
14
|
self.name = name
|
15
|
self.value = value
|
16
|
self.is_attr = is_attr
|
17
|
self.is_positive = True
|
18
|
self.is_ptr = False
|
19
|
self.keys = []
|
20
|
self.attrs = []
|
21
|
self.other_branches = [] # temp implementation for split paths
|
22
|
|
23
|
def __repr__(self):
|
24
|
str_ = ''
|
25
|
if not self.is_positive: str_ += '!'
|
26
|
if self.is_attr: str_ += '@'
|
27
|
if self.name == '': str_ += '""'
|
28
|
else: str_ += self.name
|
29
|
if self.keys != []: str_ += repr(self.keys)
|
30
|
if self.attrs != []: str_ += ':'+repr(self.attrs)
|
31
|
if self.is_ptr: str_ += '->'
|
32
|
if self.other_branches != []: str_ += '{'+repr(self.other_branches)+'}'
|
33
|
if self.value != None: str_ += '='+repr(self.value)
|
34
|
return str_
|
35
|
|
36
|
def __eq__(self, other): return self.__dict__ == other.__dict__
|
37
|
|
38
|
empty_elem = XpathElem()
|
39
|
|
40
|
def elem_is_empty(elem): return elem == empty_elem
|
41
|
|
42
|
def is_self(elem): return elem.name == '' or elem.name == '.'
|
43
|
|
44
|
def is_parent(elem): return elem.name == '..'
|
45
|
|
46
|
##### Paths
|
47
|
|
48
|
def is_positive(path): return path[0].is_positive
|
49
|
|
50
|
def is_rooted(path): return elem_is_empty(path[0])
|
51
|
|
52
|
def value(path): return path[-1].value
|
53
|
|
54
|
def set_value(path, value): path[-1].value = value
|
55
|
|
56
|
def backward_id(elem):
|
57
|
if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
|
58
|
else: return None
|
59
|
|
60
|
instance_level = 1 # root's grandchildren
|
61
|
|
62
|
def obj(path):
|
63
|
obj_path = copy.deepcopy(path[:instance_level+1])
|
64
|
obj_path[-1].is_ptr = False # prevent pointer w/o target
|
65
|
return obj_path
|
66
|
|
67
|
def set_id(path, id_, has_types=True):
|
68
|
if has_types: id_level = instance_level
|
69
|
else: id_level = 0 # root's children
|
70
|
if is_self(path[0]): id_level += 1 # explicit root element
|
71
|
path[id_level].keys.append([XpathElem('id', id_, True)])
|
72
|
|
73
|
def is_id(path): return path[0].is_attr and path[0].name == 'id'
|
74
|
|
75
|
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0])
|
76
|
|
77
|
##### Parsing
|
78
|
|
79
|
def expand_abbr(name, repl):
|
80
|
before, abbr, after = name.partition('*')
|
81
|
if abbr != '': name = before+repl+after
|
82
|
return name
|
83
|
|
84
|
_cache = {}
|
85
|
|
86
|
def parse(str_):
|
87
|
try: return _cache[str_]
|
88
|
except KeyError: pass
|
89
|
|
90
|
parser = Parser(str_)
|
91
|
|
92
|
def _path():
|
93
|
is_positive = not parser.str_('!')
|
94
|
|
95
|
tree = []
|
96
|
while True:
|
97
|
# Split path
|
98
|
if parser.str_('{'):
|
99
|
tree[-1].other_branches = _paths()
|
100
|
parser.str_('}', required=True)
|
101
|
tree += tree[-1].other_branches.pop(0) # use first path for now
|
102
|
break # nothing allowed after split path
|
103
|
|
104
|
elem = XpathElem(is_attr=parser.str_('@'), name=_value())
|
105
|
|
106
|
# Keys used to match nodes
|
107
|
if parser.str_('['):
|
108
|
elem.keys = _paths()
|
109
|
parser.str_(']', required=True)
|
110
|
|
111
|
# Attrs created when no matching node exists
|
112
|
if parser.str_(':'):
|
113
|
parser.str_('[', required=True)
|
114
|
elem.attrs = _paths()
|
115
|
parser.str_(']', required=True)
|
116
|
|
117
|
elem.is_ptr = parser.str_('->')
|
118
|
tree.append(elem)
|
119
|
|
120
|
# Lookahead assertion
|
121
|
if parser.str_('('):
|
122
|
parser.str_('/', required=True) # next / is inside ()
|
123
|
path = _path()
|
124
|
parser.str_(')', required=True)
|
125
|
elem.keys.append(path)
|
126
|
tree += path
|
127
|
|
128
|
if not parser.str_('/'): break
|
129
|
|
130
|
tree[0].is_positive = is_positive
|
131
|
|
132
|
# Value
|
133
|
if parser.str_('='):
|
134
|
if parser.str_('$'): # reference (different from a pointer)
|
135
|
value = _path()
|
136
|
else: value = _value() # literal value
|
137
|
set_value(tree, value)
|
138
|
|
139
|
# Expand * abbrs
|
140
|
for i in reversed(xrange(len(tree))):
|
141
|
elem = tree[i]
|
142
|
if elem.is_ptr: offset = 2
|
143
|
else: offset = 1
|
144
|
try: repl = tree[i+offset].name
|
145
|
except IndexError: pass # no replacement elem
|
146
|
else: elem.name = expand_abbr(elem.name, repl)
|
147
|
|
148
|
return tree
|
149
|
|
150
|
def _value():
|
151
|
if parser.str_('"'):
|
152
|
value = parser.re(r'[^"]*')
|
153
|
parser.str_('"', required=True)
|
154
|
else: value = parser.re(r'(?:(?:\w+:)*[\w.*]+)?')
|
155
|
return value
|
156
|
|
157
|
def _paths():
|
158
|
paths = []
|
159
|
while True:
|
160
|
paths.append(_path())
|
161
|
if not parser.str_(','): break
|
162
|
return paths
|
163
|
|
164
|
path = _path()
|
165
|
parser.end()
|
166
|
_cache[str_] = path
|
167
|
return path
|
168
|
|
169
|
##### Querying/creating XML trees
|
170
|
|
171
|
def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
|
172
|
'''Warning: The last_only optimization may put data that should be together
|
173
|
into separate nodes'''
|
174
|
if last_only == None: last_only = create
|
175
|
if limit == None or limit > 1: last_only = False
|
176
|
if util.is_str(xpath): xpath = parse(xpath)
|
177
|
|
178
|
# Handle edge cases
|
179
|
if xpath == []: return [root]
|
180
|
if create and not is_positive(xpath): return []
|
181
|
|
182
|
# Define vars
|
183
|
doc = root.ownerDocument
|
184
|
if allow_rooted and is_rooted(xpath): root = doc.documentElement
|
185
|
elem = xpath[0]
|
186
|
|
187
|
# Find possible matches
|
188
|
children = []
|
189
|
if elem.is_attr:
|
190
|
child = root.getAttributeNode(elem.name)
|
191
|
if child != None: children = [child]
|
192
|
elif is_self(elem): children = [root]
|
193
|
elif is_parent(elem):
|
194
|
parent = xml_dom.parent(root)
|
195
|
if parent == None: return [] # don't try to create doc root's parent
|
196
|
root = parent
|
197
|
children = [root]
|
198
|
else:
|
199
|
children = xml_dom.by_tag_name(root, elem.name,
|
200
|
last_only and (elem.keys == [] or is_instance(elem)))
|
201
|
|
202
|
# Retrieve elem value
|
203
|
value_ = elem.value
|
204
|
if util.is_list(value_): # reference (different from a pointer)
|
205
|
targets = get(root, value_)
|
206
|
try: target = targets[0]
|
207
|
except IndexError:
|
208
|
warnings.warn(UserWarning('XPath reference target missing: '
|
209
|
+str(value_)+'\nXPath: '+str(xpath)))
|
210
|
value_ = None
|
211
|
else: value_ = xml_dom.value(target)
|
212
|
|
213
|
# Check each match
|
214
|
nodes = []
|
215
|
for child in children:
|
216
|
is_match = value_ == None or xml_dom.value(child) == value_
|
217
|
for attr in elem.keys:
|
218
|
if not is_match: break
|
219
|
is_match = ((get(child, attr, False, last_only,
|
220
|
allow_rooted=False) != []) == is_positive(attr))
|
221
|
if is_match:
|
222
|
nodes.append(child)
|
223
|
if limit != None and len(nodes) >= limit: break
|
224
|
|
225
|
# Create node
|
226
|
if nodes == []:
|
227
|
if not create: return []
|
228
|
if elem.is_attr:
|
229
|
root.setAttribute(elem.name, '')
|
230
|
node = root.getAttributeNode(elem.name)
|
231
|
elif util.list_eq_is(children, [root]): node = root
|
232
|
else: node = root.appendChild(doc.createElement(elem.name))
|
233
|
if value_ != None: xml_dom.set_value(node, value_)
|
234
|
nodes.append(node)
|
235
|
|
236
|
path_value = value(xpath)
|
237
|
xpath = xpath[1:] # rest of XPath
|
238
|
|
239
|
next = []
|
240
|
for node in nodes:
|
241
|
# Create attrs
|
242
|
if create:
|
243
|
for attr in elem.keys + elem.attrs:
|
244
|
get(node, attr, create, last_only, allow_rooted=False)
|
245
|
|
246
|
# Follow pointer
|
247
|
if elem.is_ptr:
|
248
|
root = doc.documentElement
|
249
|
xpath = copy.deepcopy(xpath)
|
250
|
id_path = backward_id(xpath[instance_level])
|
251
|
if id_path != None: # backward (child-to-parent) pointer with ID key
|
252
|
id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
|
253
|
set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
|
254
|
else: # forward (parent-to-child) pointer
|
255
|
id_ = xml_dom.value(node)
|
256
|
obj_xpath = obj(xpath) # target object
|
257
|
if id_ == None or get(root, obj_xpath, False, True) == []:
|
258
|
# no target or target keys don't match
|
259
|
if not create: continue
|
260
|
|
261
|
# Use last target object's ID + 1
|
262
|
obj_xpath[-1].keys = [] # just get by tag name
|
263
|
last = get(root, obj_xpath, False, True)
|
264
|
if last != []: id_ = str(int(xml_dom.get_id(last[0])) + 1)
|
265
|
else: id_ = '0'
|
266
|
|
267
|
# Will append if target keys didn't match.
|
268
|
# Use lookahead assertion to avoid this.
|
269
|
xml_dom.set_value(node, id_)
|
270
|
else: last_only = False
|
271
|
set_id(xpath, id_)
|
272
|
else: root = node
|
273
|
next += get(root, xpath, create, last_only, limit, allow_rooted=False)
|
274
|
|
275
|
for branch in elem.other_branches:
|
276
|
branch = copy.deepcopy(branch)
|
277
|
set_value(branch, path_value)
|
278
|
next += get(node, branch, create, last_only, limit,
|
279
|
allow_rooted=False)
|
280
|
|
281
|
return next
|
282
|
|
283
|
def put_obj(root, xpath, id_, has_types, value=None):
|
284
|
if util.is_str(xpath): xpath = parse(xpath)
|
285
|
|
286
|
xpath = copy.deepcopy(xpath) # don't modify input!
|
287
|
set_id(xpath, id_, has_types)
|
288
|
if value != None: set_value(xpath, value)
|
289
|
get(root, xpath, True)
|
290
|
|
291
|
def path2xml(xpath, first_branch=True):
|
292
|
root = xml_dom.create_doc().documentElement
|
293
|
get(root, xpath, True)
|
294
|
return root
|
295
|
|
296
|
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
|