一、用JAVA代码把数据库的PDM文件转换成相应格式的XML文件,代码如下:
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
public class PdmToXml {
private static SAXReader reader = new SAXReader();
private static Document doc = null;
private static String mddir = "D:/tmp/"; // 存放转换结果的主目录
// private static String PRE = "";// 生成文件的文件名tables.xml
// 获取主键
private static Element getPrimaryKeyOfId(String id) {
List list = doc.selectNodes("Model/o:RootObject/c:Children/o:Model/c:Tables/o:Table/c:Keys/o:Key");
for (Object obj : list) {
Element e = (Element) obj;
if (id.equals(e.attributeValue("Id"))) {
return e;
}
}
return null;
}
// 获取列名
private static String getColumnFieldOfId(String id) {
List list = doc
.selectNodes("Model/o:RootObject/c:Children/o:Model/c:Tables/o:Table/c:Columns/o:Column");
for (Object obj : list) {
Element e = (Element) obj;
if (id.equals(e.attributeValue("Id"))) {
return e.element("Code").getTextTrim();
}
}
return null;
}
// 获取字段的类型,应该加上精度,日期
private static String getTypeString(String value) {
if (value != null) {
if (value.toUpperCase().contains("CHAR")
|| value.toUpperCase().contains("CLOB")) {
return "string";
}
if (value.toUpperCase().contains("TINYINT")
|| value.toUpperCase().contains("BOOL")) {
return "boolean";
}
if (value.toUpperCase().contains("BIGINT")) {
return "long";
}
if (value.toUpperCase().contains("FLOAT")) {
return "float";
}
if (value.toUpperCase().contains("DOUBLE")
|| value.toUpperCase().matches("DECIMAL\\(\\d+,[1-9]\\)")
|| value.toUpperCase().matches("NUMERIC\\(\\d+,[1-9]\\)")
|| value.toUpperCase().matches("NUMBER\\(\\d+,[1-9]\\)")) {
return "double";
}
if (value.toUpperCase().contains("TIME")
|| value.toUpperCase().contains("DATE")) {
return "date";
}
if (value.toUpperCase().contains("BLOB")
|| value.toUpperCase().contains("BINARY")) {
return "binary";
}
// 放在最后
if (!value.toUpperCase().matches("DECIMAL\\(\\d+,[1-9]\\)")
&& !value.toUpperCase().matches("NUMERIC\\(\\d+,[1-9]\\)")
&& !value.toUpperCase().matches("NUMBER\\(\\d+,[1-9]\\)")
|| value.toUpperCase().contains("INT")
|| value.toUpperCase().contains("YEAR")) {
return "integer";
}
}
return "string";
}
// 是否主键
private static boolean isPrimaryKey(Element e, String id) {
boolean rtn = false;
if (e == null) {
return false;
}
if (id.equals(e.element("Key").attributeValue("Ref"))) {
rtn = true;
}
return rtn;
}
// 得到所有的表
private static List<Element> getReferences(String tableId) {
List<Element> rtn = new ArrayList<Element>();
List list = doc
.selectNodes("Model/o:RootObject/c:Children/o:Model/c:References/o:Reference");
for (Object obj : list) {
Element e = (Element) obj;
if (tableId.equals(e.element("ChildTable").element("Table")
.attributeValue("Ref"))) {
rtn.add(e);
}
}
return rtn;
}
// 获取表名
private static Element getTableByPDMOfId(String tableId) {
List list = doc
.selectNodes("Model/o:RootObject/c:Children/o:Model/c:Tables/o:Table");
for (Object obj : list) {
Element e = (Element) obj;
if (tableId.equals(e.attributeValue("Id"))) {
return e;
}
}
return null;
}
/*
* @param filename 要解析的pdm文件
*
* @param createfile paginateFile 为 false 时,所生成的文件名字,当 paginateFile =
* true时,createfile 为 null
*
* @param paginateFile 是否根据表生成对应的文件(一个表生成一个文件,文件名为表的名字 + model.xml) true
* 表示一个表生成一个 model.xml 文件,false 表示,将pdm生成一个大文件
*/
public static void createModelXml(String filename, String createfile,boolean paginateFile) {
try {
doc = reader.read(new FileReader(filename));
List list = doc
.selectNodes("Model/o:RootObject/c:Children/o:Model/c:Tables/o:Table");
Document wdoc = null;
XMLWriter writer = null;
Element root = null;
if (paginateFile == false) {
wdoc = DocumentHelper.createDocument();
root = wdoc.addElement("tables");
//wdoc = DocumentHelper.createDocument(root);
}
/*
* 当 paginateFile = false时,每一张表都要生成一个对应的XML文件
*/
for (Object o : list) {
Element pdmtable = (Element) o;
if (paginateFile == true) {
wdoc = DocumentHelper.createDocument();
root = wdoc.addElement("tables");
}
Element table = root.addElement("table");
String xmlfilename = pdmtable.element("Code").getTextTrim();
try {
table.addAttribute("name", pdmtable.element("Code").getTextTrim());
//列
Element columns = pdmtable.element("Columns");
if (null == columns) {
throw new RuntimeException(pdmtable.element("Code")
.getTextTrim() + "表在PDM中设置错误");
}
for (Object obj : columns.elements()) {
Element pdmcolumn = (Element) obj;
Element col = table.addElement("column");
col.addAttribute("field", pdmcolumn.element("Code")
.getTextTrim());
col.addAttribute("type", getTypeString(pdmcolumn
.element("DataType").getTextTrim()));
if (null != pdmcolumn.element("Length")) {
col.addAttribute("length",
pdmcolumn.element("Length").getTextTrim());
}
if (null != pdmcolumn.element("Precision")) {
col.addAttribute("precision",
pdmcolumn.element("Precision")
.getTextTrim());
}
col.addAttribute("property", pdmcolumn.element("Code")
.getTextTrim().toLowerCase());
col.addAttribute("label", pdmcolumn.element("Name")
.getTextTrim());
if (pdmcolumn.element("Mandatory") != null
&& "1".equals(pdmcolumn.element("Mandatory")
.getTextTrim())) {
col.addAttribute("required", "true");
}
}
//key的属性
Element key = pdmtable.element("Keys");
if (key != null) {
List keys = key.elements();
for (Object obj : keys) {
Element pdmkey = (Element) obj;
if (!isPrimaryKey(pdmtable.element("PrimaryKey"),
pdmkey.attributeValue("Id"))) {
Element unique = table.addElement("unique");
unique.addAttribute("name",
pdmkey.element("Code").getTextTrim());
Element fields = pdmkey.element("Key.Columns");
List cols = fields.elements("Column");
String uniqueField = "";
for (Object c : cols) {
uniqueField += getColumnFieldOfId(((Element) c)
.attributeValue("Ref")) + ",";
}
if (uniqueField.length() > 0) {
uniqueField = uniqueField.substring(0,
uniqueField.length() - 1);
}
unique.addAttribute("field", uniqueField);
}
}
}
//是否主键
Element primarykey = pdmtable.element("PrimaryKey");
if (primarykey != null) {
Element pkfields = getPrimaryKeyOfId(primarykey
.element("Key").attributeValue("Ref"));
Element fields = pkfields.element("Key.Columns");
if (fields != null) {
Element primary = table.addElement("primary");
primary.addAttribute("name",
pkfields.element("Code").getTextTrim());
List cols = fields.elements("Column");
String pkField = "";
for (Object c : cols) {
pkField += getColumnFieldOfId(((Element) c)
.attributeValue("Ref")) + ",";
}
if (pkField.length() > 0) {
pkField = pkField.substring(0,
pkField.length() - 1);
}
primary.addAttribute("field", pkField);
} else {
System.out.println("注意:表 " + xmlfilename + " 没有主键");
System.out.println("文件 filename 中,表 " + xmlfilename
+ "设计的有问题,请检查该表");
}
}
//外键
List<Element> refertable = getReferences(pdmtable
.attributeValue("Id"));
if (refertable != null && refertable.size() > 0) {// foreign
// key
for (Element e : refertable) {
Element foreign = table.addElement("foreign");
// foreign name
foreign.addAttribute("name",
e.element("ForeignKeyConstraintName")
.getTextTrim());
// foreign column field code
String selffield = e.element("Joins")
.element("ReferenceJoin")
.element("Object2").element("Column")
.attributeValue("Ref");
foreign.addAttribute("field",
getColumnFieldOfId(selffield));
// foreign reference table name
Element tableelement = getTableByPDMOfId(e
.element("ParentTable").element("Table")
.attributeValue("Ref"));
foreign.addAttribute("reference", tableelement
.element("Code").getTextTrim());
// foreign reference table's column field code
String referfiled = e.element("Joins")
.element("ReferenceJoin")
.element("Object1").element("Column")
.attributeValue("Ref");
foreign.addAttribute("referField",
getColumnFieldOfId(referfiled));
}
}
/**
* 当paginateFile 为 true时,生成每张表对应的XML
* 生成 XML 文件 分别生成对应的表的xml中
*/
if (paginateFile == true) {
writer = new XMLWriter(new FileWriter(mddir
+ xmlfilename.toLowerCase() + ".xml"),
new OutputFormat(" ", true));
writer.write(wdoc);
writer.flush();
writer.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* paginateFile == false,将所有的表都生成一个大的XML文件
*/
if (paginateFile == false) {
writer = new XMLWriter(new FileWriter(mddir + createfile
+ ".xml"), new OutputFormat(" ", true));
writer.write(wdoc);
writer.flush();
writer.close();
}
System.out.println("PDM转换为XML文件完成");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filename = "D:/tmp/tables.xml" ;
createModelXml(filename,"tables", false);
}
}