/**
* 根据物理实体文件在开发工程中创建实体文件
*/
@Override
public void getEntityFilesByErFile(IFile erfile, IFolder entityFolder) {
if (null == erfile || null == entityFolder) {
ERDiagramActivator.showErrorDialog("ER图表为空!");
return;
} // 通过file反编译获取diagram,再创建实体,通过流写入文件,到folder路径下
File tradeFile = erfile.getLocation().toFile(); // ifile转换成file
byte[] fileByteArray = this.File2ByteArray(tradeFile);// 文件转成二进制数据
if (null == fileByteArray) {
return;
}
// 将二进制数组转换成对象
ERDiagram resultDiagram = null;
try {
resultDiagram = (ERDiagram) this.restore(fileByteArray);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // 设置er图中所有表格转换成实体对象
for (final ERTable table : resultDiagram.getDiagramContents()
.getContents().getTableSet().getList()) { final String className = table.getPhysicalName(); Document document = DocumentHelper.createDocument();
Element root = document.addElement("entity");
createElement(root, "schema", "");
createElement(root, "name", className);
createElement(root, "objName", className);
createElement(root, "strategy", ""); createColumns(root, table); System.out.println(XmlUtils.formatXML(document.asXML(), true));
InputStream in = EntityUtils.parseEntity(document.asXML()); String fileName = className + "." + Constants.FILE_EXT_EIX;
IFile ifile = entityFolder.getFile(fileName);
// 設置實體對象字段值
try {
if (!ifile.exists()) {
ifile.create(null, true, null);
}
ifile.setContents(in, IFile.FORCE, null);
// entityFolder.copy((IPath) new Path(fileName), IFile.FORCE,
// null);
} catch (CoreException e) {
e.printStackTrace();
}
}
} // 将文件转换成byte数组
public byte[] File2ByteArray(File tradeFile) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(tradeFile);// 文件读取成流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[(int) tradeFile.length()];// 文件大小长度的数组
if(b.length == 0){
ERDiagramActivator.showErrorDialog("ER文件为空!");
throw new IOException("ER文件为空!");
}
int n;
// 文件没有读取完,一直读取文件,并且写入到数组
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
} // 把二进制数组的数据转回对象
public Object restore(byte[] b) throws ClassNotFoundException, IOException {
if(null == b)
return null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
// 读取二进制数据并转换成对象
bis = new ByteArrayInputStream(b);
ois = new ObjectInputStream(bis);
return ois.readObject();
} finally {
if (ois != null) {
ois.close();
}
if (bis != null) {
bis.close();
}
}
} // 創建元素節點
private Element createElement(Element element, String tag, String value) {
Element e = element.addElement(tag);
e.addText(value);
return e;
} // 得到当前sql类型数据的规范名称
private String getFullClassName(final SqlType type) {
if (type == null) {
return "";
}
final Class clazz = type.getJavaClass();
final String name = clazz.getCanonicalName();
return name;
} // 创建表格中列数据
private void createColumns(Element root, ERTable table) {
Element columns = createElement(root, "columns", ""); List<NormalColumn> columnsList = table.getExpandedColumns();
String length = "";
String type = "";
for (NormalColumn column : columnsList) {
Element columnEle = createElement(columns, "column", "");
createElement(columnEle, "primaryKey", BooleanUtils
.toStringTrueFalse(column.isPrimaryKey()).toLowerCase());
createElement(columnEle, "physicalName", column.getPhysicalName());
createElement(columnEle, "logicName", column.getLogicalName());
type = ObjectUtils.toString(column.getType());
if (column.getWord() != null
&& column.getWord().getTypeData() != null
&& column.getWord().getTypeData().getLength() != null) {
length = Integer.toString(column.getWord().getTypeData()
.getLength());
}
type = type.replace("(n)", "(" + length + ")");
createElement(columnEle, "type", type);
createElement(columnEle, "length", length);
createElement(columnEle, "notNull",
BooleanUtils.toStringTrueFalse(column.isNotNull())
.toLowerCase());
createElement(columnEle, "mapType",
getFullClassName(column.getType()));
createElement(columnEle, "comment", column.getDescription());
}
}

最新文章

  1. Redis常用命令(String数据类型)
  2. &lt;2048&gt;游戏问卷调查心得与体会
  3. 09.C#委托转换和匿名方法(五章5.1-5.4)
  4. jQuery点击图片弹出大图遮罩层
  5. php cookie不刷新及时生效的实现代码
  6. java基础之导入(药师点评)
  7. 不管肉鸡,还是代理,CC识别就封杀!
  8. Unable to find setter method for attribute: 属性名
  9. fastJson将json字符串转换为map
  10. SQL优化二(Sql性能调优)
  11. tar (child): jdk-7u71-linux-x64.tar.gz:无法 open: 没有那个文件或目录
  12. Virtualbox开机启动,service命令管理
  13. 《前端之路》之 Babel 下一代 JavaScript 语法编译器
  14. Leetcode 137. 只出现一次的数字 II - 题解
  15. 【Hibernate学习笔记-5.1】使用@Transient修饰不想持久保存的属性
  16. 《Linux内核精髓:精通Linux内核必会的75个绝技》目录
  17. restful 注解 总结 (比较完整的):http://www.xuetimes.com/archives/388 , https://www.cnblogs.com/chen-lhx/p/5599806.html
  18. C# 生成四位数字字母混合验证码
  19. ElasticSearch关联查找
  20. 《Go并发编程实战》第2版 紧跟Go的1.8版本号

热门文章

  1. Libsvm java工程实践
  2. Eclipse工具的简单使用
  3. python设计模式之状态模式
  4. Typora markdown代码块显示序号
  5. c cpp编程用到的系统边角与其拾遗
  6. Mysql多条数据以一条展示和判断某字符是否在某字段
  7. python 11 类与对象
  8. vscode下终端返回中文乱码
  9. Vue生命周期,我奶奶看了都懂了
  10. Robust and Communication-Efficient Federated Learning from Non-IID Data