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());
        }
    }

 

转载于:https://www.cnblogs.com/Soy-technology/p/11429850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值