Package pyx12 :: Module x12xml_simple
[hide private]

Source Code for Module pyx12.x12xml_simple

  1  ###################################################################### 
  2  # Copyright (c) 2001-2008 Kalamazoo Community Mental Health Services, 
  3  #   John Holland <jholland@kazoocmh.org> <john@zoner.org> 
  4  # All rights reserved. 
  5  # 
  6  # This software is licensed as described in the file LICENSE.txt, which 
  7  # you should have received as part of this distribution. 
  8  # 
  9  ###################################################################### 
 10   
 11  #    $Id: x12xml_simple.py 1286 2008-06-11 21:37:34Z johnholland $ 
 12   
 13  """ 
 14  Create a XML rendering of the X12 document 
 15  """ 
 16   
 17  import os.path 
 18  import logging 
 19   
 20  # Intrapackage imports 
 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   
28 -class x12xml_simple(x12xml):
29 - def __init__(self, fd, dtd_urn=None):
30 x12xml.__init__(self, fd, u"x12simple", dtd_urn) 31 self.last_path = []
32
33 - def __del__(self):
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) # Get enclosing loop 49 # check path for new loops to be added 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 # Do not try to ouput for invalid or empty elements 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() #end composite 79 elif child_node.is_element(): 80 if seg_data.get_value('%02i' % (i+1)) == '': 81 pass 82 #self.writer.empty(u"ele", attrs={u'id': child_node.id}) 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() #end segment 89 self.last_path = cur_path
90
91 - def _get_loop_info(self, loop_id):
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
100 - def _get_seg_info(self, seg_id):
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
109 - def _get_comp_info(self, comp_id):
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
118 - def _get_ele_info(self, ele_id):
119 """ 120 Override element node value 121 """ 122 name = u'ele' 123 attrib = {} 124 attrib[u'id'] = ele_id 125 return (name, attrib)
126
127 - def _get_subele_info(self, subele_id):
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