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 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...
随机推荐
- Spring Boot打包瘦身 Docker 使用全过程 动态配置、日志记录配置
springBoot打包的时候代码和jar包打包在同一个jar包里面,会导致jar包非常庞大,在不能连接内网的时候调试代码,每次只改动了java代码就需要把所有的jar包一起上传,导致传输文件浪费了很 ...
- C#LeetCode刷题之#190-颠倒二进制位(Reverse Bits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4050 访问. 颠倒给定的 32 位无符号整数的二进制位. 输入: ...
- Unity3D制作类似吃鸡的小地图
先看效果图: 实现的效果就是右上角的一个小地图,会随着人物的移动而移动,显示人物的方向,并且可以展示地图设定范围的其他的玩家 制作起来也很简单,不需要任何代码.主要原理就是先创建Render Text ...
- 设计模式:装饰者模式介绍及代码示例 && JDK里关于装饰者模式的应用
0.背景 来看一个项目需求:咖啡订购项目. 咖啡种类有很多:美式.摩卡.意大利浓咖啡: 咖啡加料:牛奶.豆浆.可可. 要求是,扩展新的咖啡种类的时候,能够方便维护,不同种类的咖啡需要快速计算多少钱,客 ...
- Nordic 52840-Timer定时器学习问题(一)
今天在ble_app_blinky例程中移植定时器驱动,在编译过程中报出了两个错误,在此记录一下. 1. 在nRF_Dreivers中添加nrfx_timer.c文件 选中“nRF_Dreivers ...
- Vue源码分析之数据驱动
响应式特点 数据响应式 修改数据时,视图自动更新,避免繁琐Dom操作,提高开发效率 双向绑定 数据改变,视图随之改变.视图改变,数据随之改变 数据驱动 开发时仅需要关注数据本身,不需要关心数据如何渲染 ...
- Redis高级项目实战,都0202年了,还不会Redis?
导读 大家都听过1万小时定律,可事实真的是这样吗?做了1万小时的CRUD,不还只会CRUD吗,这年头不适当的更新自身下技术栈,出门和别人聊天吹牛的时候,都没拿不出手,(⊙o⊙)…Redis没入门的童鞋 ...
- 归并排序-Python实现
归并排序(MergeSort) 归并排序(英语:Merge sort,或mergesort),是创建在归并操作上的一种有效的排序算法,效率为 O(n\log n)(大O符号).1945年由约翰·冯·诺 ...
- 个人项目WC.exe Node.js+electron实现
前言 实现语言:Javascript 编译工具:webstorm GitHub:https://github.com/NPjuan/WC.git 项目要求 wc.exe 是一个常见的工具,它能统计文本 ...
- linux上的deepin-qq不能显示图片解决方法
在贴吧发现的一个方法 在终端输入以下命令,重新打开QQ即可 sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.piv ...