浏览代码

同步测试环境代码

lideyin 5 年之前
父节点
当前提交
e593882326
共有 1 个文件被更改,包括 36 次插入8 次删除
  1. 36 8
      src/main/java/cn/com/qmth/examcloud/support/handler/richText/HtmlTextHandler.java

+ 36 - 8
src/main/java/cn/com/qmth/examcloud/support/handler/richText/HtmlTextHandler.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.support.handler.richText;
 
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
 import cn.com.qmth.examcloud.commons.util.RegExpUtil;
 import cn.com.qmth.examcloud.support.handler.richText.bean.BlockBean;
 import cn.com.qmth.examcloud.support.handler.richText.bean.SectionBean;
@@ -7,6 +8,8 @@ import cn.com.qmth.examcloud.support.handler.richText.bean.SectionCollectionBean
 import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
 import com.mysql.cj.util.StringUtils;
 import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jsoup.Jsoup;
 import org.jsoup.safety.Whitelist;
 
@@ -22,8 +25,10 @@ import java.util.Map;
  * @Version 1.0
  */
 public class HtmlTextHandler implements RichTextHandler {
+    private static Log logger = LogFactory.getLog(HtmlTextHandler.class);
+
     private static Map<String, String> tagMap;
-    private static String DEFAULT_RETAIN_TAG="b,u,i,sup,sub";
+    private static String DEFAULT_RETAIN_TAG = "b,u,i,sup,sub";
 
     static {
         tagMap = new HashMap<>();
@@ -36,12 +41,24 @@ public class HtmlTextHandler implements RichTextHandler {
 
     @Override
     public SectionCollectionBean handle(String richText) {
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("[HTML-TEXT-HANDLER]--richText: " + richText);
+        }
+
+        if (StringUtils.isNullOrEmpty(richText)) {
+            return new SectionCollectionBean();
+        }
+
         /*过滤不需要的html标签*/
         String retainTags = PropertyHolder.getString("cloudMarking.interface.retainTags", DEFAULT_RETAIN_TAG);
 
-        String[] specialRetainTagArr = (retainTags+",br").split(",");
-        richText = Jsoup.clean(richText, new Whitelist().addTags(specialRetainTagArr));
+//        String[] specialRetainTagArr = (retainTags + ",br").split(",");
+//        richText = Jsoup.clean(richText, new Whitelist().addTags(specialRetainTagArr));
 
+        richText = richText.replace("<div>", "\n")
+                .replace("</div>", "")
+                .replace("<br>", "");
         richText = richText.replace("<br>", "\n");
 
         SectionCollectionBean sectionCollection = new SectionCollectionBean();
@@ -54,7 +71,7 @@ public class HtmlTextHandler implements RichTextHandler {
             SectionBean section = new SectionBean();
 
             List<BlockBean> blockList = new ArrayList<>();
-            getSplitList(p, blockList,retainTagArr);
+            blockList = getSplitList(p, blockList, retainTagArr);
 
             section.setBlocks(blockList);
             sectionList.add(section);
@@ -67,7 +84,7 @@ public class HtmlTextHandler implements RichTextHandler {
         return sectionCollection;
     }
 
-    private static List<BlockBean> getSplitList(String str, List<BlockBean> resultList,final String[] retainTagArr) {
+    private static List<BlockBean> getSplitList(String str, List<BlockBean> resultList, final String[] retainTagArr) {
         if (StringUtils.isNullOrEmpty(str)) {
             return null;
         }
@@ -78,7 +95,7 @@ public class HtmlTextHandler implements RichTextHandler {
             if (i != 0) {
                 regPattern += "|";
             }
-            regPattern += String.format("<%s>;\\S*<\\/%s>", tag, tag);
+            regPattern += String.format("<%s>\\S*<\\/%s>", tag, tag);
         }
 
 
@@ -107,7 +124,9 @@ public class HtmlTextHandler implements RichTextHandler {
             resultList.add(buildSpecialTextAnswer(Jsoup.clean(firstMatch, Whitelist.none()), getTag(firstMatch)));
             //其次,将剩下的字符串递归继续处理
             str = str.substring(firstMatch.length());
-            getSplitList(str, resultList,retainTagArr);
+            getSplitList(str, resultList, retainTagArr);
+
+            return resultList;
         }
 
         //如果字符串不以特殊标签开头,且还有其它内容
@@ -118,7 +137,7 @@ public class HtmlTextHandler implements RichTextHandler {
         resultList.add(buildSpecialTextAnswer(Jsoup.clean(firstMatch, Whitelist.none()), getTag(firstMatch)));
         //剩下部分的字符串,继续递归处理
         str = str.substring(matchIndex + firstMatch.length());
-        getSplitList(str, resultList,retainTagArr);
+        getSplitList(str, resultList, retainTagArr);
 
         return resultList;
     }
@@ -159,5 +178,14 @@ public class HtmlTextHandler implements RichTextHandler {
         return null;
     }
 
+//    public static void main(String[] args) {
+//        String testStr = "1111111<div>222&lt;div&gt;&lt;/div&gt;21000<sup>2</sup></div><div>3333399<sub>3</sub>5555555</div><div><br></div><div>444444</div>";
+//
+//        HtmlTextHandler o = new HtmlTextHandler();
+//
+//
+//        SectionCollectionBean sfds = o.handle(testStr);
+//        System.out.println(JsonUtil.toJson(sfds));
+//    }
 
 }