1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Provides classes for handling soap multirefs.
19 """
20
21 from logging import getLogger
22 from suds import *
23 from suds.sax.element import Element
24
25 log = getLogger(__name__)
26
27 soapenc = (None, 'http://schemas.xmlsoap.org/soap/encoding/')
28
30 """
31 Resolves and replaces multirefs.
32 @ivar nodes: A list of non-multiref nodes.
33 @type nodes: list
34 @ivar catalog: A dictionary of multiref nodes by id.
35 @type catalog: dict
36 """
37
39 self.nodes = []
40 self.catalog = {}
41
43 """
44 Process the specified soap envelope body and replace I{multiref} node
45 references with the contents of the referenced node.
46 @param body: A soap envelope body node.
47 @type body: L{Element}
48 @return: The processed I{body}
49 @rtype: L{Element}
50 """
51 self.nodes = []
52 self.catalog = {}
53 self.build_catalog(body)
54 self.update(body)
55 body.children = self.nodes
56 return body
57
59 """
60 Update the specified I{node} by replacing the I{multiref} references with
61 the contents of the referenced nodes and remove the I{href} attribute.
62 @param node: A node to update.
63 @type node: L{Element}
64 @return: The updated node
65 @rtype: L{Element}
66 """
67 self.replace_references(node)
68 for c in node.children:
69 self.update(c)
70 return node
71
73 """
74 Replacing the I{multiref} references with the contents of the
75 referenced nodes and remove the I{href} attribute. Warning: since
76 the I{ref} is not cloned,
77 @param node: A node to update.
78 @type node: L{Element}
79 """
80 href = node.getAttribute('href')
81 if href is None:
82 return
83 id = href.getValue()
84 ref = self.catalog.get(id)
85 if ref is None:
86 log.error('soap multiref: %s, not-resolved', id)
87 return
88 node.append(ref.children)
89 node.setText(ref.getText())
90 for a in ref.attributes:
91 if a.name != 'id':
92 node.append(a)
93 node.remove(href)
94
96 """
97 Create the I{catalog} of multiref nodes by id and the list of
98 non-multiref nodes.
99 @param body: A soap envelope body node.
100 @type body: L{Element}
101 """
102 for child in body.children:
103 if self.soaproot(child):
104 self.nodes.append(child)
105 id = child.get('id')
106 if id is None: continue
107 key = '#%s' % id
108 self.catalog[key] = child
109
111 """
112 Get whether the specified I{node} is a soap encoded root.
113 This is determined by examining @soapenc:root='1'.
114 The node is considered to be a root when the attribute
115 is not specified.
116 @param node: A node to evaluate.
117 @type node: L{Element}
118 @return: True if a soap encoded root.
119 @rtype: bool
120 """
121 root = node.getAttribute('root', ns=soapenc)
122 if root is None:
123 return True
124 else:
125 return ( root.value == '1' )
126