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);
}*/
}