Java解析Powerdesigner逻辑数据模型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/redfoxtao/article/details/77160714

近来无事,尝试着对Powerdesigner的逻辑数据模型进行解析,以便按照实际需要完成从逻辑数据模型到数据库建表脚本、代码的自动生成实验。

在对LDM文件进行分析后,定义如下类:
1 LDM文件中生成代码所需的对象集合 LdmInfo

// 字段表
private LinkedHashMap<String, LdmAttribute> attrHash = null;
// 关联关系表
private LinkedHashMap<String, LdmRelationship> relHash = null;
// 外键关联关系表
private LinkedHashMap<String, LdmRelationship> relFkHash = null;
// 表对应外键列表
private LinkedHashMap<String, List<LdmRelationship>> relFkTblHash = null;
// 主键关系表
private LinkedHashMap<String, LdmIdentifier> identHash = null;
// 实体类表(数据库表)
private LinkedHashMap<String, LdmEntity> entityHash = null;
// 实体类表(数据库表)
private LinkedHashMap<String, LdmEntity> entityCodeHash = null;

2 LDM文件中的实体类 LdmEntity

// 可关联唯一id
private String oid = "";
// 对象id(全局唯一)
private String objID = "";
// 对象名称
private String name = "";
// 对象代码
private String code = "";
// 注释
private String comment = "";
// 主键对象id
private String pkOid = "";
// 外键对象id列表
private List<String> fkOidList = null;
// 关联关系表
private List<LdmIdentifier> identList = null;
// 字段表
private List<LdmAttribute> attrList = null;

3 LDM文件中实体类下的属性字段 LdmAttribute

// 可关联唯一id
private String oid = "";
// 对象id(全局唯一)
private String objID = "";
// 对象名称
private String name = "";
// 对象代码
private String code = "";
// 注释
private String comment = "";
// 是否为主键
private boolean pk = false;
// 是否为外键
private boolean fk = false;
// 外键关联表名
private String fkTblName = "";
// 外键关联对应表的字段
private String fkTblField = "";
// 外键关联对应关系
private String fkTblFieldRltn = "";
// 数据类型
private String dataType = "";
// 长度
private String length = "";
// 小数点位数
private String precision = "";
// 是否可为null值
private int mandatory = 0;
// 默认值
private String defVal = "";
// 字段数据类型
private String columnType = "";
// 字段数据类型是否为字符
private boolean columnTypeIsChar = false;

4 LDM中实体类中的关联对象 LdmIdentifier

// 可关联唯一id
private String oid = "";
// 对象id(全局唯一)
private String objID = "";
// 对象名称
private String name = "";
// 对象代码
private String code = "";
// 关联对象oid
private String refOid = "";

5 LDM文件中的外键关联对象 LdmRelationship

// 可关联唯一id
private String oid = "";
// 对象id(全局唯一)
private String objID = "";
// 对象名称
private String name = "";
// 对象代码
private String code = "";
// 关联对象1
private String obj1Ref = "";
// 关联对象2
private String obj2Ref = "";
// 关联对象1对应字段
private String obj1AttrRef = "";
// 关联对象2对应字段
private String obj2AttrRef = "";
// 对象1对象2关联关系
private String obj1Obj2 = "";
// 对象2对象1关联关系
private String obj2Obj1 = "";
// 上级关联主键
private String parentIdentifierOid = "";

6 读取并解析LDM文件

