用 XSLT 将 PowerDesign 9.5 的 cdm 文件直接转化为源代码
2004-09-29 17:57
260 查看
用 XSLT 将 PowerDesign 9.5 的 cdm 文件直接转化为源代码
工作中发现一旦项目中使用powerdesign进行建模之后,最后进行编码的时候几乎还需要自己手工做一遍,因为架构不同,使用powerdesign生成的C#源代码几乎不能使用。所以没办法只能自己来做这个转换。
第一版转换程序用的C#编的程序,一旦要修改,就要重新编译,不爽。所以这次用纯的XSLT来做这个转换。因为cdm文件比较复杂,作第一版的时候曾经尝试过用XSLT,但是最后因为自己xslt功力不够,只好转用代码实现。
经过半天奋战,半成品如下。我会根据工作进度及时修改的<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>C# source from CDM file</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- 覆盖缺省的文字处理过程 -->
<xsl:template match="text()|@*">
</xsl:template>
<!-- /Model -->
<xsl:template match="Model">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject -->
<xsl:template match="o:RootObject">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children -->
<xsl:template match="c:Children">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children/o:Model -->
<xsl:template match="o:Model">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages -->
<xsl:template match="c:Packages">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package -->
<xsl:template match="o:Package">
// <xsl:value-of select="a:Name" />
namespace <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
} // namespace <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:ConceptualDiagrams">
</xsl:template>
<xsl:template match="c:Relationships">
</xsl:template>
<xsl:template match="c:Domains">
</xsl:template>
<xsl:template match="c:ChildExtendedDependencies">
</xsl:template>
<xsl:template match="c:TargetModels">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities -->
<xsl:template match="c:Entities">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity -->
<xsl:template match="o:Entity">
// class <xsl:value-of select="a:Name" />
public class <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
}// class <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:Identifiers">
</xsl:template>
<xsl:template match="c:PrimaryIdentifier">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes -->
<xsl:template match="c:Attributes">
<xsl:apply-templates />
</xsl:template>
<!-- 开始写入数据字段定义和初始值 -->
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute -->
<xsl:template match="o:EntityAttribute">
<xsl:apply-templates mode="FieldRefer" select="c:DataItem" /><xsl:if test="a:Mandatory=0"> = null</xsl:if>;
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem -->
<!-- 根据DataItem中的子元素,分别定向到 DataItem 或 Shortcut 中 -->
<xsl:template match="c:DataItem" mode="FieldRefer">
<xsl:apply-templates mode="FieldJump" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:DataItem -->
<xsl:template match="o:DataItem" mode="FieldJump">
<!-- 跳到 o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:apply-templates mode="Field" select="../../../../../../c:DataItems/o:DataItem[@Id=$ref]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:Shortcut -->
<xsl:template match="o:Shortcut" mode="FieldJump">
<!-- 取得 o:Model/o:Packages/o:Package/c:Entities/o:Shortcut 信息并跳到 DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:variable name="tag" select="//o:Shortcut[@Id=$ref]/a:TargetID" />
<xsl:apply-templates mode="Field" select="//c:DataItems/o:DataItem[a:ObjectID=$tag]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<!-- 显示字段的类型:如果没有domain则解释映射dataType的值 -->
<xsl:template match="o:DataItem" mode="Field">
<xsl:variable name="domainId" select="c:Domain/o:Domain/@Ref" />
[XmlElement("<xsl:value-of select="a:Code" />")] public <xsl:if test="string-length($domainId) != 0" >
<xsl:apply-templates mode="FieldType" select="//c:Domains/o:Domain[@Id=$domainId]" />
</xsl:if><xsl:if test="string-length($domainId) = 0" >
<xsl:call-template name="SqlType2CsType">
<xsl:with-param name="type" select="a:DataType" />
<xsl:with-param name="len" select="a:Length" />
</xsl:call-template>
</xsl:if> m_<xsl:value-of select="a:Code" />
</xsl:template>
<!-- o:Model/c:Domains/o:Domain -->
<!-- 显示类型 -->
<xsl:template match="o:Domain" mode="FieldType"><xsl:value-of select="a:Code" />Type</xsl:template>
<!-- 转换 SQL 类型到 C# 类型 -->
<xsl:template name="SqlType2CsType">
<xsl:param name="type" />
<xsl:param name="len" />
<xsl:choose>
<xsl:when test="$type='VA'">string</xsl:when>
</xsl:choose>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 将实体之间关系转化为字段 -->
<xsl:template match="c:Relationships" mode="Field">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 将实体之间关系转化为字段 -->
<xsl:template match="o:Relationship" mode="Field">
</xsl:template>
</xsl:stylesheet>
工作中发现一旦项目中使用powerdesign进行建模之后,最后进行编码的时候几乎还需要自己手工做一遍,因为架构不同,使用powerdesign生成的C#源代码几乎不能使用。所以没办法只能自己来做这个转换。
第一版转换程序用的C#编的程序,一旦要修改,就要重新编译,不爽。所以这次用纯的XSLT来做这个转换。因为cdm文件比较复杂,作第一版的时候曾经尝试过用XSLT,但是最后因为自己xslt功力不够,只好转用代码实现。
经过半天奋战,半成品如下。我会根据工作进度及时修改的<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>C# source from CDM file</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- 覆盖缺省的文字处理过程 -->
<xsl:template match="text()|@*">
</xsl:template>
<!-- /Model -->
<xsl:template match="Model">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject -->
<xsl:template match="o:RootObject">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children -->
<xsl:template match="c:Children">
<xsl:apply-templates/>
</xsl:template>
<!-- /Model/o:RootObject/c:Children/o:Model -->
<xsl:template match="o:Model">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages -->
<xsl:template match="c:Packages">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package -->
<xsl:template match="o:Package">
// <xsl:value-of select="a:Name" />
namespace <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
} // namespace <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:ConceptualDiagrams">
</xsl:template>
<xsl:template match="c:Relationships">
</xsl:template>
<xsl:template match="c:Domains">
</xsl:template>
<xsl:template match="c:ChildExtendedDependencies">
</xsl:template>
<xsl:template match="c:TargetModels">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities -->
<xsl:template match="c:Entities">
<xsl:apply-templates/>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity -->
<xsl:template match="o:Entity">
// class <xsl:value-of select="a:Name" />
public class <xsl:value-of select="a:Code" />
{
<xsl:apply-templates/>
}// class <xsl:value-of select="a:Code" />
</xsl:template>
<xsl:template match="c:Identifiers">
</xsl:template>
<xsl:template match="c:PrimaryIdentifier">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes -->
<xsl:template match="c:Attributes">
<xsl:apply-templates />
</xsl:template>
<!-- 开始写入数据字段定义和初始值 -->
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute -->
<xsl:template match="o:EntityAttribute">
<xsl:apply-templates mode="FieldRefer" select="c:DataItem" /><xsl:if test="a:Mandatory=0"> = null</xsl:if>;
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem -->
<!-- 根据DataItem中的子元素,分别定向到 DataItem 或 Shortcut 中 -->
<xsl:template match="c:DataItem" mode="FieldRefer">
<xsl:apply-templates mode="FieldJump" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:DataItem -->
<xsl:template match="o:DataItem" mode="FieldJump">
<!-- 跳到 o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:apply-templates mode="Field" select="../../../../../../c:DataItems/o:DataItem[@Id=$ref]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Entities/o:Entity/c:Attributes/o:EntityAttribute/c:DataItem/o:Shortcut -->
<xsl:template match="o:Shortcut" mode="FieldJump">
<!-- 取得 o:Model/o:Packages/o:Package/c:Entities/o:Shortcut 信息并跳到 DataItem -->
<xsl:variable name="ref" select="@Ref" />
<xsl:variable name="tag" select="//o:Shortcut[@Id=$ref]/a:TargetID" />
<xsl:apply-templates mode="Field" select="//c:DataItems/o:DataItem[a:ObjectID=$tag]" />
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:DataItems/o:DataItem -->
<!-- 显示字段的类型:如果没有domain则解释映射dataType的值 -->
<xsl:template match="o:DataItem" mode="Field">
<xsl:variable name="domainId" select="c:Domain/o:Domain/@Ref" />
[XmlElement("<xsl:value-of select="a:Code" />")] public <xsl:if test="string-length($domainId) != 0" >
<xsl:apply-templates mode="FieldType" select="//c:Domains/o:Domain[@Id=$domainId]" />
</xsl:if><xsl:if test="string-length($domainId) = 0" >
<xsl:call-template name="SqlType2CsType">
<xsl:with-param name="type" select="a:DataType" />
<xsl:with-param name="len" select="a:Length" />
</xsl:call-template>
</xsl:if> m_<xsl:value-of select="a:Code" />
</xsl:template>
<!-- o:Model/c:Domains/o:Domain -->
<!-- 显示类型 -->
<xsl:template match="o:Domain" mode="FieldType"><xsl:value-of select="a:Code" />Type</xsl:template>
<!-- 转换 SQL 类型到 C# 类型 -->
<xsl:template name="SqlType2CsType">
<xsl:param name="type" />
<xsl:param name="len" />
<xsl:choose>
<xsl:when test="$type='VA'">string</xsl:when>
</xsl:choose>
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 将实体之间关系转化为字段 -->
<xsl:template match="c:Relationships" mode="Field">
</xsl:template>
<!-- o:Model/o:Packages/o:Package/c:Relationships/ -->
<!-- 将实体之间关系转化为字段 -->
<xsl:template match="o:Relationship" mode="Field">
</xsl:template>
</xsl:stylesheet>
相关文章推荐
- paip.powerdesign cdm pdm文件 代码生成器 java web 页面 实现
- eclipse直接查看jar包中class文件的源代码
- Python pandas读取csv文件直接转化成二维列表(Dataframe转化为list)
- eclipse直接查看jar包中class文件的源代码
- 网络流进行转化(String转化,直接写入文件)
- 文件直接转化为16进制
- Java对象与XML文件之间相互转化(含源代码)
- StringGrid数据、Excel文件之间的直接转化
- 无需转化直接使用ESD映像文件安装系统简明教程
- 无需转化直接使用ESD映像文件安装系统简明教程
- nginx配置虚拟域名后直接输出或下载网页源代码而不是运行网页文件的解决办法
- Qt-直接将xxx.ui文件转化为ui_xxx.h文件
- C++读取shp文件源代码并将转化为DXF
- eclipse直接查看jar包中class文件的源代码
- 如何将MATLAB文件转化为可执行文件
- 如何实现xslt文件中节点属性的自动更新
- Spring文件上传工具类,拿过去直接用就行了
- C# print2flash3文件转化
- MATLAB与图像处理(四):将图片序列转化为视频文件,将视频文件转化为图片
- ext2文件系统源代码之ext2.h