diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WordController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WordController.java index 753545d2..267fbc29 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WordController.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WordController.java @@ -1,27 +1,21 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord; +package pc.exam.pp.module.judgement.controller.admin.Wps; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.validation.Valid; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import pc.exam.pp.framework.common.enums.CommonStatusEnum; import pc.exam.pp.framework.common.pojo.CommonResult; import pc.exam.pp.framework.common.util.object.BeanUtils; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordRespVO; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordSaveReqVO; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordSimpleRespVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordListReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordRespVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordSaveReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordSimpleRespVO; import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.service.wps_word.WpsWordLinkService; -import pc.exam.pp.module.system.controller.admin.dept.vo.dept.DeptListReqVO; -import pc.exam.pp.module.system.controller.admin.dept.vo.dept.DeptRespVO; -import pc.exam.pp.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; -import pc.exam.pp.module.system.controller.admin.dept.vo.dept.DeptSimpleRespVO; -import pc.exam.pp.module.system.dal.dataobject.dept.DeptDO; import java.util.List; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WpsWordController.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WpsWordController.java index ca1b873a..cfe1fe2d 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WpsWordController.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/WpsWordController.java @@ -1,18 +1,15 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord; +package pc.exam.pp.module.judgement.controller.admin.Wps; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import pc.exam.pp.framework.common.pojo.CommonResult; import pc.exam.pp.module.judgement.service.wps_word.JudgementWpsWordService; import pc.exam.pp.module.judgement.utils.wps_word.vo.WordVO; -import java.io.IOException; import java.util.List; /** @@ -21,8 +18,8 @@ import java.util.List; */ @RestController -@RequestMapping("/tool/wps_word") -@Tag( name = "WPSWORD") +@RequestMapping("/tool/wps") +@Tag( name = "wps相关操作") @Validated public class WpsWordController { diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordListReqVO.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordListReqVO.java index a814119d..befd2965 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordListReqVO.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordListReqVO.java @@ -1,4 +1,4 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord.vo; +package pc.exam.pp.module.judgement.controller.admin.Wps.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordRespVO.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordRespVO.java index 98f9ccbf..b576e48d 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordRespVO.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordRespVO.java @@ -1,4 +1,4 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord.vo; +package pc.exam.pp.module.judgement.controller.admin.Wps.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSaveReqVO.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSaveReqVO.java index bfd9cb33..4cbf29d4 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSaveReqVO.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSaveReqVO.java @@ -1,7 +1,6 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord.vo; +package pc.exam.pp.module.judgement.controller.admin.Wps.vo; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSimpleRespVO.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSimpleRespVO.java index 18dd0ef4..14abe6a7 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSimpleRespVO.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/admin/Wps/vo/WordSimpleRespVO.java @@ -1,4 +1,4 @@ -package pc.exam.pp.module.judgement.controller.admin.WpsWord.vo; +package pc.exam.pp.module.judgement.controller.admin.Wps.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/dal/mysql/wpsword/WpsWordLinkMapper.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/dal/mysql/wpsword/WpsWordLinkMapper.java index 3115bf09..5338e04f 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/dal/mysql/wpsword/WpsWordLinkMapper.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/dal/mysql/wpsword/WpsWordLinkMapper.java @@ -1,10 +1,9 @@ package pc.exam.pp.module.judgement.dal.mysql.wpsword; import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; import pc.exam.pp.framework.mybatis.core.mapper.BaseMapperX; import pc.exam.pp.framework.mybatis.core.query.LambdaQueryWrapperX; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordListReqVO; import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.utils.tree.vo.TreeVO; @@ -17,6 +16,7 @@ public interface WpsWordLinkMapper extends BaseMapperX { default List selectList(WordListReqVO reqVO) { return selectList(new LambdaQueryWrapperX() .likeIfPresent(WpsWordLinkDO::getName, reqVO.getName()) + .eq(reqVO.getBelongTo() != null, WpsWordLinkDO::getBelongTo, reqVO.getBelongTo()) .eqIfPresent(WpsWordLinkDO::getStatus, reqVO.getStatus())); } diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_excel/JudgementWpsExcelServiceImpl.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_excel/JudgementWpsExcelServiceImpl.java index 28469403..b45465f8 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_excel/JudgementWpsExcelServiceImpl.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_excel/JudgementWpsExcelServiceImpl.java @@ -4,21 +4,12 @@ package pc.exam.pp.module.judgement.service.wps_excel; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; -import pc.exam.pp.module.exam.dal.dataobject.ExamQuestion; -import pc.exam.pp.module.exam.dal.dataobject.ExamQuestionAnswer; -import pc.exam.pp.module.infra.dal.dataobject.config.ConfigDO; import pc.exam.pp.module.infra.service.config.ConfigService; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; -import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.dal.mysql.wpsword.WpsWordLinkMapper; import pc.exam.pp.module.judgement.service.auto_tools.AutoToolsService; import pc.exam.pp.module.judgement.utils.wps_excel.WpsExcelUtils; -import pc.exam.pp.module.judgement.utils.wps_word.WpsWordUtils; -import pc.exam.pp.module.judgement.utils.wps_word.vo.WordVO; -import java.io.File; import java.io.FileNotFoundException; -import java.util.List; @Service public class JudgementWpsExcelServiceImpl implements JudgementWpsExcelService { diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/JudgementWpsWordServiceImpl.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/JudgementWpsWordServiceImpl.java index 437683e4..0ed929c1 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/JudgementWpsWordServiceImpl.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/JudgementWpsWordServiceImpl.java @@ -9,7 +9,7 @@ import pc.exam.pp.module.exam.dal.dataobject.ExamQuestionAnswer; import pc.exam.pp.module.exam.utils.file.LogFileUtils; import pc.exam.pp.module.infra.dal.dataobject.config.ConfigDO; import pc.exam.pp.module.infra.service.config.ConfigService; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordListReqVO; import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.dal.mysql.wpsword.WpsWordLinkMapper; import pc.exam.pp.module.judgement.service.auto_tools.AutoToolsService; @@ -17,8 +17,6 @@ import pc.exam.pp.module.judgement.utils.wps_word.WpsWordUtils; import pc.exam.pp.module.judgement.utils.wps_word.vo.WordVO; import java.io.File; -import java.math.BigDecimal; -import java.math.RoundingMode; import java.util.List; @Service @@ -35,6 +33,7 @@ public class JudgementWpsWordServiceImpl implements JudgementWpsWordService { @Override public List ProgrammingWpsWord(String path) throws Exception { + // 1、获取文件临时下载路径 ConfigDO config = configService.getConfigByKey("file_down_path"); // 2、下载文件并返回文件完整路径 @@ -42,9 +41,13 @@ public class JudgementWpsWordServiceImpl implements JudgementWpsWordService { // 3、创建word考点tree WordListReqVO wordListReqVO = new WordListReqVO(); wordListReqVO.setBelongTo(0); - List list = wpsWordLinkMapper.selectList(wordListReqVO); + // 3-1、查询段落的标签 + List paragraphList = wpsWordLinkMapper.selectList(wordListReqVO); + // 3-2、查询锚点的标签 + wordListReqVO.setBelongTo(3); + List anchorList = wpsWordLinkMapper.selectList(wordListReqVO); // 4、docx文件读取并返回考点及说明信息 - List margins1 = WpsWordUtils.wps_word(pathName, list); + List margins1 = WpsWordUtils.wps_word(pathName, paragraphList, anchorList); // 5、已经读取完得考点删除源文件 File file = new File(pathName); file.delete(); @@ -60,9 +63,13 @@ public class JudgementWpsWordServiceImpl implements JudgementWpsWordService { // 1、查询Word考点tree WordListReqVO wordListReqVO = new WordListReqVO(); wordListReqVO.setBelongTo(0); - List list = wpsWordLinkMapper.selectList(wordListReqVO); + // 3-1、查询段落的标签 + List paragraphList = wpsWordLinkMapper.selectList(wordListReqVO); + // 3-2、查询锚点的标签 + wordListReqVO.setBelongTo(3); + List anchorList = wpsWordLinkMapper.selectList(wordListReqVO); // 2、docx文件读取并返回考点及说明信息 - List margins1 = WpsWordUtils.wps_word(path, list); + List margins1 = WpsWordUtils.wps_word(path, paragraphList, anchorList); // 3、获取答案得组成 List answerList = examQuestion.getAnswerList(); // 4、进行关联判断 diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkService.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkService.java index 6c1f9307..94c173ca 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkService.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkService.java @@ -1,8 +1,8 @@ package pc.exam.pp.module.judgement.service.wps_word; import pc.exam.pp.framework.common.util.collection.CollectionUtils; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordSaveReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordListReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordSaveReqVO; import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.utils.tree.vo.TreeVO; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkServiceImpl.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkServiceImpl.java index a52f3a53..9a477afa 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkServiceImpl.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/service/wps_word/WpsWordLinkServiceImpl.java @@ -13,8 +13,8 @@ import pc.exam.pp.framework.common.enums.CommonStatusEnum; import pc.exam.pp.framework.common.util.object.BeanUtils; import pc.exam.pp.framework.datapermission.core.annotation.DataPermission; import pc.exam.pp.framework.tenant.core.aop.TenantIgnore; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordListReqVO; -import pc.exam.pp.module.judgement.controller.admin.WpsWord.vo.WordSaveReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordListReqVO; +import pc.exam.pp.module.judgement.controller.admin.Wps.vo.WordSaveReqVO; import pc.exam.pp.module.judgement.dal.dataobject.wpsword.WpsWordLinkDO; import pc.exam.pp.module.judgement.dal.mysql.wpsword.WpsWordLinkMapper; import pc.exam.pp.module.judgement.utils.tree.vo.TreeVO; diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/WpsWordUtils.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/WpsWordUtils.java index fd01e2b8..afd2ec51 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/WpsWordUtils.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/WpsWordUtils.java @@ -18,36 +18,105 @@ public class WpsWordUtils { /** * 获取文档段落W:P标签得数量,判断出一个有多少段 */ - public static List wps_word(String filePath, List wpsWordLinkDOS) throws Exception { + public static List wps_word(String filePath, List paragraphList, List anchorList) throws Exception { List wordVO = new ArrayList<>(); int count = 0; - // 路径初始化 + // 1、路径初始化 String xmlpath = ""; + String type = ""; XWPFDocument document = new XWPFDocument(new FileInputStream(filePath)); String xmlString = XmlUtil.getDocumentXml(document); - // 获取CTDocument对象 + // 2、创建最全的命名空间 + Pattern pattern = Pattern.compile("xmlns:(\\w+)=\"([^\"]+)\""); + Matcher matcher = pattern.matcher(xmlString); + Map namespaces = new HashMap<>(); + while (matcher.find()) { + // 如 w, wp, a + String prefix = matcher.group(1); + // 如 http://schemas.openxmlformats.org/... + String uri = matcher.group(2); + namespaces.put(prefix, uri); + } + StringBuilder xpathBuilder = new StringBuilder(); + namespaces.forEach((prefix, uri) -> + xpathBuilder.append("declare namespace ") + .append(prefix) + .append("='") + .append(uri) + .append("' ") + ); + // 2-1、获取出来最全的命名空间 + String allPathx = xpathBuilder.toString(); + // 3、获取CTDocument对象 XmlObject docXml = document.getDocument(); - List tree = TreeUtils.buildTree(wpsWordLinkDOS); // flatList 是你贴出的 JSON 解析出来的 - XmlObject xmlObject = document.getDocument(); // Word XML document - // 创建一个list,存放word读取的数据 - for (WpsWordLinkDO wpsWordLinkDO : tree) { + // 3-1、段落的树数据 + List paragraphTree = TreeUtils.buildTree(paragraphList); + // 3-2、锚点的树数据 + List anchorTree = TreeUtils.buildTree(anchorList); + // Word XML document + XmlObject xmlObject = document.getDocument(); + for (WpsWordLinkDO anchor : anchorTree) { + String xpathAnchor = allPathx + "//" + anchor.getName(); + XmlCursor cursorAnchor = docXml.newCursor(); + int index = 0; + cursorAnchor.selectPath(xpathAnchor); + while (cursorAnchor.toNextSelection()) { + type = "图片"; + index += 1; + XmlCursor cursorAnchorXml = cursorAnchor.getObject().newCursor(); + XmlCursor cursorAnchorText = cursorAnchor.getObject().newCursor(); + // 首先:判断类型 1 :图片,2:文本框 + cursorAnchorXml.selectPath(allPathx + ".//wp:anchor/wp:docPr/@id"); + String typeValue = ""; + while (cursorAnchorXml.toNextSelection()) { + typeValue = cursorAnchorXml.getTextValue(); + } + String anchorName = "图片"; + if (Objects.equals(typeValue, "2")) { + System.out.println(cursorAnchorText.xmlText()); + // 要获取文本框的文本 + cursorAnchorText.selectPath(allPathx + ".//wp:anchor/a:graphic/a:graphicData/wps:wsp/wps:txbx"); + while (cursorAnchorText.toNextSelection()) { + System.out.println(cursorAnchorText.getObject().xmlText()); + anchorName = cursorAnchorText.getTextValue(); + } + } + cursorAnchorText.dispose(); + cursorAnchorXml.dispose(); + XmlObject paraObj = cursorAnchor.getObject(); + for (WpsWordLinkDO root : anchor.getChildren()) { + traverseTreeAndQueryXml(type, wordVO, anchor.getName(), anchorName, allPathx, root, paraObj, new ArrayList<>(), new ArrayList<>(), index,1); + } + } + cursorAnchor.dispose(); + } +// 创建一个list,存放word读取的数据(段落数据) + for (WpsWordLinkDO paragraph : paragraphTree) { + type = "段落"; // 段落划分 根据w:p标签 // 到达参数节点,构造 XPath 路径查询它的值 - String xpath = "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' "; - xpath += "//"+wpsWordLinkDO.getName(); + String wpPath = allPathx + "/w:document/w:body/" + paragraph.getName(); XmlCursor cursor = docXml.newCursor(); int index = 0; - cursor.selectPath(xpath); - String xpathName = "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' "; + cursor.selectPath(wpPath); while (cursor.toNextSelection()) { + String rTextName = ""; + index += 1; // 每一段进行赋值,进行判断 XmlObject paraObj = cursor.getObject(); String paraText = paraObj.xmlText(); -// System.out.println(paraText); -// System.out.println(cursor.getTextValue()); - index += 1; - for (WpsWordLinkDO root : wpsWordLinkDO.getChildren()) { - traverseTreeAndQueryXml(wordVO, wpsWordLinkDO.getName(), cursor.getTextValue(), xpathName, root, paraObj, new ArrayList<>(), new ArrayList<>(), index,1); + XmlCursor rCursor = paraObj.newCursor(); + // 想要查询指定的path + rCursor.selectPath(allPathx + ".//w:r/w:t"); + while (rCursor.toNextSelection()) { + XmlCursor rsCursor = rCursor.getObject().newCursor(); + rTextName = rsCursor.getTextValue(); + rsCursor.dispose(); + } + rCursor.dispose(); + // 创建新的 + for (WpsWordLinkDO root : paragraph.getChildren()) { + traverseTreeAndQueryXml(type, wordVO, paragraph.getName(), rTextName, allPathx, root, paraObj, new ArrayList<>(), new ArrayList<>(), index,1); } } } @@ -59,14 +128,13 @@ public class WpsWordUtils { } // 获取底层 XML 对象 CTHdrFtr ctHdrFtr = header._getHdrFtr(); // ✅ 正确方法 - String xpath = "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' "; - xpath += "//w:hdr/w:p/w:r/w:pict"; + String xpath = allPathx + "//w:hdr/w:p/w:r/w:pict"; XmlCursor cursor = ctHdrFtr.newCursor(); cursor.selectPath(xpath); while (cursor.toNextSelection()) { // 查询 v:textpath 节点 cursor.selectPath( - "declare namespace v='urn:schemas-microsoft-com:vml' " + + allPathx + ".//v:textpath" ); while (cursor.toNextSelection()) { @@ -86,6 +154,7 @@ public class WpsWordUtils { path.add("name:"+stringAttr); path.add("style:"+styleAttr); attr_word.setExamKeynote(path); + attr_word.setType("水印"); wordVO.add(attr_word); } } @@ -106,6 +175,7 @@ public class WpsWordUtils { email_word.setWordText("邮件合并"); email_word.setExamKeynote(email); email_word.setKeynoteChinese(email); + email_word.setType("邮件合并"); wordVO.add(email_word); } return wordVO; @@ -126,6 +196,7 @@ public class WpsWordUtils { style_list.add("加粗: " + run.isBold()); style_list.add("斜体: " + run.isItalic()); wordVO1.setKeynoteChinese(style_list); + wordVO1.setType("页眉,页脚"); wordVO.add(wordVO1); } } @@ -145,17 +216,31 @@ public class WpsWordUtils { style_list.add("加粗: " + run.isBold()); style_list.add("斜体: " + run.isItalic()); wordVO1.setKeynoteChinese(style_list); + wordVO1.setType("页眉,页脚"); wordVO.add(wordVO1); } } } } - } - public static void traverseTreeAndQueryXml(List wordVOs, String firstTitle, String text, String xpath, WpsWordLinkDO node, XmlObject currentXml, List pathSoFar, List pathChinese, int index, int beginIndex) { + + /** + * @param type String 用来存放类型 + * @param wordVOs List 用来存放结果 + * @param firstTitle String 用来存放一级标题 + * @param text String 用来存放文本 + * @param xpath String 用来存放xpath + * @param node WpsWordLinkDO 用来存放节点 + * @param currentXml XmlObject 用来存放当前xml + * @param pathSoFar List 用来存放路径 + * @param pathChinese List 用来存放中文路径 + * @param index int 用来存放索引 + * @param beginIndex int 用来存放开始索引 + */ + public static void traverseTreeAndQueryXml(String type, List wordVOs, String firstTitle, String text, String xpath, WpsWordLinkDO node, XmlObject currentXml, List pathSoFar, List pathChinese, int index, int beginIndex) { // 到达参数节点,构造 XPath 路径查询它的值 if (beginIndex == 1) { - xpath += "//" + firstTitle + "[" + index + "]/"; + xpath += "./"; } pathSoFar.add(node.getName()); pathChinese.add(node.getToChinese() + index); @@ -163,9 +248,9 @@ public class WpsWordUtils { if (node.getType() == 1) { // pathSoFar.remove(0); xpath += String.join("/", pathSoFar); -// System.out.println(xpath); try (XmlCursor cursors = currentXml.newCursor()) { // System.out.println(xpath); +// System.out.println(cursors.xmlText()); cursors.selectPath(xpath); if (cursors.toNextSelection()) { // System.out.println(cursors.xmlText()); @@ -175,11 +260,11 @@ public class WpsWordUtils { // System.out.println("值:" + texts); // } - String text_value = cursors.getTextValue(); - if (!Objects.equals(text_value, "")) { + String textValue = cursors.getTextValue(); + if (!Objects.equals(textValue, "")) { // 查找List里面是否已经存在相应文本的数据 String finalText = text; - int list_index = IntStream.range(0, wordVOs.size()) + int listIndex = IntStream.range(0, wordVOs.size()) .filter(i -> finalText.equals(wordVOs.get(i).getWordText())) .findFirst() .orElse(-1); @@ -197,15 +282,16 @@ public class WpsWordUtils { woVo.setIsTrue(true); List kchinese = woVo.getKeynoteChinese(); List examKeynote = woVo.getExamKeynote(); - examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + text_value); - kchinese.add(String.join(" → ", pathChinese) + ",值:" + text_value); + examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + textValue); + kchinese.add(String.join(" → ", pathChinese) + ",值:" + textValue); wordVO.setKeynoteChinese(kchinese); wordVO.setExamKeynote(examKeynote); + wordVO.setType(type); wordVOs.set(tindex, woVo); } tindex += 1; } - } else if (list_index < 0) { // 如果没有查询到了 + } else if (listIndex < 0) { // 如果没有查询到了 if (xpath.indexOf("w:sectPr[") > 0) { // 页面属性 // 文本 @@ -219,35 +305,37 @@ public class WpsWordUtils { // 创建list进行存放数据 List kchinese = new ArrayList<>(); List examKeynote = new ArrayList<>(); - examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + text_value); - - kchinese.add(String.join(" → ", pathChinese) + ",值:" + text_value); + examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + textValue); + kchinese.add(String.join(" → ", pathChinese) + ",值:" + textValue); // 组合完数据后进行存放数据 wordVO.setKeynoteChinese(kchinese); wordVO.setExamKeynote(examKeynote); wordVO.setIsTrue(false); + wordVO.setType(type); wordVOs.add(wordVO); } } } else { // 如果找到了 - WordVO wordVO = wordVOs.get(list_index); + WordVO wordVO = wordVOs.get(listIndex); List kchinese = wordVO.getKeynoteChinese(); List examKeynote = wordVO.getExamKeynote(); - examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + text_value); - kchinese.add(String.join(" → ", pathChinese) + ",值:" + text_value); + examKeynote.add(String.join(" → ", pathSoFar) + ",value:" + textValue); + kchinese.add(String.join(" → ", pathChinese) + ",值:" + textValue); wordVO.setKeynoteChinese(kchinese); wordVO.setExamKeynote(examKeynote); wordVO.setIsTrue(false); - wordVOs.remove(list_index); + wordVO.setType(type); + wordVOs.remove(listIndex); wordVOs.add(wordVO); } // System.out.println("文本:" + text + " 参数路径:" + String.join(" → ", pathChinese) + ",值:" + text_value); } + cursors.dispose(); } } } else { for (WpsWordLinkDO child : node.getChildren()) { - traverseTreeAndQueryXml(wordVOs, firstTitle, text, xpath, child, currentXml, new ArrayList<>(pathSoFar), new ArrayList<>(pathChinese), index,2); + traverseTreeAndQueryXml(type, wordVOs, firstTitle, text, xpath, child, currentXml, new ArrayList<>(pathSoFar), new ArrayList<>(pathChinese), index,2); } } pathSoFar.remove(pathSoFar.size() - 1); diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/vo/WordVO.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/vo/WordVO.java index c85e7f61..a4f12536 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/vo/WordVO.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/utils/wps_word/vo/WordVO.java @@ -20,5 +20,7 @@ public class WordVO { @Schema(description = "是否有样式修饰") private Boolean isTrue; + @Schema(description = "类型") + private String type; }