# Simple wrapper for FCD XML data in field type


import osg2
try:
   import xml.etree.ElementTree as ET
except:
   import elementtree.ElementTree as ET

class FcdReflector(object):
   def __init__(self, fctype, mergeParents=False):      
      """
        @param mergeParents: If true, merge in the parent's fields.
      """
      self.fc_type = fctype
      xml_data = fctype.getFcdXML()
      if xml_data and xml_data != "":
         self.element = ET.fromstring(xml_data)
      else:
         self.element = None
      
      if self.element and mergeParents:
         fc_type = fctype.getParent()
         while fc_type:
            xml_data = fc_type.getFcdXML()
            if xml_data and xml_data != "":
               new_elt = ET.fromstring(xml_data)
               if new_elt:
                  for n in new_elt:
                     self.element.append(n)
            fc_type = fc_type.getParent()
         

   def hasData(self):
      """ Return true if we wrap XML data successfully.
          Note: Some object types may not have an fcd file as a backend so they
          may return false.
      """
      return self.element != None
   
   def fields(self):
      """ return wrappers for all child fields. """
      if len(self.element) == 0:
         return []
      else:
         return [FieldReflector(f,self) for f in self.element.findall("Field")]
   
   def getField(self, name):
      """ Return a wrapper around the first field named name. """
      for f in self.element.findall("Field"):
         if name == f.attrib["name"]:
            return FieldReflector(f,self)
      return None
   
   def getAttrib(self):
      return self.element.attrib
   attrib = property(getAttrib, doc='Return the element.attrib property')
      
   def getName(self):
      return self.element.get("name")   
   def getType(self):
      return self.element.get("type")
   
   def getDocumentation(self):
      return self.element.text
   def getCardinality(self):
      return self.element.get("cardinality")
   def getVisibility(self):
      return self.element.get("visibility")
   def getAccess(self):
      return self.element.get("access")
   

class FieldReflector(FcdReflector):
   def __init__(self, fieldElement, parent):
      self.parent = parent
      self.element = fieldElement