private boolean isTest = false;
// LdmInfo ldmInfo = new LdmInfo();
String tmpXmlFileName = "QX_Mng_LDM";

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ReadFileFromLDM app = new ReadFileFromLDM();
    try {
        app.dealTblFromLDM("D:/dvc_code/AttendApp/doc/权限管理/QX_Mng.ldm", app.tmpXmlFileName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 读取LDM文件内容
 * 
 * @param fileName
 * @param tmpXmlFileName
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public LdmInfo dealTblFromLDM(String fileName, String tmpXmlFileName) throws Exception {
    LdmInfo ldmInfo = new LdmInfo();
    // 创建saxReader对象
    SAXReader reader = new SAXReader();
    // 通过read方法读取一个文件 转换成Document对象
    Document document = reader.read(new File(fileName));
    //
    writer(document, "src//" + tmpXmlFileName + ".xml");

    // 获取根节点元素对象
    Element root = document.getRootElement();
    // if (isTest) {
    // showNodeInfo(root);
    // }

    // 获取RootObject节点元素对象
    Element objElm = root.element("RootObject");
    // if (isTest) {
    // showNodeInfo(objElm);
    // }

    // 获取Children节点元素对象
    Element childElm = objElm.element("Children");
    // if (isTest) {
    // showNodeInfo(childElm);
    // }

    // 获取Model节点元素对象
    Element modelElm = childElm.element("Model");
    // if (isTest) {
    // showNodeInfo(modelElm);
    // }

    // 获取Entities节点元素中的全部EntityAttribute对象
    Element entitiesElm = modelElm.element("Entities");
    if (entitiesElm != null) {
        // if (isTest) {
        // showNodeInfo(entitiesElm);
        // }
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = entitiesElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            // if (isTest) {
            // showNodeInfo(elm);
            // }
            dealTblAttrInfo(elm, ldmInfo);
        }
    }

    // 获取Relationships节点元素对象
    Element relsElm = modelElm.element("Relationships");
    if (relsElm != null) {
        if (isTest) {
            showNodeInfo(relsElm);
        }
        // 获取Relationship元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = relsElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            // if (isTest) {
            // showNodeInfo(elm);
            // }
            LdmRelationship ldmRelationship = dealRelShipInfo(elm);
            ldmInfo.addRelHash(ldmRelationship);
        }
    }

    // 获取Entities节点元素对象
    if (entitiesElm != null) {
        // if (isTest) {
        // showNodeInfo(entitiesElm);
        // }
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = entitiesElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            // if (isTest) {
            // showNodeInfo(elm);
            // }
            dealTblInfo(elm, ldmInfo);
        }
    }
    return ldmInfo;
}

/**
 * 遍历当前节点元素下面的所有(元素的)子节点
 * 
 * @param node
 */
@SuppressWarnings("unchecked")
public void showNodeInfo(Element node) {
    System.out.println("当前节点的名称:[" + node.getName() + "]");
    // 获取当前节点的所有属性节点
    List<Attribute> list = node.attributes();
    // 遍历属性节点
    if (list != null && list.size() > 0) {
        System.out.println("遍历属性节点:");
        for (Attribute attr : list) {
            System.out.println("[" + attr.getText() + "]-----[" + attr.getName() + "]---[" + attr.getValue() + "]");
        }
        System.out.println("<----------------------->");
    }

    if (!(node.getTextTrim().equals(""))) {
        System.out.println("文本内容:[" + node.getText() + "]");
    }

    // 当前节点下面子节点迭代器
    Iterator<Element> it = node.elementIterator();
    // 遍历
    while (it.hasNext()) {
        // 获取某个子节点对象
        Element e = it.next();
        // 对子节点进行遍历
        System.out.println("当前子节点的名称:[" + e.getName() + "]");
    }
}

/**
 * 遍历当前节点元素下面的所有(元素的)子节点
 * 
 * @param attribute
 */
public void showNodeInfo(Attribute attribute) {
    System.out.println(
            "[" + attribute.getText() + "]-----[" + attribute.getName() + "]---[" + attribute.getValue() + "]");
}

/**
 * 把document对象写入新的文件
 * 
 * @param document
 * @param fileName
 * @throws Exception
 */
public void writer(Document document, String fileName) throws Exception {
    // 紧凑的格式
    // OutputFormat format = OutputFormat.createCompactFormat();
    // 排版缩进的格式
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置编码
    format.setEncoding("UTF-8");
    // 创建XMLWriter对象,指定了写出文件及编码格式
    // XMLWriter writer = new XMLWriter(new FileWriter(new
    // File("src//a.xml")),format);
    XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "UTF-8"),
            format);
    // 写入
    writer.write(document);
    // 立即写入
    writer.flush();
    // 关闭操作
    writer.close();
}

/**
 * 解析数据库表信息(获取全部Attribute属性信息)
 * 
 * @param node
 * @param ldmInfo
 */
