1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Create a XML rendering of the X12 document
15 Uses node IDs as the tag names
16 Append ID value for non-unique segments IDs
17 """
18
19 import os.path
20 import logging
21
22
23 from errors import *
24 from x12xml import x12xml
25 from xmlwriter import XMLWriter
26 from map_walker import pop_to_parent_loop
27
28 logger = logging.getLogger('pyx12.x12xml.idtagqual')
29
32 x12xml.__init__(self, fd, u"x12idtagqual", dtd_urn)
33 self.last_path = []
34
36 while len(self.writer) > 0:
37 self.writer.pop()
38
39 - def seg(self, seg_node, seg_data):
40 """
41 Generate XML for the segment data and matching map node
42
43 @param seg_node: Map Node
44 @type seg_node: L{node<map_if.x12_node>}
45 @param seg_data: Segment object
46 @type seg_data: L{segment<segment.Segment>}
47 """
48 if not seg_node.is_segment():
49 raise EngineError, 'Node must be a segment'
50 parent = pop_to_parent_loop(seg_node)
51
52 cur_path = self._path_list(parent.get_path())
53 if self.last_path != cur_path:
54 last_path = self.last_path
55 match_idx = self._get_path_match_idx(last_path, cur_path)
56 root_path = self._path_list(os.path.commonprefix(
57 ['/'.join(cur_path), '/'.join(last_path)]))
58 if seg_node.is_first_seg_in_loop() and root_path==cur_path:
59 match_idx -= 1
60 for i in range(len(last_path)-1, match_idx-1, -1):
61 self.writer.pop()
62 for i in range(match_idx, len(cur_path)):
63 (xname, attrib) = self._get_loop_info(cur_path[i])
64 self.writer.push(xname, attrib)
65 seg_node_id = self._get_node_id(seg_node, parent, seg_data)
66 (xname, attrib) = self._get_seg_info(seg_node_id)
67 self.writer.push(xname, attrib)
68 for i in range(len(seg_data)):
69 child_node = seg_node.get_child_node_by_idx(i)
70 if child_node.usage == 'N' or seg_data.get('%02i' % (i+1)).is_empty():
71 pass
72 elif child_node.is_composite():
73 (xname, attrib) = self._get_comp_info(seg_node_id)
74 self.writer.push(xname, attrib)
75 comp_data = seg_data.get('%02i' % (i+1))
76 for j in range(len(comp_data)):
77 subele_node = child_node.get_child_node_by_idx(j)
78 (xname, attrib) = self._get_subele_info(subele_node.id)
79 self.writer.elem(xname, comp_data[j].get_value(), attrib)
80 self.writer.pop()
81 elif child_node.is_element():
82 if seg_data.get_value('%02i' % (i+1)) == '':
83 pass
84
85 else:
86 (xname, attrib) = self._get_ele_info(child_node.id)
87 self.writer.elem(xname, seg_data.get_value('%02i' % (i+1)), attrib)
88 else:
89 raise EngineError, 'Node must be a either an element or a composite'
90 self.writer.pop()
91 self.last_path = cur_path
92
94 """
95 Get a unique node ID string
96 Override base class function
97 @param seg_node: L{node<map_if.segment_if>}
98 @param parent: L{node<map_if.segment_if>}
99 @param seg_data: L{node<segment.Segment>}
100 @return: Unique node representation
101 @rtype: string
102 """
103 if len(parent.pos_map[seg_node.pos]) > 1:
104 id_val = seg_data.get_value('01')
105 if seg_node.children[0].is_element() \
106 and seg_node.children[0].get_data_type() == 'ID' \
107 and len(seg_node.children[0].valid_codes) > 0 \
108 and id_val in seg_node.children[0].valid_codes:
109 return seg_node.id + '_' + id_val
110 elif seg_node.children[0].is_composite() \
111 and seg_node.children[0].children[0].get_data_type() == 'ID' \
112 and len(seg_node.children[0].children[0].valid_codes) > 0 \
113 and id_val in seg_node.children[0].children[0].valid_codes:
114 return seg_node.id + '_' + id_val
115 return seg_node.id
116
118 """
119 Override loop node value
120 """
121 loop_name = 'L' + loop_id
122 attrib = {}
123 return (loop_name, attrib)
124
126 """
127 Override segment node value
128 """
129 seg_name = seg_id
130 attrib = {}
131 return (seg_name, attrib)
132
134 """
135 Override composite node value
136 """
137 comp_name = comp_id
138 attrib = {}
139 return (comp_name, attrib)
140
142 """
143 Override element node value
144 """
145 name = ele_id
146 attrib = {}
147 return (name, attrib)
148
150 """
151 Override sub-element node value
152 """
153 name = subele_id
154 attrib = {}
155 return (name, attrib)
156