xmind Java解析

xmind解析

支持xmind 23及以下

public class XmindParser {
    public static final String xmindZenJson = "content.json";
    public static final String xmindLegacyContent = "content.xml";
    public static final String xmindLegacyComments = "comments.xml";

    public static JsonRootBean parseBean(String xmindFile) throws IOException, ArchiveException, DocumentException {
        String context = parseJson(xmindFile);
        JsonRootBean jsonRootBean = JSONUtil.toBean(context, JsonRootBean.class);
        return jsonRootBean;
    }
    /**
     * 解析脑图文件,返回content整合后的内容
     *
     * @param xmindFile
     * @return
     * @throws IOException
     * @throws ArchiveException
     */
    public static String parseJson(String xmindFile) throws IOException, ArchiveException, DocumentException {

        String tempId = IDFactory.createId();
        String res = Workspace.getTempDir("xmind/" + tempId);
        File tempDir = new File(res);
        FileUtils.ensureDirectory(tempDir);
        DirectoryStorage tempStorage = new DirectoryStorage(tempDir);
        try {
            InputStream stream = new FileInputStream(xmindFile);
            ZipInputStream zin = new ZipInputStream(stream);

            try {
                FileUtils.extractZipFile(zin, tempStorage.getOutputTarget());
            } finally {
                zin.close();
            }
            String content;
            if (isXmindZen(res, xmindFile)) {
                content = getXmindZenContent(xmindFile,res);
            } else {
                content = getXmindLegacyContent(xmindFile,res);
            }
            return content;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            tempStorage.clear();
        }
        return null;
    }



    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空,可以删除
        return dir.delete();
    }


    /**
     * @return
     */
    public static String getXmindZenContent(String xmindFile,String extractFileDir) throws IOException, ArchiveException {
        String fileName = extractFileDir+"/" + xmindZenJson;
        String content = getFileContent(fileName);
        return content;
    }

    public static String getFileContent(String fileName) throws IOException {
        File file;
        try {
            file = new File(fileName);
        } catch (Exception e) {
            throw new RuntimeException("找不到该文件");
        }
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReder = new BufferedReader(fileReader);
        StringBuilder stringBuffer = new StringBuilder();
        while (bufferedReder.ready()) {
            stringBuffer.append(bufferedReder.readLine());
        }
        //打开的文件需关闭,在unix下可以删除,否则在windows下不能删除(file.delete())
        bufferedReder.close();
        fileReader.close();
        return stringBuffer.toString();
    }
    /**
     * @return
     */
    public static String getXmindLegacyContent(String xmindFile,String extractFileDir) throws IOException, ArchiveException, DocumentException {
        List<String> keys = new ArrayList<>();
        keys.add(xmindLegacyContent);
        keys.add(xmindLegacyComments);
        Map<String, String> map = ZipUtils.getContents(keys, xmindFile,extractFileDir);

        String contentXml = map.get(xmindLegacyContent);
        String commentsXml = map.get(xmindLegacyComments);
        String xmlContent = XmindLegacy.getContent(contentXml, commentsXml);

        return xmlContent;
    }


    private static boolean isXmindZen(String res, String xmindFile) throws IOException, ArchiveException {
        //解压
        File parent = new File(res+"/" + xmindZenJson);
        if (parent.exists()) {
            return true;
        }
        return false;
    }

   /* public static void main(String[] args) throws IOException, ArchiveException, DocumentException {
        String fileName = "doc/XmindZen解析.xmind";
        XmindParser xmindParser = new XmindParser();
        String res = xmindParser.parser(fileName);
        JsonRootBean jsonRootBean = JSON.parseObject(res, JsonRootBean.class);
        System.out.println(jsonRootBean);
    }*/

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值