@SuppressWarnings("unchecked")
public void dealTblAttrInfo(Element node, LdmInfo ldmInfo) throws Exception {
    LdmEntity ldmEntity = new LdmEntity();
    // 读取属性“Id”的值
    Attribute attribute = node.attribute("Id");
    ldmEntity.setOid(attribute.getText());

    // 判断外键
    List<LdmRelationship> relList = ldmInfo.getRelFkTblHashByOid(ldmEntity.getOid());
    if (relList != null && relList.size() > 0) {
        for (int i = 0; i < relList.size(); i++) {
            LdmRelationship relShip = relList.get(i);
            ldmEntity.addFkOidList(relShip.getObj2AttrRef());
        }
    }

    // 获取ObjectID节点元素对象
    Element objElm = node.element("ObjectID");
    ldmEntity.setObjID(objElm.getTextTrim());

    // 获取Name节点元素对象
    Element nameElm = node.element("Name");
    ldmEntity.setName(nameElm.getTextTrim());

    // 获取Code节点元素对象
    Element codeElm = node.element("Code");
    ldmEntity.setCode(codeElm.getTextTrim());

    // 获取Comment节点元素对象
    Element commentElm = node.element("Comment");
    if (commentElm != null) {
        ldmEntity.setComment(commentElm.getTextTrim());
    }
    ldmInfo.addEntityHash(ldmEntity);

    // 获取Identifiers节点元素对象
    Element attrsElm = node.element("Attributes");
    if (attrsElm != null) {
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = attrsElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            LdmAttribute ldmAttribute = dealAttrbute(elm);
            ldmInfo.addAttrHash(ldmAttribute);
        }
    }
}

/**
 * 解析数据库表信息
 * 
 * @param node
 * @param ldmInfo
 */
@SuppressWarnings("unchecked")
public void dealTblInfo(Element node, LdmInfo ldmInfo) throws Exception {
    LdmEntity ldmEntity = new LdmEntity();
    // 读取属性“Id”的值
    Attribute attribute = node.attribute("Id");
    ldmEntity.setOid(attribute.getText());

    // 判断外键
    List<LdmRelationship> relList = ldmInfo.getRelFkTblHashByOid(ldmEntity.getOid());
    if (relList != null && relList.size() > 0) {
        for (int i = 0; i < relList.size(); i++) {
            LdmRelationship relShip = relList.get(i);
            ldmEntity.addFkOidList(relShip.getObj2AttrRef());
        }
    }

    // 获取ObjectID节点元素对象
    Element objElm = node.element("ObjectID");
    ldmEntity.setObjID(objElm.getTextTrim());

    // 获取Name节点元素对象
    Element nameElm = node.element("Name");
    ldmEntity.setName(nameElm.getTextTrim());

    // 获取Code节点元素对象
    Element codeElm = node.element("Code");
    ldmEntity.setCode(codeElm.getTextTrim());

    // 获取Comment节点元素对象
    Element commentElm = node.element("Comment");
    if (commentElm != null) {
        ldmEntity.setComment(commentElm.getTextTrim());
    }

    // 获取Identifiers节点元素对象
    Element IdentsElm = node.element("Identifiers");
    if (IdentsElm != null) {
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = IdentsElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            LdmIdentifier ldmIdentifier = dealIdentInfo(elm);
            if (ldmIdentifier != null) {
                ldmEntity.addIdentList(ldmIdentifier);
                ldmInfo.addIdentHash(ldmIdentifier);
            }
        }
    }

    // 获取PrimaryIdentifier节点元素对象
    Element pkElm = node.element("PrimaryIdentifier");
    if (pkElm != null) {
        Element pkIdentElm = pkElm.element("Identifier");
        if (pkIdentElm != null) {
            Attribute refAttr = pkIdentElm.attribute("Ref");
            if (refAttr != null) {
                ldmEntity.setPkOid(refAttr.getText());
            }
        }
    }

    // 获取Identifiers节点元素对象
    Element attrsElm = node.element("Attributes");
    if (attrsElm != null) {
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = attrsElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            LdmAttribute ldmAttribute = dealAttrbute(elm);
            // 检查是否为主键
            if (ldmEntity.getPkOid() != null && ldmEntity.getPkOid().trim().length() > 0) {
                LdmIdentifier ldmIdentifier = ldmInfo.getIdentHashByOid(ldmEntity.getPkOid().trim());
                if (ldmIdentifier != null && ldmIdentifier.getRefOid().equalsIgnoreCase(ldmAttribute.getOid())) {
                    ldmAttribute.setPk(true);
                } else {
                    ldmAttribute.setPk(false);
                }
            }
            // 检查是否为外键
            if (ldmEntity.getFkOidList() != null && ldmEntity.getFkOidList().size() > 0) {
                for (int i = 0; i < ldmEntity.getFkOidList().size(); i++) {
                    LdmRelationship ldmRelationship = ldmInfo
                            .getRelFkHashByOid(ldmEntity.getFkOidList().get(i).trim());
                    if (ldmRelationship != null
                            && ldmRelationship.getObj2AttrRef().equalsIgnoreCase(ldmAttribute.getOid())) {
                        ldmAttribute.setFk(true);
                        LdmEntity fkLdmEntity = ldmInfo.getEntityHashByOid(ldmRelationship.getObj2Ref());
                        if (fkLdmEntity != null) {
                            ldmAttribute.setFkTblName(fkLdmEntity.getCode());
                        }
                        LdmAttribute fkLdmAttribute = ldmInfo.getAttrHashByOid(ldmRelationship.getObj1AttrRef());
                        ldmAttribute.setFkTblField(fkLdmAttribute.getCode());
                        ldmAttribute.setFkTblFieldRltn(ldmRelationship.getObj1Obj2());
                    } else {
                        ldmAttribute.setFk(false);
                        ldmAttribute.setFkTblName("");
                        ldmAttribute.setFkTblField("");
                    }
                }
            }
            ldmInfo.addAttrHash(ldmAttribute);
            ldmEntity.addAttrList(ldmAttribute);
        }
    }

    ldmInfo.addEntityHash(ldmEntity);
    if (isTest) {
        System.out.println(ldmEntity);
    }
}

