IFileFile与实体转换
Posted soy-technology
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IFileFile与实体转换相关的知识,希望对你有一定的参考价值。
/** * 根据物理实体文件在开发工程中创建实体文件 */ @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());
以上是关于IFileFile与实体转换的主要内容,如果未能解决你的问题,请参考以下文章