update 文档拆分回滚
This commit is contained in:
+17
-39
@@ -18,6 +18,7 @@ import com.jero.modules.split.vo.TitleNumberVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
||||
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
@@ -128,22 +129,22 @@ public class FileSpiltService {
|
||||
// nowStart.add(String.valueOf(sarFileSplitInfoEO.getStartNumber()));
|
||||
// }
|
||||
// 获取文件地址
|
||||
// String fileId = sarFileSplitInfoEO.getFileId();
|
||||
// LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
|
||||
// queryWrapper.eq(OSSFile::getId, fileId);
|
||||
// queryWrapper.orderByDesc(OSSFile::getCreateTime);
|
||||
// List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
|
||||
// if (CollectionUtils.isEmpty(ossFileList)) {
|
||||
// throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||
// }
|
||||
// OSSFile ossFile = ossFileList.get(0);
|
||||
// String readFilePath = ossFile.getUrl();
|
||||
// if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
|
||||
// throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||
// }
|
||||
// try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
|
||||
String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx";
|
||||
try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
|
||||
String fileId = sarFileSplitInfoEO.getFileId();
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper();
|
||||
queryWrapper.eq(OSSFile::getId, fileId);
|
||||
queryWrapper.orderByDesc(OSSFile::getCreateTime);
|
||||
List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
|
||||
if (CollectionUtils.isEmpty(ossFileList)) {
|
||||
throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||
}
|
||||
OSSFile ossFile = ossFileList.get(0);
|
||||
String readFilePath = ossFile.getUrl();
|
||||
if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) {
|
||||
throw new JeroBootException("当前文件未找到,请重新选择!");
|
||||
}
|
||||
try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
|
||||
//String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx";
|
||||
//try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。
|
||||
ZipSecureFile.setMinInflateRatio(-1.0d);
|
||||
XWPFDocument doc = new XWPFDocument(is);
|
||||
|
||||
@@ -173,29 +174,6 @@ public class FileSpiltService {
|
||||
XWPFParagraph p = (XWPFParagraph) element;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
ParagraphChildOrderManager runOrMaths = new ParagraphChildOrderManager(p);
|
||||
List<Object> childList = runOrMaths.getChildList();
|
||||
|
||||
for (Object child : childList) {
|
||||
//if (child instanceof XWPFRun) {
|
||||
// //处理段落中的文本以及图片
|
||||
// handleParagraphRun(content, (XWPFRun) child, imageParser);
|
||||
//} else
|
||||
if (child instanceof CTOMath) {
|
||||
// 处理word中存在的公式成mathML格式
|
||||
handleParagraphOMath(stringBuilder, (CTOMath) child);
|
||||
if (messageList.size() > 0) {
|
||||
addItermsConditionsMath(messageList.get(messageList.size() - 1), itemValList, stringBuilder.toString());
|
||||
}
|
||||
} else if (child instanceof CTOMathPara) {
|
||||
//处理word中存在的公式
|
||||
handleParagraphOMath(stringBuilder, (CTOMathPara) child);
|
||||
if (messageList.size() > 0) {
|
||||
addItermsConditionsMath(messageList.get(messageList.size() - 1), itemValList, stringBuilder.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (XWPFRun xwrun : p.getRuns()) {
|
||||
VerticalAlign subscript = xwrun.getSubscript();
|
||||
String smalltext = xwrun.getText(0);
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ public class SarFileSplitInfoServiceImpl extends ServiceImpl<SarFileSplitInfoMap
|
||||
@Override
|
||||
public void add(SarFileSplitInfoEO sarFileSplitInfoEO) {
|
||||
// 唯一校验
|
||||
uniqueCheck(sarFileSplitInfoEO);
|
||||
//uniqueCheck(sarFileSplitInfoEO);
|
||||
|
||||
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
Date now = new Date();
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.jero.modules.split.util;
|
||||
|
||||
import cn.hutool.poi.word.WordUtil;
|
||||
import org.apache.xmlbeans.impl.jam.provider.ResourcePath;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
@@ -10,6 +13,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author liJiaRao
|
||||
@@ -23,10 +27,12 @@ public class OmmlUtils {
|
||||
*/
|
||||
public static String getMathMLFromNode(String omML) {
|
||||
//通过word中的xsl转成器解析omml公式格式
|
||||
//StreamSource xslSource = new StreamSource(new File("src/main/resources/OMML2MML.XSL"));
|
||||
URL resourceUrl = ResourcePath.class.getClassLoader().getResource("OMML2MML.XSL");
|
||||
File file = new File(resourceUrl.getPath());
|
||||
StreamSource xslSource = new StreamSource(WordUtil.class.getResourceAsStream("conventer/OMML2MML.XSL"));
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
Transformer t = TransformerFactory.newInstance().newTransformer();
|
||||
Transformer t = TransformerFactory.newInstance().newTransformer(xslSource);
|
||||
Source sources = new StreamSource(new StringReader(omML));
|
||||
StreamResult result = new StreamResult(writer);
|
||||
t.transform(sources, result);
|
||||
|
||||
+112
@@ -1,11 +1,23 @@
|
||||
package com.jero.modules.split.util;
|
||||
|
||||
import cn.hutool.poi.word.WordUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.xmlbeans.XmlCursor;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.Node;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
|
||||
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -19,6 +31,7 @@ import java.util.Queue;
|
||||
* @author liJiaRao
|
||||
* @date 2023-11-09 9:24
|
||||
*/
|
||||
@Slf4j
|
||||
public class ParagraphChildOrderManager {
|
||||
public static int TYPE_RUN = 1;
|
||||
public static int TYPE_OMATH = 2;
|
||||
@@ -84,4 +97,103 @@ public class ParagraphChildOrderManager {
|
||||
return runsOrMathList;
|
||||
}
|
||||
}
|
||||
|
||||
public static String parseParagraph(XWPFParagraph xwpfParagraph) {
|
||||
CTP ctp = xwpfParagraph.getCTP();
|
||||
String xmlText = ctp.xmlText();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// if (xmlText.contains("<m:oMath>")) {
|
||||
|
||||
//段落文本内容
|
||||
sb.append(xwpfParagraph.getParagraphText());
|
||||
//段落公式解析
|
||||
//得到根节点的值
|
||||
SAXReader saxReader = new SAXReader();
|
||||
//将String类型的字符串转换成XML文本对象
|
||||
Document doc = null;
|
||||
try {
|
||||
doc = saxReader.read(new ByteArrayInputStream(xmlText.getBytes()));
|
||||
} catch (DocumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Element root = doc.getRootElement();
|
||||
// 一个段落多个表达式解析
|
||||
//用xpath得到OMML节点
|
||||
List<Node> nodes = root.selectNodes("//m:oMath");
|
||||
if (nodes != null && !nodes.isEmpty()) {
|
||||
for (Node node : nodes) {
|
||||
/**
|
||||
* OMML -> MathML -> LaTex
|
||||
* Office在安装目录中提供了将OMML转为MathML的xsl工具:MML2OMML.XSL
|
||||
* MathML转LaTex使用网上找到另一个xsl工具mmltex.xsl。
|
||||
*/
|
||||
String xml = node.asXML();
|
||||
//xml转 mathml
|
||||
String mathml = convertOMML2MML(xml);
|
||||
//mathml转latx
|
||||
String latex = convertMML2Latex(mathml);
|
||||
sb.append(latex);
|
||||
log.info("latex表达式:{}",latex);
|
||||
}
|
||||
}
|
||||
log.info("公式个数:{},解析内容:{}",nodes.size(),sb.toString());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Description: 将mathml转为latx </p>
|
||||
* @param mml mathml字符串
|
||||
* @return
|
||||
*/
|
||||
public static String convertMML2Latex(String mml){
|
||||
mml = mml.substring(mml.indexOf("?>")+2, mml.length()); //去掉xml的头节点
|
||||
URIResolver r = new URIResolver(){ //设置xls依赖文件的路径
|
||||
@Override
|
||||
public Source resolve(String href, String base) throws TransformerException {
|
||||
File f = new File("/conventer/mml2tex/" + href);
|
||||
InputStream inputStream = WordUtil.class.getResourceAsStream("/conventer/mml2tex/" + href);
|
||||
return new StreamSource(inputStream);
|
||||
}
|
||||
};
|
||||
String latex = xslConvert(mml, "/conventer/mml2tex/mmltex.xsl", r);
|
||||
if(latex != null && latex.length() > 1){
|
||||
latex = latex.substring(1, latex.length() - 1);
|
||||
}
|
||||
return latex;
|
||||
}
|
||||
/**
|
||||
* <p>Description: office xml转为mathml </p>
|
||||
* @param xml 公式xml
|
||||
* @return
|
||||
*/
|
||||
public static String convertOMML2MML(String xml){
|
||||
// 进行转换的过程中需要借助这个文件,一般来说本机安装office就会有这个文件,找到就可以
|
||||
String result = xslConvert(xml, "/conventer/OMML2MML.XSL", null);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: xsl转换器</p>
|
||||
* @param s 公式xml字符串
|
||||
* @param xslpath 转换器路径
|
||||
* @param uriResolver xls依赖文件
|
||||
* @return
|
||||
*/
|
||||
public static String xslConvert(String s, String xslpath, URIResolver uriResolver){
|
||||
TransformerFactory tFac = TransformerFactory.newInstance();
|
||||
if(uriResolver != null) {
|
||||
tFac.setURIResolver(uriResolver);
|
||||
}
|
||||
StreamSource xslSource = new StreamSource(WordUtil.class.getResourceAsStream(xslpath));
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
Transformer t = tFac.newTransformer(xslSource);
|
||||
Source source = new StreamSource(new StringReader(s));
|
||||
Result result = new StreamResult(writer);
|
||||
t.transform(source, result);
|
||||
} catch (TransformerException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return writer.getBuffer().toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user