一个简单的powerdesigner模型(oom,pdm)分析器

  由于工作需要,写了一个简单的模型分析器,源码:

 

代码
     ///   <summary>
    
///  通过Xml分析powerdesigner模型文件
    
///   </summary>
     public   class  ModelAnalyserLinq : IModelAnalyser
    {  

        
///   <summary>
        
///  Xml命名空间管理
        
///   </summary>
        XmlNamespaceManager nsmgr  =   null ;

        
///   <summary>
        
///  属性列表,记录字段
        
///   </summary>
        List < ModelElement >  listAtt  =   new  List < ModelElement > ();

        
///   <summary>
        
///  单元列表:记录领域对象列表或者数据库表对象列表
        
///   </summary>
        List < ModelElement >  listUnit  =   new  List < ModelElement > ();

        
///   <summary>
        
///  
        
///   </summary>
        ModelType _mt  =  ModelType.Domain;

        
///   <summary>
        
///  一个ModelAnalyser对应一个XElement root
        
///   </summary>
        XElement root  =   null ;

        
public  ModelAnalyserLinq(String fileName)
            : 
this (fileName, ModelType.Domain)
        {
            
try
            {
                
// 执行linq检索(element) 
                var classesNodes   =  from element  in  root.Descendants(Const.c  +   " Classes " )
                          select element; 

                
if  (classesNodes.Count < XElement > ()  >   0 ) // 说明存在类型,就是领域模型,数据库模型中不存在这个的
                {
                    _mt 
=  ModelType.Domain;
                }
                
else
                {
                    var tablesNodes 
=  from element  in  root.Descendants(Const.c  +   " Tables " )
                             select element;

                    
if  (tablesNodes.Count < XElement > ()  >   0 ) // 说明存在类型,就是数据库模型
                    {
                        _mt 
=  ModelType.Database;
                    }
                    
else
                    {
                        
throw   new  Exception( " 非法模型文件,系统只支持领域模型和数据库模型! " );
                    }

                }

            }
            
catch  (Exception e)
            {
                MessageBox.Show(
" 读取model出错!\r\n "   +  e.Message,  " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        
public  ModelAnalyserLinq(String fileName, ModelType mt)
        {
            
try
            {
                _mt 
=  mt;
                root 
=  XElement.Load(fileName);
            }
            
catch  (Exception e)
            {
                MessageBox.Show(
" 读取model出错!\r\n "   +  e.Message,  " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        
public  ModelType MT
        {
            
get  {  return  _mt; }
        }

        
///   <summary>
        
///  获取模型的基本对象集合,特指表集合或者实体集合
        
///   </summary>
        
///   <returns></returns>
         public  List < ModelElement >  GetObjectList()
        {
            List
< ModelElement >  list  =   new  List < ModelElement > ();
            
if  (_mt  ==  ModelType.Domain)
            {

                var classNodes 
=  from element  in  root.Descendants(Const.o  +   " Class " )
                                 
where  element.Parent  != null
                                 
&&  element.Parent.Name  ==  Const.c  +   " Classes "
                                   select element;

                
foreach  (XElement n  in  classNodes)
                {
                    ModelElement attr 
=  GenerateElementFromNode(n);
                    list.Add(attr);
                }
            
            }
            
else
            {
                var tableNodes 
=  from element  in  root.Descendants(Const.o  +   " Table " )
                                 
where  element.Parent  !=   null
                                
&&  element.Parent.Name  ==  Const.c  +   " Tables "
                                 select element;

                
foreach  (XElement n  in  tableNodes)
                {
                    ModelElement attr 
=  GenerateElementFromNode(n);
                    IEnumerable
< XElement >   tmp  =  n.Descendants(Const.a  +   " Comment " );
                    
if  (tmp.Count < XElement > ()  >   0  )
                    {
                        attr.Comment 
=  tmp.First < XElement > ().Value;
                    }
                    list.Add(attr);
                }
            }
      
            
return  list;
        }

        
///   <summary>
        
///  根据xml节点获取对应的ModelElement
        
///   </summary>
        
///   <param name="n"></param>
        
///   <returns></returns>
         private  ModelElement GenerateElementFromNode(XElement n)
        {      
            ModelElement attr 
=   new  ModelElement();

            attr.Code 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " Code " );
            attr.CreationDate 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " CreationDate " );
            attr.Creator 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " Creator " );
            attr.ModificationDate 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " ModificationDate " );
            attr.Modifier 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " Modifier " );
            attr.Name 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " Name " );
            attr.ObjectID 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " ObjectID " );
            attr.Code 
=  GetElementValueFromClassTable(n.FirstAttribute.Value,  " Code " );

            
return  attr;
        }

        
