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