///
<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
;
}
}