/**
* 根据物理实体文件在开发工程中创建实体文件
*/
@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());
}
}

IFile、File与实体转换的更多相关文章

  1. C# 获取config文件 实体转换

    随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...

  2. DataTable转List<Model>通用类【实体转换辅助类】

    /// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...

  3. HBaseConvetorUtil 实体转换工具

    HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil {        /**    * @Title: convetor    * @De ...

  4. Datatable转实体 实体转换辅助类

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  5. html实体转换

    摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...

  6. C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument

    https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx XElement.Parse 方法 ( ...

  7. .NET 实体转换辅助类

    /// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...

  8. C#中实体集合和实体转换成相应的string、XDocument、XElement

    C#中实体集合和实体转换成相应的string.XDocument.XElement public class SimpleXmlConverter { public static string ToX ...

  9. .Net Core2.2 使用 AutoMapper进行实体转换

    一.遇到的问题 在. Core Api 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...

随机推荐

  1. MyBatisPlus乐观锁,乐观锁竟然如此简单

    乐观锁 在便是过程中,我们经常会被问到乐观锁,悲观锁,都非常简单 乐观锁:顾名思义,思想十分乐观,总是认为不会出现问题,无论什么都不去上锁!如果出现了问题,就再更新测试 悲观锁:顾明思义,思想十分悲观 ...

  2. 我搭的神经网络不work该怎么办!看看这11条新手最容易犯的错误

    1. 忘了数据规范化 2. 没有检查结果 3. 忘了数据预处理 4. 忘了正则化 5. 设置了过大的批次大小 6. 使用了不适当的学习率 7. 在最后一层使用了错误的激活函数 8. 网络含有不良梯度 ...

  3. 利用maven的MyBatis Generator 插件自动创建代码

    1.首先创建Maven工程 2.修改pom.xml文件代码如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  4. C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...

  5. C#LeetCode刷题之#1-两数之和(Two Sum)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3762 访问. 给定一个整数数组和一个目标值,找出数组中和为目标值 ...

  6. JavaScript Babel说明

    babel插件只是去唤醒 @babel/core中的转换过程 转换模块需要手动安装 npm install @babel/core 转换方式需要安装 @babel/preset-env babel默认 ...

  7. 详解POW工作量证明原理

    原文地址  来自 微信公众号 区块链大师 POW工作量证明(英文全称为Proof of Work)早在比特币出现之前就已经有人探索,常见的是利用HASH运算的复杂度进行CPU运算实现工作量确定,当然你 ...

  8. go 字符串

    目录 前言 1.声明/赋值 2.遍历 3.操作 1.截取 2.修改 3.连接 4.比较 5.查长 6.格式化输出 4.字符串优势 跳转 前言 不做文字的搬运工,多做灵感性记录 这是平时学习总结的地方, ...

  9. Android开发学习进程0.18 SharePreference的使用 AIDL

    SharePreference SharePreference是一种持久化存储手段,使用场景很多,如第一次打开时加载的用户协议等.适合小数据单进程的应用.将数据以键值对的形式存储在XML中. 使用方式 ...

  10. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...