private   String GetElementValueFromClassTable( string  classID,  string  property)
        {
            IEnumerable
< XElement >  tmpList  =  from t2  in  root.Descendants(Const.a  +  property)
                                            
where  t2.Parent  !=   null
                                                
&&  t2.Parent.FirstAttribute  !=   null
                                                
&&  t2.Parent.FirstAttribute.Value  ==  classID
                                            select t2;
            
if  (tmpList.Count < XElement > ()  ==   0 )
                
return  String.Empty;
            
return  tmpList.First < XElement > ().Value;
        }

        
///   <summary>
        
///  根据对象ID找到属性列表
        
///   </summary>
        
///   <param name="objectID"></param>
        
///   <returns></returns>
         public  List < ModelElement >  GetAttributeList( string  objectID)
        {
            List
< ModelElement >  list  =   new  List < ModelElement > ();
            XElement node 
=  FindNodeByObjectID(objectID);

            
string  classOrTable  =  String.Empty;

            
if  (_mt  ==  ModelType.Domain)
            {
                classOrTable 
=   " Attribute " ;
            }
            
else
            {
                classOrTable 
=   " Column " ;
            }

            IEnumerable
< XElement >  listAttr  =
                        from el 
in  root.Descendants(Const.o  +  classOrTable)
                        
where  el.Parent  !=   null
                        
&&  el.Parent .Parent  !=   null
                        
&&  el.Parent.Parent.FirstAttribute  !=   null
                        
&&  FindNodeByObjectID(objectID)  !=   null
                        
&&  FindNodeByObjectID(objectID).Parent   !=   null
                        
&&  FindNodeByObjectID(objectID).Parent.FirstAttribute  !=   null
                        
&&  el.Parent.Parent.FirstAttribute.Value  ==  FindNodeByObjectID(objectID).Parent.FirstAttribute.Value
                        select el;

            
foreach  (XElement n  in  listAttr)
            {
                ModelElement ma 
=  GenerateElementFromNode(n);
                list.Add(ma);
            }
            
return  list;
        }

        
///   <summary>
        
///  根据对象id(guid)找到对应的节点
        
///   </summary>
        
///   <param name="objectID"></param>
        
///   <returns></returns>
         private  XElement FindNodeByObjectID( string  objectID)
        {
            IEnumerable
< XElement >  list  =
                            from el 
in  root.Descendants(Const.a  +   " ObjectID " )
                            
where  el.Value  ==  objectID
                            select el;
            
if  (list.Count < XElement > ()  >   0 )
                
return  list.First < XElement > ();
            
else
                
return   null ;
        
        }

    }

 

 

代码
  class  Const
    {
        
public   readonly   static  XNamespace a  =    " attribute " , c  =   " collection " , o  =   " object " ;

        
public   const   string  cClasses  =   " c:Classes " ;
        
public   const   string  oClass  =   " o:Class " ;

        
public   const   string  cAttributes  =   " c:Attributes " ;
        
public   const   string  oAttribute  =   " o:Attribute " ;

        
public   const   string  cTables  =   " c:Tables " ;
        
public   const   string  oTable  =   " o:Table " ;

        
public   const   string  cColumns  =   " c:Columns " ;
        
public   const   string  oColumn  =   " o:Column " ;
    }

 

代码
  interface   IModelAnalyser
    {
        List
< ModelElement >  GetAttributeList( string  objectID);
        List
< ModelElement >  GetObjectList();
        ModelType MT { 
get ; }
    }


    
///   <summary>
    
///  xml节点entity,可以指定类型:table,class,column,attribute等
    