/**
 * 解析标识信息
 * 
 * @param node
 * @return
 * @throws Exception
 */
public LdmIdentifier dealIdentInfo(Element node) throws Exception {
    LdmIdentifier ldmIdentifier = new LdmIdentifier();

    // 读取属性“Id”的值
    Attribute attribute = node.attribute("Id");
    ldmIdentifier.setOid(attribute.getText());

    // 获取ObjectID节点元素对象
    Element objElm = node.element("ObjectID");
    ldmIdentifier.setObjID(objElm.getTextTrim());

    // 获取Name节点元素对象
    Element nameElm = node.element("Name");
    ldmIdentifier.setName(nameElm.getTextTrim());

    // 获取Code节点元素对象
    Element codeElm = node.element("Code");
    ldmIdentifier.setCode(codeElm.getTextTrim());

    // 获取PrimaryIdentifier节点元素对象
    Element attrsElm = node.element("Identifier.Attributes");
    if (attrsElm != null) {
        Element attrElm = attrsElm.element("EntityAttribute");
        if (attrElm != null) {
            Attribute refAttr = attrElm.attribute("Ref");
            if (refAttr != null) {
                ldmIdentifier.setRefOid(refAttr.getText());
            }
        }
    }

    return ldmIdentifier;
}

/**
 * 解析属性(表字段)信息
 * 
 * @param node
 * @return
 * @throws Exception
 */
public LdmAttribute dealAttrbute(Element node) throws Exception {
    LdmAttribute ldmAttribute = new LdmAttribute();

    // 读取属性“Id”的值
    Attribute attribute = node.attribute("Id");
    ldmAttribute.setOid(attribute.getText());

    // 获取ObjectID节点元素对象
    Element objElm = node.element("ObjectID");
    ldmAttribute.setObjID(objElm.getTextTrim());

    // 获取Name节点元素对象
    Element nameElm = node.element("Name");
    ldmAttribute.setName(nameElm.getTextTrim());

    // 获取Code节点元素对象
    Element codeElm = node.element("Code");
    ldmAttribute.setCode(codeElm.getTextTrim());

    // 获取Code节点元素对象
    Element commentElm = node.element("Comment");
    if (commentElm != null) {
        ldmAttribute.setComment(commentElm.getTextTrim());
    }

    // 获取DataType节点元素对象
    Element dataTypeElm = node.element("DataType");
    if (dataTypeElm != null) {
        ldmAttribute.setDataType(dataTypeElm.getTextTrim());
    }

    // 获取Code节点元素对象
    Element lengthElm = node.element("Length");
    if (lengthElm != null) {
        ldmAttribute.setLength(lengthElm.getTextTrim());
    }

    // 获取LogicalAttribute.Mandatory节点元素对象
    Element mandatoryElm = node.element("LogicalAttribute.Mandatory");
    if (mandatoryElm != null) {
        if (mandatoryElm.getTextTrim().equalsIgnoreCase("1")) {
            ldmAttribute.setMandatory(1);
        } else {
            ldmAttribute.setMandatory(0);
        }
    }

    // 获取DefaultValue节点元素对象
    Element defValElm = node.element("DefaultValue");
    if (defValElm != null) {
        ldmAttribute.setDefVal(defValElm.getTextTrim());
    }

    return ldmAttribute;
}

