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
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
        fork_idx = None
42
        elem_idx = 0
43
        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
            if not elem.is_ptr and self._match_str('!'): fork_idx = elem_idx
50
            tree.append(elem)
51
            if not self._match_str('/'): break
52
            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
        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
            if self._match_str('='): set_value(path, self._value())
74
            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
instance_level = 1
86

    
87
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
def set_id(path, id_, has_types=True):
93
    if has_types: id_level = instance_level
94
    else: id_level = 0
95
    path[id_level].attrs.append([XpathElem('id', id_, is_attr=True)])
96

    
97
def get(doc, path, create=False, last_only=None, parent=None):
98
    # Warning: The last_only optimization may split data between multiple nodes
99
    if parent == None: parent = doc.documentElement
100
    if last_only == None: last_only = create
101
    elem_idx = 0
102
    for elem in path:
103
        # 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
        node = None
113
        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
        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
            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
        if elem.is_ptr:
132
            path = deepcopy(path[elem_idx+1:]) # rest of path
133
            attrs = path[instance_level].attrs
134
            if len(attrs) >= 1 and value(attrs[0]) == None:
135
                # backward (child-to-parent) pointer with target ID attr
136
                set_value(attrs[0], xml_util.get_id(node))
137
            else: # forward (parent-to-child) pointer
138
                id_ = xml_util.value(node)
139
                obj_path = obj(path) # target object
140
                if id_ == None or get(doc, obj_path, False, True) == None:
141
                    # no target or target attrs don't match
142
                    if not create: return None
143
                    
144
                    # Use last target object's ID + 1
145
                    obj_path[-1].attrs = [] # just get by tag name
146
                    last = get(doc, obj_path, False, True)
147
                    if last != None: id_ = str(int(xml_util.get_id(last)) + 1)
148
                    else: id_ = '0'
149
                    
150
                    # Will append if target attrs didn't match. Place ! in XPath
151
                    # after element to fork at to avoid this.
152
                    xml_util.set_value(doc, node, id_)
153
                else: last_only = False
154
                set_id(path, id_)
155
            return get(doc, path, create, last_only)
156
        
157
        parent = node
158
        elem_idx += 1
159
    return parent
(9-9/9)