///   </summary>
     public   class  ModelElement
    {
        
///   <summary>
        
///  重写ToString(),给ListView显示用
        
///   </summary>
        
///   <returns></returns>
         public   override   string  ToString()
        {
            
return  Name  +  Code  +  (String.IsNullOrEmpty(Comment)  ?   ""  : Comment);
        }

        
// <a:ObjectID>DE001EE1-B9D8-4904-AA97-1CB93DAE107E</a:ObjectID>
         public   string  ObjectID {  get set ; }
        
// <a:Name>类型</a:Name>
         public   string  Name {  get set ; }
        
// <a:Code>类型</a:Code>
         public   string  Code {  get set ; }
        
// <a:CreationDate>1271233784</a:CreationDate>
         public   string  CreationDate {  get set ; }
        
// <a:Creator>xiaxilin</a:Creator>
         public   string  Creator {  get set ; }
        
// <a:ModificationDate>1271233784</a:ModificationDate>
         public   string  ModificationDate {  get set ; }
        
// <a:Modifier>xiaxilin</a:Modifier>
         public   string  Modifier {  get set ; }
        
// <a:DataType>java.lang.String</a:DataType>
         public   string  DataType {  get set ; }
        
// <a:Attribute.Visibility>-</a:Attribute.Visibility>
         public   string  Visibility {  get set ; }
        
// <a:Comment>-</a:Comment>
         public   string  Comment {  get set ; }

    }

    
///   <summary>
    
///  模型类型
    
///   </summary>
     public   enum  ModelType
    {
        
///   <summary>
        
///  领域模型
        
///   </summary>
        Domain  =   0 ,

        
///   <summary>
        
///  数据库模型
        
///   </summary>
        Database  =   1
    }

 

 

代码
   ///   <summary>
    
///  通过Xml分析powerdesigner模型文件
    
