Project

General

Profile

1
# XPath-based XML tree manipulation
2

    
3
from copy import deepcopy
4
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
        str_ += self.name
23
        if self.attrs != []: str_ += repr(self.attrs)
24
        if self.value != None: str_ += '='+repr(self.value)
25
        if self.is_ptr: str_ += '->'
26
        return str_
27
    
28
    def __eq__(self, other): return self.__dict__ == other.__dict__
29

    
30
def value(path): return path[-1].value
31

    
32
def set_value(path, value): path[-1].value = value
33

    
34
def backward_id(elem):
35
    if len(elem.attrs) >= 1 and value(elem.attrs[0]) == None:
36
        return elem.attrs[0]
37
    else: return None
38

    
39
class XpathParser(Parser):
40
    def _main(self):
41
        self._match_str('/') # optional leading /
42
        return self._path()
43
    
44
    def _path(self):
45
        tree = []
46
        while True:
47
            elem = XpathElem(is_attr=self._match_str('@'), name=self._fields())
48
            
49
            # Attrs
50
            if self._match_str('['):
51
                elem.attrs = []
52
                while True:
53
                    path = self._path()
54
                    if self._match_str('='): set_value(path, self._value())
55
                    elem.attrs.append(path)
56
                    if not self._match_str(','): break
57
                self._match_str(']', required=True)
58
            
59
            elem.is_ptr = self._match_str('->')
60
            tree.append(elem)
61
            
62
            # Lookahead assertion
63
            if self._match_str('('):
64
                self._match_str('/', required=True) # next / is inside ()
65
                path = self._path()
66
                self._match_str(')', required=True)
67
                elem.attrs.append(path)
68
                tree += path
69
            
70
            if not self._match_str('/'): break
71
        
72
        # Expand * abbrs
73
        elem_idx = 0
74
        for elem in tree:
75
            id_ = backward_id(elem)
76
            if id_ != None: elem = id_[0]; offset = -2
77
            elif elem.is_ptr: offset = 2
78
            else: offset = 1
79
            before, abbr, after = elem.name.partition('*')
80
            if abbr != '':
81
                try: elem.name = before+tree[elem_idx+offset].name+after
82
                except IndexError: pass # no replacement elem
83
            elem_idx += 1
84
        
85
        return tree
86
    
87
    def _fields(self):
88
        if self._match_str('{'):
89
            tree = []
90
            while True:
91
                tree.append(self._field())
92
                if not self._match_str(','): break
93
            self._match_str('}', required=True)
94
            tree = tuple(tree)
95
            tree = tree[0] # just use first field for now
96
        else: tree = self._field()
97
        return tree
98
    
99
    def _field(self):
100
        return self._name()
101
    
102
    def _name(self): return self._match_re(r'[\w.*]+', required=True)
103
    
104
    def _value(self): return self._match_re(r'[\w.|]+', required=True)
105

    
106
instance_level = 1
107

    
108
def obj(path):
109
    obj_path = deepcopy(path[:instance_level+1])
110
    obj_path[-1].is_ptr = False # prevent pointer w/o target
111
    return obj_path
112

    
113
def set_id(path, id_, has_types=True):
114
    if has_types: id_level = instance_level
115
    else: id_level = 0
116
    path[id_level].attrs.append([XpathElem('id', id_, is_attr=True)])
117

    
118
def get(doc, path, create=False, last_only=None, parent=None):
119
    # Warning: The last_only optimization may put data that should be together
120
    # into separate nodes
121
    if parent == None: parent = doc.documentElement
122
    if last_only == None: last_only = create
123
    elem_idx = 0
124
    for elem in path:
125
        # Find possible matches
126
        children = []
127
        if elem.is_attr:
128
            child = parent.getAttributeNode(elem.name)
129
            if child != None: children = [child]
130
        elif elem.name == '.': children = [parent]
131
        else: children = xml_util.by_tag_name(parent, elem.name, last_only)
132
        
133
        # Check each match
134
        node = None
135
        for child in children:
136
            is_match = elem.value == None or xml_util.value(child) == elem.value
137
            for attr in elem.attrs:
138
                if not is_match: break
139
                is_match = get(doc, attr, False, last_only, child) != None
140
            if is_match: node = child; break
141
        
142
        # Create node
143
        if node == None:
144
            if not create: return None
145
            if elem.is_attr:
146
                parent.setAttribute(elem.name, '')
147
                node = parent.getAttributeNode(elem.name)
148
            else: node = parent.appendChild(doc.createElement(elem.name))
149
            if elem.value != None: xml_util.set_value(doc, node, elem.value)
150
            for attr in elem.attrs: get(doc, attr, create, last_only, node)
151
        
152
        # Follow pointer
153
        if elem.is_ptr:
154
            path = deepcopy(path[elem_idx+1:]) # rest of path
155
            id_elem = backward_id(path[instance_level])
156
            if id_elem != None:
157
                # backward (child-to-parent) pointer with target ID attr
158
                set_value(id_elem, xml_util.get_id(node))
159
            else: # forward (parent-to-child) pointer
160
                id_ = xml_util.value(node)
161
                obj_path = obj(path) # target object
162
                if id_ == None or get(doc, obj_path, False, True) == None:
163
                    # no target or target attrs don't match
164
                    if not create: return None
165
                    
166
                    # Use last target object's ID + 1
167
                    obj_path[-1].attrs = [] # just get by tag name
168
                    last = get(doc, obj_path, False, True)
169
                    if last != None: id_ = str(int(xml_util.get_id(last)) + 1)
170
                    else: id_ = '0'
171
                    
172
                    # Will append if target attrs didn't match. Place ! in XPath
173
                    # after element to fork at to avoid this.
174
                    xml_util.set_value(doc, node, id_)
175
                else: last_only = False
176
                set_id(path, id_)
177
            return get(doc, path, create, last_only)
178
        
179
        parent = node
180
        elem_idx += 1
181
    return parent
(10-10/10)