|
@@ -1,6 +1,8 @@
|
|
|
package cn.com.qmth.dp.examcloud.oe.util;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.jsoup.Jsoup;
|
|
@@ -11,51 +13,86 @@ import cn.com.qmth.dp.examcloud.oe.modules.findreduplicatequestion.QuesOptionDto
|
|
|
import cn.com.qmth.dp.examcloud.oe.modules.findreduplicatequestion.QuestionDto;
|
|
|
|
|
|
public class PaperUtil {
|
|
|
- public static String getExtractText(String html) {
|
|
|
- if (StringUtils.isBlank(html)) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- return getTextInHtml(html);
|
|
|
- }
|
|
|
- public static String getExtractText(QuestionDto question) {
|
|
|
- StringBuilder quesText = new StringBuilder();
|
|
|
- if (question == null) {
|
|
|
- return quesText.toString();
|
|
|
- }
|
|
|
-
|
|
|
- if (!StringUtils.isEmpty(question.getQuesBody())) {
|
|
|
- quesText.append(getTextInHtml(question.getQuesBody()));
|
|
|
- }
|
|
|
-
|
|
|
- List<QuesOptionDto> quesOptionList = question.getQuesOptions();
|
|
|
- if (quesOptionList != null && quesOptionList.size() > 0) {
|
|
|
- for (QuesOptionDto quesOption : quesOptionList) {
|
|
|
- if (!StringUtils.isEmpty(quesOption.getOptionBody())) {
|
|
|
- quesText.append(getTextInHtml(quesOption.getOptionBody()));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return quesText.toString();
|
|
|
- }
|
|
|
- public static String getTextInHtml(String htmlStr) {
|
|
|
- htmlStr = htmlStr.replaceAll("\\&[a-zA-Z]{1,10};", "").trim();
|
|
|
- if (!htmlStr.startsWith("<p>")) {
|
|
|
- return htmlStr;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
- StringBuilder textStr = new StringBuilder();
|
|
|
- Elements links = doc.select("p").removeAttr("img");
|
|
|
-
|
|
|
- for (Element link : links) {
|
|
|
- textStr.append(link.text().trim());
|
|
|
- }
|
|
|
-
|
|
|
- return textStr.toString();
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
+ private static Pattern imgPat = Pattern.compile("(<img[^<]+src=['\"])([^<\"]+)(['\"][^<]*>)");
|
|
|
+
|
|
|
+ public static boolean hasImg(QuestionDto question) {
|
|
|
+ if (question == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hasImg(question.getQuesBody())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<QuesOptionDto> quesOptionList = question.getQuesOptions();
|
|
|
+ if (quesOptionList != null && quesOptionList.size() > 0) {
|
|
|
+ for (QuesOptionDto quesOption : quesOptionList) {
|
|
|
+ if (hasImg(quesOption.getOptionBody())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean hasImg(String htmlStr) {
|
|
|
+ Matcher matcher = imgPat.matcher(htmlStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getExtractText(String html) {
|
|
|
+ if (StringUtils.isBlank(html)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return getTextInHtml(html);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getExtractText(QuestionDto question) {
|
|
|
+ StringBuilder quesText = new StringBuilder();
|
|
|
+ if (question == null) {
|
|
|
+ return quesText.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(question.getQuesBody())) {
|
|
|
+ quesText.append(getTextInHtml(question.getQuesBody()));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<QuesOptionDto> quesOptionList = question.getQuesOptions();
|
|
|
+ if (quesOptionList != null && quesOptionList.size() > 0) {
|
|
|
+ for (QuesOptionDto quesOption : quesOptionList) {
|
|
|
+ if (!StringUtils.isEmpty(quesOption.getOptionBody())) {
|
|
|
+ quesText.append(getTextInHtml(quesOption.getOptionBody()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return quesText.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getTextInHtml(String htmlStr) {
|
|
|
+ htmlStr = htmlStr.replaceAll("\\&[a-zA-Z]{1,10};", "").trim();
|
|
|
+ if (!htmlStr.startsWith("<p>")) {
|
|
|
+ return htmlStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
+ StringBuilder textStr = new StringBuilder();
|
|
|
+ Elements links = doc.select("p").removeAttr("img");
|
|
|
+
|
|
|
+ for (Element link : links) {
|
|
|
+ textStr.append(link.text().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ return textStr.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|