1 |
21
|
aaronmk
|
# XPath-based XML tree manipulation
|
2 |
|
|
|
3 |
22
|
aaronmk
|
from copy import deepcopy
|
4 |
21
|
aaronmk
|
from xml.dom import Node
|
5 |
|
|
|
6 |
|
|
from Parser import Parser
|
7 |
|
|
import xml_util
|
8 |
|
|
|
9 |
|
|
class XpathElem:
|
10 |
|
|
def __init__(self, name, value=None, attrs=None, is_attr=False,
|
11 |
|
|
is_ptr=False):
|
12 |
|
|
if attrs == None: attrs = []
|
13 |
|
|
self.name = name
|
14 |
|
|
self.value = value
|
15 |
|
|
self.attrs = attrs
|
16 |
|
|
self.is_attr = is_attr
|
17 |
|
|
self.is_ptr = is_ptr
|
18 |
|
|
|
19 |
|
|
def __repr__(self):
|
20 |
|
|
str_ = ''
|
21 |
|
|
if self.is_attr: str_ += '@'
|
22 |
25
|
aaronmk
|
str_ += self.name
|
23 |
|
|
if self.attrs != []: str_ += repr(self.attrs)
|
24 |
|
|
if self.value != None: str_ += '='+repr(self.value)
|
25 |
21
|
aaronmk
|
if self.is_ptr: str_ += '->'
|
26 |
|
|
return str_
|
27 |
|
|
|
28 |
|
|
def __eq__(self, other): return self.__dict__ == other.__dict__
|
29 |
|
|
|
30 |
24
|
aaronmk
|
def value(path): return path[-1].value
|
31 |
|
|
|
32 |
22
|
aaronmk
|
def set_value(path, value): path[-1].value = value
|
33 |
|
|
|
34 |
21
|
aaronmk
|
class XpathParser(Parser):
|
35 |
|
|
def _main(self):
|
36 |
|
|
self._match_str('/', required=True)
|
37 |
|
|
return self._path()
|
38 |
|
|
|
39 |
|
|
def _path(self):
|
40 |
|
|
tree = []
|
41 |
25
|
aaronmk
|
fork_idx = None
|
42 |
|
|
elem_idx = 0
|
43 |
21
|
aaronmk
|
while True:
|
44 |
|
|
elem = XpathElem(is_attr=self._match_str('@'), name=self._fields())
|
45 |
|
|
if self._match_str('['):
|
46 |
|
|
elem.attrs = self._attrs()
|
47 |
|
|
self._match_str(']', required=True)
|
48 |
|
|
elem.is_ptr = self._match_str('->')
|
49 |
25
|
aaronmk
|
if not elem.is_ptr and self._match_str('!'): fork_idx = elem_idx
|
50 |
21
|
aaronmk
|
tree.append(elem)
|
51 |
|
|
if not self._match_str('/'): break
|
52 |
25
|
aaronmk
|
elem_idx += 1
|
53 |
|
|
# Add lookahead assertion for rest of path
|
54 |
|
|
if fork_idx != None: tree[fork_idx].attrs.append(tree[fork_idx+1:])
|
55 |
21
|
aaronmk
|
return tree
|
56 |
|
|
|
57 |
|
|
def _fields(self):
|
58 |
|
|
if self._match_str('{'):
|
59 |
|
|
tree = []
|
60 |
|
|
while True:
|
61 |
|
|
tree.append(self._field())
|
62 |
|
|
if not self._match_str(','): break
|
63 |
|
|
self._match_str('}', required=True)
|
64 |
|
|
tree = tuple(tree)
|
65 |
|
|
tree = tree[0] # just use first field for now
|
66 |
|
|
else: tree = self._field()
|
67 |
|
|
return tree
|
68 |
|
|
|
69 |
|
|
def _attrs(self):
|
70 |
|
|
tree = []
|
71 |
|
|
while True:
|
72 |
|
|
path = self._path()
|
73 |
22
|
aaronmk
|
if self._match_str('='): set_value(path, self._value())
|
74 |
21
|
aaronmk
|
tree.append(path)
|
75 |
|
|
if not self._match_str(','): break
|
76 |
|
|
return tree
|
77 |
|
|
|
78 |
|
|
def _field(self):
|
79 |
|
|
return self._name()
|
80 |
|
|
|
81 |
|
|
def _name(self): return self._match_re(r'[\w.]+', required=True)
|
82 |
|
|
|
83 |
|
|
def _value(self): return self._match_re(r'[\w.|]+', required=True)
|
84 |
|
|
|
85 |
26
|
aaronmk
|
instance_level = 1
|
86 |
22
|
aaronmk
|
|
87 |
26
|
aaronmk
|
def obj(path):
|
88 |
|
|
obj_path = deepcopy(path[:instance_level+1])
|
89 |
|
|
obj_path[-1].is_ptr = False # prevent pointer w/o target
|
90 |
|
|
return obj_path
|
91 |
|
|
|
92 |
22
|
aaronmk
|
def set_id(path, id_, has_types=True):
|
93 |
26
|
aaronmk
|
if has_types: id_level = instance_level
|
94 |
21
|
aaronmk
|
else: id_level = 0
|
95 |
|
|
path[id_level].attrs.append([XpathElem('id', id_, is_attr=True)])
|
96 |
|
|
|
97 |
22
|
aaronmk
|
def get(doc, path, create=False, last_only=None, parent=None):
|
98 |
|
|
# Warning: The last_only optimization may split data between multiple nodes
|
99 |
21
|
aaronmk
|
if parent == None: parent = doc.documentElement
|
100 |
22
|
aaronmk
|
if last_only == None: last_only = create
|
101 |
|
|
elem_idx = 0
|
102 |
21
|
aaronmk
|
for elem in path:
|
103 |
22
|
aaronmk
|
# Find possible matches
|
104 |
|
|
children = []
|
105 |
|
|
if elem.is_attr:
|
106 |
|
|
child = parent.getAttributeNode(elem.name)
|
107 |
|
|
if child != None: children = [child]
|
108 |
|
|
elif elem.name == '.': children = [parent]
|
109 |
|
|
else: children = xml_util.by_tag_name(parent, elem.name, last_only)
|
110 |
|
|
|
111 |
|
|
# Check each match
|
112 |
21
|
aaronmk
|
node = None
|
113 |
22
|
aaronmk
|
for child in children:
|
114 |
|
|
is_match = elem.value == None or xml_util.value(child) == elem.value
|
115 |
|
|
for attr in elem.attrs:
|
116 |
|
|
if not is_match: break
|
117 |
|
|
is_match = get(doc, attr, False, last_only, child) != None
|
118 |
|
|
if is_match: node = child; break
|
119 |
|
|
|
120 |
|
|
# Create node
|
121 |
21
|
aaronmk
|
if node == None:
|
122 |
|
|
if not create: return None
|
123 |
|
|
if elem.is_attr:
|
124 |
|
|
parent.setAttribute(elem.name, '')
|
125 |
|
|
node = parent.getAttributeNode(elem.name)
|
126 |
|
|
else: node = parent.appendChild(doc.createElement(elem.name))
|
127 |
22
|
aaronmk
|
if elem.value != None: xml_util.set_value(doc, node, elem.value)
|
128 |
|
|
for attr in elem.attrs: get(doc, attr, create, last_only, node)
|
129 |
|
|
|
130 |
|
|
# Follow pointer
|
131 |
21
|
aaronmk
|
if elem.is_ptr:
|
132 |
25
|
aaronmk
|
path = deepcopy(path[elem_idx+1:]) # rest of path
|
133 |
26
|
aaronmk
|
attrs = path[instance_level].attrs
|
134 |
24
|
aaronmk
|
if len(attrs) >= 1 and value(attrs[0]) == None:
|
135 |
22
|
aaronmk
|
# backward (child-to-parent) pointer with target ID attr
|
136 |
24
|
aaronmk
|
set_value(attrs[0], xml_util.get_id(node))
|
137 |
22
|
aaronmk
|
else: # forward (parent-to-child) pointer
|
138 |
|
|
id_ = xml_util.value(node)
|
139 |
26
|
aaronmk
|
obj_path = obj(path) # target object
|
140 |
25
|
aaronmk
|
if id_ == None or get(doc, obj_path, False, True) == None:
|
141 |
|
|
# no target or target attrs don't match
|
142 |
22
|
aaronmk
|
if not create: return None
|
143 |
25
|
aaronmk
|
|
144 |
22
|
aaronmk
|
# Use last target object's ID + 1
|
145 |
26
|
aaronmk
|
obj_path[-1].attrs = [] # just get by tag name
|
146 |
|
|
last = get(doc, obj_path, False, True)
|
147 |
25
|
aaronmk
|
if last != None: id_ = str(int(xml_util.get_id(last)) + 1)
|
148 |
22
|
aaronmk
|
else: id_ = '0'
|
149 |
25
|
aaronmk
|
|
150 |
|
|
# Will append if target attrs didn't match. Place ! in XPath
|
151 |
|
|
# after element to fork at to avoid this.
|
152 |
22
|
aaronmk
|
xml_util.set_value(doc, node, id_)
|
153 |
23
|
aaronmk
|
else: last_only = False
|
154 |
25
|
aaronmk
|
set_id(path, id_)
|
155 |
|
|
return get(doc, path, create, last_only)
|
156 |
22
|
aaronmk
|
|
157 |
21
|
aaronmk
|
parent = node
|
158 |
22
|
aaronmk
|
elem_idx += 1
|
159 |
21
|
aaronmk
|
return parent
|