IFile、File与实体转换
/**
* 根据物理实体文件在开发工程中创建实体文件
*/
@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与实体转换的更多相关文章
- C# 获取config文件 实体转换
随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...
- DataTable转List<Model>通用类【实体转换辅助类】
/// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...
- HBaseConvetorUtil 实体转换工具
HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil { /** * @Title: convetor * @De ...
- Datatable转实体 实体转换辅助类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- html实体转换
摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...
- C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument
https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx XElement.Parse 方法 ( ...
- .NET 实体转换辅助类
/// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...
- C#中实体集合和实体转换成相应的string、XDocument、XElement
C#中实体集合和实体转换成相应的string.XDocument.XElement public class SimpleXmlConverter { public static string ToX ...
- .Net Core2.2 使用 AutoMapper进行实体转换
一.遇到的问题 在. Core Api 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...
随机推荐
- Bytom 储蓄分红 DAPP 开发指南
储蓄分红DAPP 储蓄分红合约简介 储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润.用户可以在 ...
- 微信小程序 progress 进度条 内部圆角及内部条渐变色
微信小程序progress进度条内部圆角及渐变色 <view class="progress-box"> <progress percent="80&q ...
- Java Redis系列3(Jedis的使用+jedis连接池技术)
Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...
- JavaScript 中 Blob对象的初步认识
Blob Binary Large Object的缩写,二进制大对象 虽然在前端中开发并不常见,但是实际上MySql数据库中,可以通过设置一个Blob类型的数据来存储一个Blob对象的内容 语法 le ...
- Linux学习日志——基本指令②
文章目录 Linux学习日志--基本指令② 前言 touch cp (copy) mv (move) rm vim 输出重定向(> 或 >>) cat df(disk free) f ...
- BLE GAP 协议和 GATT 协议
BLE GAP 协议和 GATT 协议 最近要打算学习 Blufi 协议进行蓝牙配置,其中必然使用 GAP 协议和 GATT 协议,于是进行重新学习一番. BLE 是一个 Bluetooth SIG ...
- HTML5+css3 的开心网游戏页面
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 记好这 24 个 ES6 方法,用来解决实际开发的 JS 问题
本文主要介绍 24 中 es6 方法,这些方法都挺实用的,本本请记好,时不时翻出来看看. 1.如何隐藏所有指定的元素 const hide = (el) => Array.from(el).fo ...
- Git的使用方法及IDEA与Git的集成
一.Git的环境配置 1.Git软件下载 (下载地址:https://git-scm.com/)由于国外的网站下载的超慢可以使用国内的阿里的开源镜像下载(下载地址:https://npm.taobao ...
- ms14-064漏洞复现
本博客最先发布于我的个人博客,如果方便,烦请移步恰醋的小屋查看,谢谢您! 这是我在实验室学习渗透测试的第五个漏洞复现,一个多小时便完成了.学长给的要求只需完成查看靶机信息.在指定位置创建文件夹两项操作 ...