///   </summary>
     public   class  ModelAnalyser : IModelAnalyser
    {
   

        
///   <summary>
        
///  Xml命名空间管理
        
///   </summary>
        XmlNamespaceManager nsmgr  =   null ;

        
///   <summary>
        
///  属性列表,记录字段
        
///   </summary>
        List < ModelElement >  listAtt  =   new  List < ModelElement > ();

        
///   <summary>
        
///  单元列表:记录领域对象列表或者数据库表对象列表
        
///   </summary>
        List < ModelElement >  listUnit  =   new  List < ModelElement > ();

        
///   <summary>
        
///  
        
///   </summary>
        ModelType _mt  =  ModelType.Domain;

        
///   <summary>
        
///  一个ModelAnalyser对应一个xmldocument
        
///   </summary>
        XmlDocument xd  =   new  XmlDocument();

        
public  ModelAnalyser(String fileName)
            : 
this (fileName, ModelType.Domain)
        {
            
try
            {
                XmlNode node 
=  xd.SelectSingleNode( " // "   +  Const.cClasses, nsmgr);
                
if  (node  !=   null ) // 说明存在类型,就是领域模型,数据库模型中不存在这个的
                {
                    _mt 
=  ModelType.Domain;
                }
                
else
                {
                    XmlNode node2 
=  xd.SelectSingleNode( " // "   +  Const.cTables, nsmgr);
                    
if  (node2  !=   null ) // 说明存在类型,就是数据库模型
                    {
                        _mt 
=  ModelType.Database;
                    }
                    
else
                    {
                        
throw   new  Exception( " 非法模型文件,系统只支持领域模型和数据库模型! " );
                    }

                }

            }
            
catch  (Exception e)
            {
                MessageBox.Show(
" 读取model出错!\r\n "   +  e.Message,  " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        
public  ModelAnalyser(String fileName, ModelType mt)
        {
            
try
            {
                _mt 
=  mt;
                xd.Load(fileName);

                nsmgr 
=   new  XmlNamespaceManager(xd.NameTable);
                nsmgr.AddNamespace(
" a " " attribute " );
                nsmgr.AddNamespace(
" c " " collection " );
                nsmgr.AddNamespace(
" o " " object " );

            }
            
catch  (Exception e)
            {
                MessageBox.Show(
" 读取model出错!\r\n "   +  e.Message,  " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        
public  ModelType MT
        {
            
get  {  return  _mt; }
        }

        
///   <summary>
        
///  获取模型的基本对象集合,特指表集合或者实体集合
        
///   </summary>
        
///   <returns></returns>
         public  List < ModelElement >  GetObjectList()
        {
            List
< ModelElement >  list  =   new  List < ModelElement > ();
            
if  (_mt  ==  ModelType.Domain)
            {
                XmlNode node 
=  xd.SelectSingleNode( " // "   +  Const.cClasses, nsmgr);
                
if  (node  !=   null )
                {
                    
foreach  (XmlNode n  in  node.ChildNodes)
                    {
                        ModelElement attr 
=  GenerateElementFromNode(n);
                        list.Add(attr);
                    }
                }
            }
            
else
            {
                XmlNode node 
=  xd.SelectSingleNode( " // "   +  Const.cTables, nsmgr);
                
if  (node  !=   null )
                {
                    
foreach  (XmlNode n  in  node.ChildNodes)
                    {
                        ModelElement attr 
=  GenerateElementFromNode(n);
                        XmlNode tmp 
=  n.SelectSingleNode( " a:Comment " , nsmgr);
                        
if  (tmp  !=   null )
                        {
                            attr.Comment 
=  tmp.InnerText;
                        }
                        list.Add(attr);
                    }
                }
            }
            
return  list;
        }

        
///   <summary>
        
///  根据xml节点获取对应的ModelElement
        
///   </summary>
        
///   <param name="n"></param>
        
///   <returns></returns>
         private  ModelElement GenerateElementFromNode(XmlNode n)
        {
            ModelElement attr 
=   new  ModelElement();
            attr.Code 
=  n.SelectSingleNode( " a:Code " , nsmgr).InnerText;
            attr.CreationDate 
=  n.SelectSingleNode( " a:CreationDate " , nsmgr).InnerText;
            attr.Creator 
=  n.SelectSingleNode( " a:Creator " , nsmgr).InnerText;
            attr.ModificationDate 
=  n.SelectSingleNode( " a:ModificationDate " , nsmgr).InnerText;
            attr.Modifier 
=  n.SelectSingleNode( " a:Modifier " , nsmgr).InnerText;
            attr.Name 
=  n.SelectSingleNode( " a:Name " , nsmgr).InnerText;
            attr.ObjectID 
=  n.SelectSingleNode( " a:ObjectID " , nsmgr).InnerText;
            
return  attr;
        }

        
///   <summary>
        
///  根据对象ID找到属性列表
        
///   </summary>
        
///   <param name="objectID"></param>
        
///   <returns></returns>
         public  List < ModelElement >  GetAttributeList( string  objectID)
        {
            List
< ModelElement >  list  =   new  List < ModelElement > ();
            XmlNode node 
=  FindNodeByObjectID(objectID);
            
foreach  (XmlNode n  in  node.ChildNodes)
            {
                ModelElement ma 
=  GenerateElementFromNode(n);
                list.Add(ma);
            }
            
return  list;
        }

        
///   <summary>
        
///  根据对象id(guid)找到对应的节点
        
///   </summary>
        
///   <param name="objectID"></param>
        
///   <returns></returns>
         private  XmlNode FindNodeByObjectID( string  objectID)
        {
            XmlNodeList nodeList 
=   null ;
            
if  (_mt  ==  ModelType.Domain)
            {
                nodeList 
=  xd.SelectSingleNode( " // "   +  Const.cClasses, nsmgr).ChildNodes;
            }
            
else
            {
                nodeList 
=  xd.SelectSingleNode( " // "   +  Const.cTables, nsmgr).ChildNodes;
            }

            
foreach  (XmlNode n  in  nodeList)
            {
                
if  (n.SelectSingleNode( " a:ObjectID " , nsmgr).InnerText  ==  objectID)
                {
                    
if  (_mt  ==  ModelType.Domain)
                    {
                        
return  n.SelectSingleNode( " // "   +  Const.cAttributes, nsmgr);
                    }
                    
else
                    {
                        
return  n.SelectSingleNode( " // "   +  Const.cColumns, nsmgr);
                    }
                }
            }
            
return   null ;
        }

    }

 

 

你可能感兴趣的:(powerDesigner)