5 import sys, string, xml.dom.minidom
6 from oils.const import OILS_NS_OBJ, OILS_NS_PERSIST, OILS_NS_REPORTER
11 global __global_parser
12 if __global_parser: return # no need to re-parse the IDL
13 idlParser = oilsIDLParser();
14 idlParser.setIDL(osrf.set.get('IDL'))
16 __global_parser = idlParser
18 def oilsGetIDLParser():
19 global __global_parser
20 return __global_parser
22 class oilsIDLParser(object):
27 def setIDL(self, file):
28 osrf.log.log_info("setting IDL file to " + str(file))
31 def __getAttr(self, node, name, ns=None):
32 """ Find the attribute value on a given node
33 Namespace is ignored for now..
34 not sure if minidom has namespace support.
36 for (k, v) in node.attributes.items():
42 """Parses the IDL file and builds class objects"""
44 doc = xml.dom.minidom.parse(self.idlFile)
45 root = doc.childNodes[0]
47 for child in root.childNodes:
49 if child.nodeType == child.ELEMENT_NODE:
51 # -----------------------------------------------------------------------
52 # 'child' is the main class node for a fieldmapper class.
53 # It has 'fields' and 'links' nodes as children.
54 # -----------------------------------------------------------------------
56 id = self.__getAttr(child, 'id')
57 self.IDLObject[id] = {}
58 obj = self.IDLObject[id]
61 obj['controller'] = self.__getAttr(child, 'controller')
62 obj['fieldmapper'] = self.__getAttr(child, 'oils_obj:fieldmapper', OILS_NS_OBJ)
63 obj['virtual'] = self.__getAttr(child, 'oils_perist:virtual', OILS_NS_PERSIST)
64 obj['rpt_label'] = self.__getAttr(child, 'reporter:label', OILS_NS_REPORTER)
65 obj['tablename'] = self.__getAttr(child, 'oils_persist:tablename', OILS_NS_REPORTER)
68 for classNode in child.childNodes:
69 if classNode.nodeType == classNode.ELEMENT_NODE:
70 if classNode.nodeName == 'fields':
71 keys = self.parseFields(id, classNode)
73 osrf.net_obj.register_hint(id, keys, 'array')
78 def parseFields(self, cls, fields):
79 """Takes the fields node and parses the included field elements"""
82 idlobj = self.IDLObject[cls]
84 for field in fields.childNodes:
85 if field.nodeType == field.ELEMENT_NODE:
88 for field in fields.childNodes:
90 if field.nodeType == fields.ELEMENT_NODE:
91 name = self.__getAttr(field, 'name')
92 position = int(self.__getAttr(field, 'oils_obj:array_position', OILS_NS_OBJ))
98 osrf.log.log_error("parseFields(): position out of range. pos=%d : key-size=%d" % (position, len(keys)))
101 virtual = self.__getAttr(field, 'oils_persist:virtual', OILS_NS_PERSIST)
102 obj['rpt_label'] = self.__getAttr(field, 'reporter:label', OILS_NS_REPORTER)
103 obj['rpt_dtype'] = self.__getAttr(field, 'reporter:datatype', OILS_NS_REPORTER)
104 obj['rpt_select'] = self.__getAttr(field, 'reporter:selector', OILS_NS_REPORTER)
106 if virtual == string.lower('true'):
107 obj['virtual'] = True
109 obj['virtual'] = False
111 idlobj['fields'].append(obj)