|
@@ -335,7 +335,7 @@ public final class DocxProcessUtil {
|
|
|
* @param omml
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String omml2mml(String omml) {
|
|
|
+ public static String omml2mml(String omml)throws Exception {
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
InputStream stylesheet = ClassLoader.getSystemResourceAsStream(OMML2MML_XSL);
|
|
|
StringReader sr = new StringReader(omml);
|
|
@@ -344,21 +344,16 @@ public final class DocxProcessUtil {
|
|
|
org.w3c.dom.Document document = null;
|
|
|
DocumentBuilder builder = null;
|
|
|
Transformer transformer = null;
|
|
|
- try {
|
|
|
- builder = factory.newDocumentBuilder();
|
|
|
- document = builder.parse(ommldata);
|
|
|
- TransformerFactory tFactory = new TransformerFactoryImpl();
|
|
|
- StreamSource stylesource = new StreamSource(stylesheet);
|
|
|
- transformer = tFactory.newTransformer(stylesource);
|
|
|
- DOMSource source = new DOMSource(document);
|
|
|
- StreamResult result = new StreamResult(writer);
|
|
|
- transformer.transform(source, result);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(sr);
|
|
|
- IOUtils.closeQuietly(writer);
|
|
|
- }
|
|
|
+ builder = factory.newDocumentBuilder();
|
|
|
+ document = builder.parse(ommldata);
|
|
|
+ TransformerFactory tFactory = new TransformerFactoryImpl();
|
|
|
+ StreamSource stylesource = new StreamSource(stylesheet);
|
|
|
+ transformer = tFactory.newTransformer(stylesource);
|
|
|
+ DOMSource source = new DOMSource(document);
|
|
|
+ StreamResult result = new StreamResult(writer);
|
|
|
+ transformer.transform(source, result);
|
|
|
+ IOUtils.closeQuietly(sr);
|
|
|
+ IOUtils.closeQuietly(writer);
|
|
|
String mml = writer.toString();
|
|
|
mml = ("<mml:math" + StringUtils.substringAfter(mml, "<mml:math")).replace("mml:", "");
|
|
|
return mml;
|
|
@@ -370,25 +365,20 @@ public final class DocxProcessUtil {
|
|
|
* @param htmlStr
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String formatHtml(String htmlStr) {
|
|
|
- try {
|
|
|
- org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
- Elements divs = doc.select("div[class='document");
|
|
|
- doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("span").stream().forEach(element -> {
|
|
|
- if (!element.hasText())
|
|
|
- element.remove();
|
|
|
- });
|
|
|
- if (divs.size() > 0) {
|
|
|
- htmlStr = divs.html();
|
|
|
- } else {
|
|
|
- htmlStr = doc.html();
|
|
|
- }
|
|
|
- return htmlStr;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ public static String formatHtml(String htmlStr) throws Exception {
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
+ Elements divs = doc.select("div[class='document");
|
|
|
+ doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("span").stream().forEach(element -> {
|
|
|
+ if (!element.hasText())
|
|
|
+ element.remove();
|
|
|
+ });
|
|
|
+ if (divs.size() > 0) {
|
|
|
+ htmlStr = divs.html();
|
|
|
+ } else {
|
|
|
+ htmlStr = doc.html();
|
|
|
}
|
|
|
return htmlStr;
|
|
|
}
|
|
@@ -399,7 +389,7 @@ public final class DocxProcessUtil {
|
|
|
* @param htmlStr
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getTextInHtml(String htmlStr) {
|
|
|
+ public static String getTextInHtml(String htmlStr){
|
|
|
try {
|
|
|
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
String textStr = "";
|
|
@@ -420,37 +410,31 @@ public final class DocxProcessUtil {
|
|
|
* @param htmlPath
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String formatHtmlByPath(String htmlPath) {
|
|
|
+ public static String formatHtmlByPath(String htmlPath) throws Exception {
|
|
|
String htmlStr = "";
|
|
|
- File htmlFile;
|
|
|
- try {
|
|
|
- htmlFile = new File(htmlPath);
|
|
|
- org.jsoup.nodes.Document doc = Jsoup.parse(htmlFile, ENCODING);
|
|
|
- Elements divs = doc.select("div[class='document']");
|
|
|
- doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
- doc.select("span").stream().forEach(element -> {
|
|
|
- if (!element.hasText() && element.childNodeSize() == 0) {
|
|
|
- element.remove();
|
|
|
- }
|
|
|
- });
|
|
|
- Elements imgs = doc.select("img");
|
|
|
- for (org.jsoup.nodes.Element img : imgs) {
|
|
|
- String imgSrc = img.attr("src");
|
|
|
- if (!StringUtils.isEmpty(imgSrc)) {
|
|
|
- String imgBase64 = new String(getBase64ByPath(htmlFile.getParent() + "/" + imgSrc));
|
|
|
- img.attr("src", BASE64_HEADER + imgBase64);
|
|
|
- }
|
|
|
+ File htmlFile = new File(htmlPath);
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlFile, ENCODING);
|
|
|
+ Elements divs = doc.select("div[class='document']");
|
|
|
+ doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
+ doc.select("span").stream().forEach(element -> {
|
|
|
+ if (!element.hasText() && element.childNodeSize() == 0) {
|
|
|
+ element.remove();
|
|
|
}
|
|
|
- if (divs.size() > 0) {
|
|
|
- htmlStr = divs.html();
|
|
|
- } else {
|
|
|
- htmlStr = doc.html();
|
|
|
+ });
|
|
|
+ Elements imgs = doc.select("img");
|
|
|
+ for (org.jsoup.nodes.Element img : imgs) {
|
|
|
+ String imgSrc = img.attr("src");
|
|
|
+ if (!StringUtils.isEmpty(imgSrc)) {
|
|
|
+ String imgBase64 = new String(getBase64ByPath(htmlFile.getParent() + "/" + imgSrc));
|
|
|
+ img.attr("src", BASE64_HEADER + imgBase64);
|
|
|
}
|
|
|
- return htmlStr;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (divs.size() > 0) {
|
|
|
+ htmlStr = divs.html();
|
|
|
+ } else {
|
|
|
+ htmlStr = doc.html();
|
|
|
}
|
|
|
return htmlStr;
|
|
|
}
|
|
@@ -461,26 +445,21 @@ public final class DocxProcessUtil {
|
|
|
* @param mathMlStr
|
|
|
* @return
|
|
|
*/
|
|
|
- public static byte[] convertMathml2Img(String mathMlStr) {
|
|
|
+ public static byte[] convertMathml2Img(String mathMlStr) throws Exception {
|
|
|
File xmlFile = null;
|
|
|
File imgFile = null;
|
|
|
FileOutputStream xmlFos = null;
|
|
|
byte[] base64Byte = new byte[0];
|
|
|
- try {
|
|
|
- xmlFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
- imgFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
- xmlFos = new FileOutputStream(xmlFile);
|
|
|
- IOUtils.write(mathMlStr, xmlFos, ENCODING);
|
|
|
- xmlFos.flush();
|
|
|
- Converter.getInstance().convert(xmlFile, imgFile, IMG_OUT_TYPE, IMG_LAYOUT);
|
|
|
- base64Byte = IOUtils.toByteArray(new FileInputStream(imgFile));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(xmlFos);
|
|
|
- FileUtils.deleteQuietly(xmlFile);
|
|
|
- FileUtils.deleteQuietly(imgFile);
|
|
|
- }
|
|
|
+ xmlFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
+ imgFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
+ xmlFos = new FileOutputStream(xmlFile);
|
|
|
+ IOUtils.write(mathMlStr, xmlFos, ENCODING);
|
|
|
+ xmlFos.flush();
|
|
|
+ Converter.getInstance().convert(xmlFile, imgFile, IMG_OUT_TYPE, IMG_LAYOUT);
|
|
|
+ base64Byte = IOUtils.toByteArray(new FileInputStream(imgFile));
|
|
|
+ IOUtils.closeQuietly(xmlFos);
|
|
|
+ FileUtils.deleteQuietly(xmlFile);
|
|
|
+ FileUtils.deleteQuietly(imgFile);
|
|
|
return base64Byte;
|
|
|
}
|
|
|
|
|
@@ -490,21 +469,16 @@ public final class DocxProcessUtil {
|
|
|
* @param imgFilePath
|
|
|
* @return
|
|
|
*/
|
|
|
- public static byte[] getBase64ByPath(String imgFilePath) {
|
|
|
+ public static byte[] getBase64ByPath(String imgFilePath) throws Exception {
|
|
|
InputStream is = null;
|
|
|
byte[] base64Byte = new byte[0];
|
|
|
byte[] imgByte;
|
|
|
File imgFile = new File(imgFilePath);
|
|
|
- try {
|
|
|
- is = new FileInputStream(imgFile);
|
|
|
- imgByte = IOUtils.toByteArray(is);
|
|
|
- base64Byte = Base64.encodeBase64(imgByte);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(is);
|
|
|
- FileUtils.deleteQuietly(imgFile);
|
|
|
- }
|
|
|
+ is = new FileInputStream(imgFile);
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
+ IOUtils.closeQuietly(is);
|
|
|
+ FileUtils.deleteQuietly(imgFile);
|
|
|
return base64Byte;
|
|
|
}
|
|
|
|
|
@@ -514,19 +488,14 @@ public final class DocxProcessUtil {
|
|
|
* @param imgFile
|
|
|
* @return
|
|
|
*/
|
|
|
- public static byte[] getBase64ByFile(File imgFile) {
|
|
|
+ public static byte[] getBase64ByFile(File imgFile) throws Exception {
|
|
|
InputStream is = null;
|
|
|
byte[] base64Byte = new byte[0];
|
|
|
byte[] imgByte;
|
|
|
- try {
|
|
|
- is = new FileInputStream(imgFile);
|
|
|
- imgByte = IOUtils.toByteArray(is);
|
|
|
- base64Byte = Base64.encodeBase64(imgByte);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(is);
|
|
|
- }
|
|
|
+ is = new FileInputStream(imgFile);
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
+ IOUtils.closeQuietly(is);
|
|
|
return base64Byte;
|
|
|
}
|
|
|
|
|
@@ -569,7 +538,8 @@ public final class DocxProcessUtil {
|
|
|
|
|
|
for (Object rChild : rContent) {
|
|
|
|
|
|
- rChild = ((JAXBElement<?>) rChild).getValue();
|
|
|
+ if (rChild instanceof JAXBElement)
|
|
|
+ rChild = ((JAXBElement<?>) rChild).getValue();
|
|
|
|
|
|
if (rChild.getClass().equals(Text.class)) {
|
|
|
++index;
|
|
@@ -632,32 +602,27 @@ public final class DocxProcessUtil {
|
|
|
* @param dataMap
|
|
|
* @param fileName
|
|
|
*/
|
|
|
- public static void exportWord(Map dataMap, String fileName, String templatePath) {
|
|
|
+ public static void exportWord(Map dataMap, String fileName, String templatePath)throws Exception {
|
|
|
Writer out = null;
|
|
|
- try {
|
|
|
- // 创建配置实例
|
|
|
- Configuration configuration = new Configuration(Configuration.VERSION_2_3_25);
|
|
|
- // 设置编码
|
|
|
- configuration.setDefaultEncoding("UTF-8");
|
|
|
- // 设置ftl模板路径
|
|
|
- configuration.setClassForTemplateLoading(DocxProcessUtil.class, "/");
|
|
|
- // 获取模板
|
|
|
- Template template = configuration.getTemplate(templatePath, ENCODING);
|
|
|
- // 输出文件
|
|
|
- File outFile = new File(TEMP_FILE_EXP + fileName + DOCX_SUFFIX);
|
|
|
-
|
|
|
- // 将模板和数据模型合并生成文件
|
|
|
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), ENCODING));
|
|
|
- // 生成文件
|
|
|
- template.process(dataMap, out);
|
|
|
-
|
|
|
- out.flush();
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
- }
|
|
|
+ // 创建配置实例
|
|
|
+ Configuration configuration = new Configuration(Configuration.VERSION_2_3_25);
|
|
|
+ // 设置编码
|
|
|
+ configuration.setDefaultEncoding("UTF-8");
|
|
|
+ // 设置ftl模板路径
|
|
|
+ configuration.setClassForTemplateLoading(DocxProcessUtil.class, "/");
|
|
|
+ // 获取模板
|
|
|
+ Template template = configuration.getTemplate(templatePath, ENCODING);
|
|
|
+ // 输出文件
|
|
|
+ File outFile = new File(TEMP_FILE_EXP + fileName + DOCX_SUFFIX);
|
|
|
+
|
|
|
+ // 将模板和数据模型合并生成文件
|
|
|
+ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), ENCODING));
|
|
|
+ // 生成文件
|
|
|
+ template.process(dataMap, out);
|
|
|
+
|
|
|
+ out.flush();
|
|
|
+
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -666,7 +631,7 @@ public final class DocxProcessUtil {
|
|
|
* @param dataMap
|
|
|
* @param fileName
|
|
|
*/
|
|
|
- public static void exportPaper(Map dataMap, String fileName) {
|
|
|
+ public static void exportPaper(Map dataMap, String fileName) throws Exception {
|
|
|
exportWord(dataMap, fileName, PAPER_TEMPLATE);
|
|
|
}
|
|
|
|
|
@@ -676,7 +641,7 @@ public final class DocxProcessUtil {
|
|
|
* @param dataMap
|
|
|
* @param fileName
|
|
|
*/
|
|
|
- public static void exportAnswer(Map dataMap, String fileName) {
|
|
|
+ public static void exportAnswer(Map dataMap, String fileName) throws Exception {
|
|
|
exportWord(dataMap, fileName, ANSWER_TEMPLATE);
|
|
|
}
|
|
|
|
|
@@ -824,7 +789,7 @@ public final class DocxProcessUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static String html2Docx(WordprocessingMLPackage wordMLPackage,String html)throws Exception{
|
|
|
+ public static String html2Docx(WordprocessingMLPackage wordMLPackage,String html)throws Exception {
|
|
|
initPkgImage(wordMLPackage);
|
|
|
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
|
|
|
String wordMl = "";
|