1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Interface to create a XML rendering of the X12 document
15 """
16
17
18 from errors import *
19 from xmlwriter import XMLWriter
20 from map_walker import pop_to_parent_loop
21
24 self.writer = XMLWriter(fd)
25 if dtd_urn:
26 self.writer.doctype(
27 type, u"-//J Holland//DTD XML X12 Document Conversion1.0//EN//XML",
28 u"%s" % (dtd_urn))
29 self.writer.push(type)
30
33
34 - def seg(self, seg_node, seg_data):
35 """
36 Generate XML for the segment data and matching map node
37
38 @param seg_node: Map Node
39 @type seg_node: L{node<map_if.x12_node>}
40 @param seg_data: Segment object
41 @type seg_data: L{segment<segment.Segment>}
42 """
43 if not seg_node.is_segment():
44 raise EngineError, 'Node must be a segment'
45 parent = pop_to_parent_loop(seg_node)
46
47 cur_path = self._path_list(parent.get_path())
48 if self.last_path != cur_path:
49 last_path = self.last_path
50 match_idx = self._get_path_match_idx(last_path, cur_path)
51 root_path = self._path_list(os.path.commonprefix(
52 ['/'.join(cur_path), '/'.join(last_path)]))
53 if seg_node.is_first_seg_in_loop() and root_path==cur_path:
54 match_idx -= 1
55 for i in range(len(last_path)-1, match_idx-1, -1):
56 self.writer.pop()
57 for i in range(match_idx, len(cur_path)):
58 (xname, attrib) = self._get_loop_info(cur_path[i])
59 self.writer.push(xname, attrib)
60 seg_node_id = self._get_node_id(seg_node, parent, seg_data)
61 (xname, attrib) = self._get_seg_info(seg_node_id)
62 self.writer.push(xname, attrib)
63 for i in range(len(seg_data)):
64 child_node = seg_node.get_child_node_by_idx(i)
65 if child_node.usage == 'N' or seg_data.get('%02i' % (i+1)).is_empty():
66 pass
67 elif child_node.is_composite():
68 (xname, attrib) = self._get_comp_info(seg_node_id)
69 self.writer.push(xname, attrib)
70 comp_data = seg_data.get('%02i' % (i+1))
71 for j in range(len(comp_data)):
72 subele_node = child_node.get_child_node_by_idx(j)
73 (xname, attrib) = self._get_subele_info(subele_node.id)
74 self.writer.elem(xname, comp_data[j].get_value(), attrib)
75 self.writer.pop()
76 elif child_node.is_element():
77 if seg_data.get_value('%02i' % (i+1)) == '':
78 pass
79
80 else:
81 (xname, attrib) = self._get_ele_info(child_node.id)
82 self.writer.elem(xname, seg_data.get_value('%02i' % (i+1)), attrib)
83 else:
84 raise EngineError, 'Node must be a either an element or a composite'
85 self.writer.pop()
86 self.last_path = cur_path
87
88
90 """
91 Get list of path nodes from path string
92 @rtype: list
93 """
94 return filter(lambda x: x!='', path_str.split('/'))
95
97 """
98 Get the index of the last matching path nodes
99 """
100 match_idx = 0
101 for i in range(min(len(cur_path), len(last_path))):
102 if cur_path[i] != last_path[i]:
103 break
104 match_idx += 1
105 return match_idx
106
107 - def _get_node_id(self, seg_node, parent=None, seg_data=None):
108 """
109 Base node id function
110 """
111 return seg_node.id
112
114 """
115 Base loop node value
116 """
117 loop_name = loop_id
118 attrib = {}
119 return (loop_name, attrib)
120
122 """
123 Base segment node value
124 """
125 seg_name = seg_id
126 attrib = {}
127 return (seg_name, attrib)
128
130 """
131 Base composite node value
132 """
133 comp_name = comp_id
134 attrib = {}
135 return (comp_name, attrib)
136
138 """
139 Base element node value
140 """
141 name = ele_id
142 attrib = {}
143 return (name, attrib)
144
146 """
147 Base sub-element node value
148 """
149 name = subele_id
150 attrib = {}
151 return (name, attrib)
152