Package rubacon :: Package parsers :: Module ModelParser
[hide private]
[frames] | no frames]

Source Code for Module rubacon.parsers.ModelParser

  1  # This parser reads the definitions from the XMI data. 
  2  # 
  3  # TODO: we need to check with which case tools it will work. Currently we use ArgoUML and its XMI export for the tool. 
  4   
  5  from ..backend.XMLStorage import XMLStorage 
  6   
  7  from Ft.Xml import Parse 
  8  from Ft.Xml.XPath.Context import Context 
  9  from Ft.Xml.XPath import Evaluate 
 10   
 11  import marshal 
 12  import sys 
 13   
 14  # TODO: make the method static class methods 
15 -class ModelParser(object):
16 # @AttributeType string 17 # The default storage for models. 18 ___STORAGE = 'data/db.model' 19 20 # @ReturnType void 21 # @ParamType aFilename string 22 # Parse the XMI file to the given collection. The collection normally is db.model
23 - def parse(self, aFilename):
24 """Parse the given XMI to the storage""" 25 26 ucr = UMLClassReader() 27 classDefs = ucr.parse(aFilename) 28 29 # -> Converting this to XML is mere overkill 30 # -> and only good for exploit processor capacity! 31 # -> We marshal this to a file 32 ouf = open(self.___STORAGE, 'wb') 33 marshal.dump(classDefs, ouf) 34 ouf.close()
35
36 -class UMLClassReader(object):
37 """Extract class definitions from a XMI file""" 38 39 # TODO: Extract this is a config parameter, so that it can 40 # easily be adapted for a variety of case tools. 41 # Known to work so far: ArgoUML, VP-UML 42 ___NAMESPACE = u'org.omg.xmi.namespace.UML' 43
44 - def __init__(self):
45 self.uml_classes = []
46
47 - def parse(self, name):
48 """Extract the list of classes""" 49 doc = Parse(name) 50 # Namespace declaration: 51 NSS = {u'Uml': self.___NAMESPACE} 52 ctx = Context(doc, processorNss=NSS) 53 defList = [] 54 classNameDict = {} # Save the id -> name mapping for the dep-graph 55 umlDefs = Evaluate(u'//Uml:Class', ctx) 56 #print >> sys.stderr, "UMLDefs: " + doc.toprettyxml() 57 # print >> sys.stderr, umlDefs 58 for e in umlDefs: 59 # print >> sys.stderr, e.localName 60 if (None, u'name') in e.attributes: 61 n = e.attributes[(None, u'name')].value 62 i = e.attributes[(None, u'xmi.id')].value 63 classNameDict[i] = n 64 ctx2 = Context(None, processorNss=NSS) 65 xp = u"//Uml:Class[@xmi.id='" + i + "']/*/Uml:Attribute" 66 attrDefs = Evaluate(xp, e, ctx2) 67 attrList = [] 68 for a in attrDefs: 69 attrList.append(a.attributes[(None, u'name')].value) 70 defList.append({'name': n, 'xmi_id': i, 'attrs': attrList}) 71 deps = Evaluate(u'//Uml:Dependency', ctx) 72 depList = [] 73 for dep in deps: 74 #print >> sys.stderr, "Deps:" 75 #print >> sys.stderr, dep 76 #print >> sys.stderr, "ChildNodes:" 77 #print >> sys.stderr, dep.childNodes 78 if not dep.childNodes == []: 79 ctx2 = Context(dep, processorNss=NSS) 80 i = dep.attributes[(None, 'xmi.id')].value 81 con_name = dep.attributes[(None, 'name')].value 82 xp = u"//Uml:Dependency[@xmi.id='" + i + "']/Uml:Dependency.client" 83 client_node = Evaluate(xp, ctx2) # [0].firstChild 84 #print >> sys.stderr, "Client Node" 85 #print >> sys.stderr, client_node[0].childNodes[1] 86 client = client_node[0].childNodes[1].attributes[(None, u'xmi.idref')].value 87 client = classNameDict[client] # replace the id by the classname 88 89 xp = u"//Uml:Dependency[@xmi.id='" + i + "']/Uml:Dependency.supplier" 90 supplier_node = Evaluate(xp, ctx2)[0].childNodes[1] 91 supplier = supplier_node.attributes[(None, u'xmi.idref')].value 92 #print >> sys.stderr, "idref: %s" % supplier 93 supplier = classNameDict[supplier] # Use the classname here 94 95 #.getElementsByTagNameNS(self.___NAMESPACE, "Class")[0].getAttribute('xmi.idref') 96 # supplier = dep.getAttribute('supplier') 97 depList.append([client, supplier, con_name]) 98 # print >> sys.stderr, defList 99 # print >> sys.stderr, depList 100 return {'classDefs': defList, 'dependencyDefs': depList}
101