1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Create a XML rendering of the X12 document
15 """
16
17 import os.path
18 import logging
19
20
21 from errors import *
22 from x12xml import x12xml
23 from xmlwriter import XMLWriter
24 from map_walker import pop_to_parent_loop
25
26 logger = logging.getLogger('pyx12.x12xml.simple')
27
30 x12xml.__init__(self, fd, u"x12simple", dtd_urn)
31 self.last_path = []
32
34 while len(self.writer) > 0:
35 self.writer.pop()
36
37 - def seg(self, seg_node, seg_data):
38 """
39 Generate XML for the segment data and matching map node
40
41 @param seg_node: Map Node
42 @type seg_node: L{node<map_if.x12_node>}
43 @param seg_data: Segment object
44 @type seg_data: L{segment<segment.Segment>}
45 """
46 if not seg_node.is_segment():
47 raise EngineError, 'Node must be a segment'
48 parent = pop_to_parent_loop(seg_node)
49
50 cur_path = self._path_list(parent.get_path())
51 if self.last_path != cur_path:
52 last_path = self.last_path
53 match_idx = self._get_path_match_idx(last_path, cur_path)
54 root_path = self._path_list(os.path.commonprefix(
55 ['/'.join(cur_path), '/'.join(last_path)]))
56 if seg_node.is_first_seg_in_loop() and root_path==cur_path:
57 match_idx -= 1
58 for i in range(len(last_path)-1, match_idx-1, -1):
59 self.writer.pop()
60 for i in range(match_idx, len(cur_path)):
61 (xname, attrib) = self._get_loop_info(cur_path[i])
62 self.writer.push(xname, attrib)
63 seg_node_id = self._get_node_id(seg_node, parent, seg_data)
64 (xname, attrib) = self._get_seg_info(seg_node_id)
65 self.writer.push(xname, attrib)
66 for i in range(len(seg_data)):
67 child_node = seg_node.get_child_node_by_idx(i)
68 if child_node.usage == 'N' or seg_data.get('%02i' % (i+1)).is_empty():
69 pass
70 elif child_node.is_composite():
71 (xname, attrib) = self._get_comp_info(seg_node_id)
72 self.writer.push(xname, attrib)
73 comp_data = seg_data.get('%02i' % (i+1))
74 for j in range(len(comp_data)):
75 subele_node = child_node.get_child_node_by_idx(j)
76 (xname, attrib) = self._get_subele_info(subele_node.id)
77 self.writer.elem(xname, comp_data[j].get_value(), attrib)
78 self.writer.pop()
79 elif child_node.is_element():
80 if seg_data.get_value('%02i' % (i+1)) == '':
81 pass
82
83 else:
84 (xname, attrib) = self._get_ele_info(child_node.id)
85 self.writer.elem(xname, seg_data.get_value('%02i' % (i+1)), attrib)
86 else:
87 raise EngineError, 'Node must be a either an element or a composite'
88 self.writer.pop()
89 self.last_path = cur_path
90
92 """
93 Override loop node value
94 """
95 loop_name = u"loop"
96 attrib = {}
97 attrib[u'id'] = loop_id
98 return (loop_name, attrib)
99
101 """
102 Override segment node value
103 """
104 seg_name = u"seg"
105 attrib = {}
106 attrib[u'id'] = seg_id
107 return (seg_name, attrib)
108
110 """
111 Override composite node value
112 """
113 comp_name = u"comp"
114 attrib = {}
115 attrib[u'id'] = comp_id
116 return (comp_name, attrib)
117
119 """
120 Override element node value
121 """
122 name = u'ele'
123 attrib = {}
124 attrib[u'id'] = ele_id
125 return (name, attrib)
126
128 """
129 Override sub-element node value
130 """
131 name = u'subele'
132 attrib = {}
133 attrib[u'id'] = subele_id
134 return (name, attrib)
135