/**
* 根据物理实体文件在开发工程中创建实体文件
*/
@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. 【计算机算法设计与分析】——NP

    时间复杂度 时间复杂度并不是表示一个程序解决问题需要花多少时间,而是当问题规模扩大后,程序需要的时间长度增长得有多快.也就是说,对于高速处理数据的计算机来说,处理某一个特定数据的效率不能衡量一个程序的 ...

  2. 【LeetCode/LintCode 题解】约瑟夫问题 · Joseph Problem

    n个人按顺序围成一圈(编号为1~n),从第1个人从1开始报数,报到k的人出列,相邻的下个人重新从1开始报数,报到k的人出列,重复这个过程,直到队伍中只有1个人为止,这就是约瑟夫问题.现在给定n和k,你 ...

  3. Vue element-ui el-table阻止行选事件

    我们经常会在某个table末尾加上操作列来放置button来处理跳转和其他的逻辑 那么当点击button的时候同样也会执行在el-table 设置的 @row-click="handleRo ...

  4. Web优化躬行记(4)——用户体验和工具

    一.用户体验 用户体验(UE/UX)是指一个人使用一个特定产品.系统或服务时的行为.情绪与态度,还包含用户对于系统的功能.易用和效率的感受,因此用户体验在本质上可以视为一个人对于系统的主观感受与主观想 ...

  5. JavaScript基础-02

    1. 六种数据类型: string字符串:number数值:boolean布尔值:null空值:undefined 未定义:object对象 基本数据类型(值类型): string字符串:number ...

  6. git操作练习

    github账号注册很久了,使用过idea的版本管理,但是命令行还不会.正好新写了一个小项目,练习一下如何上传到github. @ 目录 新建github仓库 安装git 上传项目 1.创建本地版本库 ...

  7. 喵的Unity游戏开发之路 - 轨道摄像机

    前言        很多童鞋没有系统的Unity3D游戏开发基础,也不知道从何开始学.为此我们精选了一套国外优秀的Unity3D游戏开发教程,翻译整理后放送给大家,教您从零开始一步一步掌握Unity3 ...

  8. linux zip压缩文件忽略指定的文件夹

    zip -r productImages.zip ./* -x "cache**" 压缩时,会忽略cache下的所有文件及文件夹

  9. CentOS下删除物理磁盘,删除LVM

    1.删除 dmsetup remove LV_name 2.vgreduce VG_name --removemissing 3.vgremove VG_name 4.pvremove disk

  10. Robot Framework(1)——环境搭建及安装

    一.了解Robot Framework Robot Framework不是一个测试工具,准确来说,它是一个自动化测试框架,或者说它是一个自动化测试平台 特性如下: 1.支持关键字驱动.数据驱动和行为驱 ...