Resolves and replaces multirefs. @ivar nodes: A list of non-multiref nodes. @type nodes: list @ivar catalog: A dictionary of multiref nodes by id. @type catalog: dict
| 26 | |
| 27 | |
| 28 | class MultiRef: |
| 29 | """ |
| 30 | Resolves and replaces multirefs. |
| 31 | @ivar nodes: A list of non-multiref nodes. |
| 32 | @type nodes: list |
| 33 | @ivar catalog: A dictionary of multiref nodes by id. |
| 34 | @type catalog: dict |
| 35 | """ |
| 36 | |
| 37 | def __init__(self): |
| 38 | self.nodes = [] |
| 39 | self.catalog = {} |
| 40 | |
| 41 | def process(self, body): |
| 42 | """ |
| 43 | Process the specified soap envelope body and replace I{multiref} node |
| 44 | references with the contents of the referenced node. |
| 45 | @param body: A soap envelope body node. |
| 46 | @type body: L{Element} |
| 47 | @return: The processed I{body} |
| 48 | @rtype: L{Element} |
| 49 | """ |
| 50 | self.nodes = [] |
| 51 | self.catalog = {} |
| 52 | self.build_catalog(body) |
| 53 | self.update(body) |
| 54 | body.children = self.nodes |
| 55 | return body |
| 56 | |
| 57 | def update(self, node): |
| 58 | """ |
| 59 | Update the specified I{node} by replacing the I{multiref} references |
| 60 | with the contents of the referenced nodes and remove the I{href} |
| 61 | 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 | |
| 72 | def replace_references(self, node): |
| 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: |