Project

General

Profile

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, allow_rooted=True):
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
    # Handle edge cases
177
    if xpath == []: return [root]
178
    if create and not is_positive(xpath): return []
179
    
180
    # Define vars
181
    doc = root.ownerDocument
182
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
183
    elem = xpath[0]
184
    
185
    # Find possible matches
186
    children = []
187
    if elem.is_attr:
188
        child = root.getAttributeNode(elem.name)
189
        if child != None: children = [child]
190
    elif is_self(elem): children = [root]
191
    elif is_parent(elem):
192
        parent = xml_dom.parent(root)
193
        if parent != None: children = [parent]
194
        else: return [] # don't try to create the document root's parent
195
    else:
196
        children = xml_dom.by_tag_name(root, elem.name,
197
        last_only and (elem.keys == [] or is_instance(elem)))
198
    
199
    # Check each match
200
    nodes = []
201
    for child in children:
202
        is_match = elem.value == None or xml_dom.value(child) == elem.value
203
        for attr in elem.keys:
204
            if not is_match: break
205
            is_match = ((get(child, attr, False, last_only) != [])
206
                == is_positive(attr))
207
        if is_match:
208
            nodes.append(child)
209
            if limit != None and len(nodes) >= limit: break
210
    
211
    # Create node
212
    if nodes == []:
213
        if not create: return []
214
        if elem.is_attr:
215
            root.setAttribute(elem.name, '')
216
            node = root.getAttributeNode(elem.name)
217
        else: node = root.appendChild(doc.createElement(elem.name))
218
        if elem.value != None: xml_dom.set_value(node, elem.value)
219
        nodes.append(node)
220
    
221
    value_ = value(xpath)
222
    xpath = xpath[1:] # rest of XPath
223
    
224
    next = []
225
    for node in nodes:
226
        # Create attrs
227
        if create:
228
            for attr in elem.keys + elem.attrs:
229
                get(node, attr, create, last_only)
230
        
231
        # Follow pointer
232
        if elem.is_ptr:
233
            root = doc.documentElement
234
            xpath = copy.deepcopy(xpath)
235
            id_path = backward_id(xpath[instance_level])
236
            if id_path != None: # backward (child-to-parent) pointer with ID key
237
                id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
238
                set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
239
            else: # forward (parent-to-child) pointer
240
                id_ = xml_dom.value(node)
241
                obj_xpath = obj(xpath) # target object
242
                if id_ == None or get(root, obj_xpath, False, True) == []:
243
                    # no target or target keys don't match
244
                    if not create: continue
245
                    
246
                    # Use last target object's ID + 1
247
                    obj_xpath[-1].keys = [] # just get by tag name
248
                    last = get(root, obj_xpath, False, True)
249
                    if last != []: id_ = str(int(xml_dom.get_id(last[0])) + 1)
250
                    else: id_ = '0'
251
                    
252
                    # Will append if target keys didn't match.
253
                    # Use lookahead assertion to avoid this.
254
                    xml_dom.set_value(node, id_)
255
                else: last_only = False
256
                set_id(xpath, id_)
257
        else: root = node
258
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
259
        
260
        for branch in elem.other_branches:
261
            branch = copy.deepcopy(branch)
262
            set_value(branch, value_)
263
            next += get(node, branch, create, last_only, limit,
264
                allow_rooted=False)
265
    
266
    return next
267

    
268
def put_obj(root, xpath, id_, has_types, value=None):
269
    if util.is_str(xpath): xpath = parse(xpath)
270
    
271
    xpath = copy.deepcopy(xpath) # don't modify input!
272
    set_id(xpath, id_, has_types)
273
    if value != None: set_value(xpath, value)
274
    get(root, xpath, True)
275

    
276
def path2xml(xpath, first_branch=True):
277
    root = xml_dom.create_doc().documentElement
278
    get(root, xpath, True)
279
    return root
280

    
281
def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
(16-16/16)