/**
 * 解析主外键关联关系
 * 
 * @param node
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public LdmRelationship dealRelShipInfo(Element node) throws Exception {
    LdmRelationship ldmRelationship = new LdmRelationship();

    // 读取属性“Id”的值
    Attribute attribute = node.attribute("Id");
    ldmRelationship.setOid(attribute.getText());

    // 获取ObjectID节点元素对象
    Element objElm = node.element("ObjectID");
    ldmRelationship.setObjID(objElm.getTextTrim());

    // 获取Name节点元素对象
    Element nameElm = node.element("Name");
    ldmRelationship.setName(nameElm.getTextTrim());

    // 获取Code节点元素对象
    Element codeElm = node.element("Code");
    ldmRelationship.setCode(codeElm.getTextTrim());

    // 获取Entity1ToEntity2RoleCardinality节点元素对象
    Element obj1Obj2Elm = node.element("Entity1ToEntity2RoleCardinality");
    if (obj1Obj2Elm != null) {
        ldmRelationship.setObj1Obj2(obj1Obj2Elm.getTextTrim());
    }

    // 获取Entity2ToEntity1RoleCardinality节点元素对象
    Element obj2Obj1Elm = node.element("Entity2ToEntity1RoleCardinality");
    if (obj2Obj1Elm != null) {
        ldmRelationship.setObj2Obj1(obj2Obj1Elm.getTextTrim());
    }

    // 获取Object1节点元素对象
    Element obj1Elm = node.element("Object1");
    if (obj1Elm != null) {
        Element obj1EntityElm = obj1Elm.element("Entity");
        if (obj1EntityElm != null) {
            Attribute refAttr = obj1EntityElm.attribute("Ref");
            if (refAttr != null) {
                ldmRelationship.setObj1Ref(refAttr.getText());
            }
        }
    }

    // 获取Object2节点元素对象
    Element obj2Elm = node.element("Object2");
    if (obj2Elm != null) {
        Element obj2EntityElm = obj2Elm.element("Entity");
        if (obj2EntityElm != null) {
            Attribute refAttr = obj2EntityElm.attribute("Ref");
            if (refAttr != null) {
                ldmRelationship.setObj2Ref(refAttr.getText());
            }
        }
    }

    // 获取Joins节点元素对象
    Element joinsElm = node.element("Joins");
    if (joinsElm != null) {
        // 获取Entity元素列表,当前节点下面子节点迭代器
        Iterator<Element> it = joinsElm.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element elm = (Element) it.next();
            // 获取Object1节点元素对象
            Element Object1Elm = elm.element("Object1");
            if (Object1Elm != null) {
                // 获取EntityAttribute节点元素对象
                Element Object1AttrElm = Object1Elm.element("EntityAttribute");
                Attribute refAttr = Object1AttrElm.attribute("Ref");
                if (refAttr != null) {
                    ldmRelationship.setObj1AttrRef(refAttr.getText());
                }
            }
            // 获取Object1节点元素对象
            Element Object2Elm = elm.element("Object2");
            if (Object2Elm != null) {
                // 获取EntityAttribute节点元素对象
                Element Object1AttrElm = Object2Elm.element("EntityAttribute");
                Attribute refAttr = Object1AttrElm.attribute("Ref");
                if (refAttr != null) {
                    ldmRelationship.setObj2AttrRef(refAttr.getText());
                }
            }
        }
    }

    // 获取ParentIdentifier节点元素对象
    Element prntIdentElm = node.element("ParentIdentifier");
    if (prntIdentElm != null) {
        Element identElm = prntIdentElm.element("Identifier");
        if (identElm != null) {
            Attribute refAttr = identElm.attribute("Ref");
            if (refAttr != null) {
                ldmRelationship.setParentIdentifierOid(refAttr.getText());
            }
        }
    }

    return ldmRelationship;
}
展开阅读全文

没有更多推荐了,返回首页