|
@@ -1,461 +1,461 @@
|
|
|
-package com.qmth.cqb.utils;
|
|
|
-
|
|
|
-import java.io.*;
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.math.RoundingMode;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Properties;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-import java.util.stream.Stream;
|
|
|
-
|
|
|
-import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.dom4j.Attribute;
|
|
|
-import org.dom4j.Document;
|
|
|
-import org.dom4j.DocumentException;
|
|
|
-import org.dom4j.DocumentHelper;
|
|
|
-import org.dom4j.Element;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.common.dto.question.enums.QuesStructType;
|
|
|
-
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-
|
|
|
-/**
|
|
|
- * Created by songyue on 16/12/27.
|
|
|
- */
|
|
|
-public final class CommonUtils {
|
|
|
-
|
|
|
- private static final String COMMON_PROPERTIES = "common.properties";
|
|
|
-
|
|
|
- private static final String[] CN_SMALL_NUM = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
|
|
|
-
|
|
|
- private static final String[] CN_BIG_NUM = { "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿" };
|
|
|
-
|
|
|
- public static final String PAPER_TITLE="中国石油大学";
|
|
|
- public static final String PAPER_SUB_TITLE="网络教育";
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
|
|
|
-
|
|
|
- /**
|
|
|
- * 加载properties配置文件
|
|
|
- *
|
|
|
- * @param propertiesPath
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Properties loadProperties(String propertiesPath) {
|
|
|
- InputStream inputStream = null;
|
|
|
- Properties properties = new Properties();
|
|
|
- try {
|
|
|
- inputStream = ClassLoader.getSystemResourceAsStream(propertiesPath);
|
|
|
- BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
- properties.load(bf);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("加载配置异常:",e.getMessage());
|
|
|
- }
|
|
|
- return properties;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 记载通用配置文件
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Properties loadCommonProperties() {
|
|
|
- return loadProperties(COMMON_PROPERTIES);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取临时文件夹根路径
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getTmpRootPath() {
|
|
|
- return (String) loadCommonProperties().get("tmp_root_path");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前日期字符串
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getCurDate() {
|
|
|
- return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前日期时间字符串
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getCurDateTime() {
|
|
|
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据当前日期获取数字字符串
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getCurNum() {
|
|
|
- return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据枚举名称获取枚举
|
|
|
- *
|
|
|
- * @param clazz
|
|
|
- * @param name
|
|
|
- * @param <T>
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> T getEnum(Class<T> clazz, String name) {
|
|
|
- T[] enumConstants = clazz.getEnumConstants();
|
|
|
- for (T _enum : enumConstants) {
|
|
|
- if (_enum.toString().equals(name)) {
|
|
|
- return _enum;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 校验题型
|
|
|
- *
|
|
|
- * @param quesType
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean checkQuesType(String quesType) {
|
|
|
- return Stream.of(QuesStructType.values()).anyMatch(type -> type.getName().equals(quesType));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取中文数字
|
|
|
- *
|
|
|
- * @param inputNum
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String toCHNum(int inputNum) {
|
|
|
- String resultNum = "";
|
|
|
- String numStr = String.valueOf(inputNum);
|
|
|
- int len = numStr.length();
|
|
|
- for (int i = 0; i < len; i++) {
|
|
|
- int tmpNum = numStr.charAt(i) - '0';
|
|
|
- if (i != len - 1 && tmpNum != 0) {
|
|
|
- resultNum += CN_SMALL_NUM[tmpNum] + CN_BIG_NUM[len - 2 - i];
|
|
|
- } else {
|
|
|
- resultNum += CN_SMALL_NUM[tmpNum];
|
|
|
- }
|
|
|
- }
|
|
|
- if(resultNum.contains("零")){
|
|
|
- resultNum = resultNum.replaceAll("零{1,}","零");
|
|
|
- }
|
|
|
- if(resultNum.endsWith("零")){
|
|
|
- resultNum = resultNum.substring(0,resultNum.length()-1);
|
|
|
- }
|
|
|
- if(resultNum.startsWith("一十")){
|
|
|
- resultNum = resultNum.replaceFirst("一十","十");
|
|
|
- }
|
|
|
- while(StringUtils.countMatches(resultNum,"万") > 1){
|
|
|
- resultNum = resultNum.replaceFirst("万","");
|
|
|
- }
|
|
|
- while(StringUtils.countMatches(resultNum,"亿") > 1){
|
|
|
- resultNum = resultNum.replaceFirst("亿","");
|
|
|
- }
|
|
|
- return resultNum;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * iterator转List
|
|
|
- *
|
|
|
- * @param <T>
|
|
|
- * @param iterator
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> List<T> toList(Iterable<T> iterator) {
|
|
|
- if (iterator == null) {
|
|
|
- throw new NullPointerException("Iterator must not be null");
|
|
|
- }
|
|
|
- final List<T> list = new ArrayList<T>();
|
|
|
- final Iterator<T> iter = iterator.iterator();
|
|
|
- while (iter.hasNext())
|
|
|
- list.add(iter.next());
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将整数转化成字母
|
|
|
- * @param number
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getOptionNum(int number){
|
|
|
- char optionNum = (char)(65 + number);
|
|
|
- return String.valueOf(optionNum);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 字母转换成数字
|
|
|
- * @param input
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Integer characterToNumber(String input){
|
|
|
- String reg = "[a-zA-Z]";
|
|
|
- StringBuffer strBuf = new StringBuffer();
|
|
|
- input = input.toLowerCase();
|
|
|
- if (null != input && !"".equals(input)) {
|
|
|
- for (char c : input.toCharArray()) {
|
|
|
- if (String.valueOf(c).matches(reg)) {
|
|
|
- strBuf.append(c - 96);
|
|
|
- } else {
|
|
|
- strBuf.append(c);
|
|
|
- }
|
|
|
- }
|
|
|
- return Integer.valueOf(strBuf.toString());
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从A标签中获取属性值
|
|
|
- * 例如从:
|
|
|
- * <a id=\"123456\" name=\"1_1_1.mp3\">
|
|
|
- * 中获取id的属性值为:123456
|
|
|
- * 获取name的属性值为:1_1_1.mp3
|
|
|
- * @param questionStr
|
|
|
- * @param attrName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getAttrValue(String questionStr,String attrName){
|
|
|
- Pattern pattern = Pattern.compile("a.*");
|
|
|
- Matcher matcher = pattern.matcher(questionStr);
|
|
|
- while(matcher.find()){
|
|
|
- String result = matcher.group();
|
|
|
- String idstr = attrName+"=\".*?\"";
|
|
|
- Pattern pattern02 = Pattern.compile(idstr);
|
|
|
- Matcher matcher02 = pattern02.matcher(result);
|
|
|
- while(matcher02.find()){
|
|
|
- return matcher02.group().replaceAll(attrName+"=\"", "").replaceAll("\"", "");
|
|
|
- }
|
|
|
- }
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从一段HTML字符串中取出标签中的属性值
|
|
|
- * @param htmlString
|
|
|
- * @return
|
|
|
- * @throws DocumentException
|
|
|
- */
|
|
|
- public static List<String> getAttrValueFromString(String htmlString,String flagName,String attrName){
|
|
|
- List<String> idValues = new ArrayList<String>();
|
|
|
- try {
|
|
|
- Document document = DocumentHelper.parseText(htmlString);
|
|
|
- Element rootElement = document.getRootElement();
|
|
|
- List<Element> nodes = new ArrayList<Element>();
|
|
|
- if(flagName.equals(rootElement.getName())){
|
|
|
- nodes.add(rootElement);
|
|
|
- }else{
|
|
|
- nodes = rootElement.elements(flagName);
|
|
|
- }
|
|
|
- for(Iterator<Element> itr = nodes.iterator();itr.hasNext();){
|
|
|
- Element element = (Element) itr.next();
|
|
|
- Attribute attr = element.attribute(attrName);
|
|
|
- idValues.add(attr.getValue());
|
|
|
- }
|
|
|
- } catch (DocumentException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return idValues;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 保留两位小数
|
|
|
- * @param number
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static double formatDouble(double number){
|
|
|
- BigDecimal formatNumber = new BigDecimal(number);
|
|
|
- return formatNumber.setScale(2, RoundingMode.HALF_UP).doubleValue();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 向下取整,可以取0.5
|
|
|
- * @param number
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static double formatDoubleFloor(double number){
|
|
|
- BigDecimal formatNumber = new BigDecimal(number);
|
|
|
- double floorNumber = formatNumber.setScale(0, RoundingMode.FLOOR).doubleValue();
|
|
|
- if(number >= floorNumber && number < floorNumber + 0.5){
|
|
|
- return floorNumber;
|
|
|
- }else{
|
|
|
- return floorNumber + 0.5;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 补全html标签
|
|
|
- * @param htmlStr
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static String repairHtmlStr(String htmlStr)throws Exception{
|
|
|
- htmlStr = htmlStr.trim();
|
|
|
- if(htmlStr.toLowerCase().contains("<!doctype html ")){
|
|
|
- int index1 = htmlStr.toLowerCase().indexOf("<!doctype html ");
|
|
|
- int index2 = htmlStr.indexOf('>',index1 + 1);
|
|
|
- htmlStr = htmlStr.substring(0, index1) + htmlStr.substring(index2 + 1);
|
|
|
- }
|
|
|
- while(htmlStr.toLowerCase().contains("<br ")){
|
|
|
- int index1 = htmlStr.toLowerCase().indexOf("<br ");
|
|
|
- int index2 = htmlStr.toLowerCase().indexOf(">",index1 + 1);
|
|
|
- htmlStr = htmlStr.substring(0, index1) + "<br/>" + htmlStr.substring(index2 + 1);
|
|
|
- }
|
|
|
- while(htmlStr.toLowerCase().endsWith("<br>") || htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
- if(htmlStr.toLowerCase().endsWith("<br>")){
|
|
|
- htmlStr = htmlStr.substring(0, htmlStr.length()-"<br>".length());
|
|
|
- }else if(htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
- htmlStr = htmlStr.substring(0, htmlStr.length()-"<br/>".length());
|
|
|
- }
|
|
|
- }
|
|
|
- htmlStr = htmlStr.replace("<br>", "<br/>").replace("<BR>", "<br/>");
|
|
|
-
|
|
|
- {//补全META标签
|
|
|
- int imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ");
|
|
|
- while(imgIndex > 0){
|
|
|
- int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
- if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
- htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
- }
|
|
|
- imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ",flag);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- {//补全img标签
|
|
|
- int imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ");
|
|
|
- while(imgIndex > 0){
|
|
|
- int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
- if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
- htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
- }
|
|
|
- imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ",flag);
|
|
|
- }
|
|
|
- }
|
|
|
- //添加body标签
|
|
|
- if((htmlStr.toLowerCase().contains("<p") || htmlStr.toLowerCase().contains("<span"))
|
|
|
- && !htmlStr.toLowerCase().contains("<body")){
|
|
|
- htmlStr = "<body>"+htmlStr+"</body>";
|
|
|
- }
|
|
|
- return new String(htmlStr.getBytes("UTF-8"));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从指定的位置开始查找第一个匹配正则表达式的字符串的位置
|
|
|
- * @param str
|
|
|
- * @param regex 正则表达式
|
|
|
- * @param fromIndex 指定的起始位置
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static int indexOfRegex(String str,String regex,int fromIndex){
|
|
|
- int index = indexOfRegex(str.substring(fromIndex),regex);
|
|
|
- if(index < 0){
|
|
|
- return -1;
|
|
|
- }
|
|
|
- return fromIndex + index;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查找第一个匹配正则表达式的字符串的位置
|
|
|
- * @param str
|
|
|
- * @param regex 正则表达式
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static int indexOfRegex(String str,String regex){
|
|
|
- Pattern p = Pattern.compile(regex);
|
|
|
- Matcher m = p.matcher(str);
|
|
|
- if(m.find()){
|
|
|
- return m.start();
|
|
|
- }else{
|
|
|
- return -1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 格式化html
|
|
|
- * @param htmlStr
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static String formatHtml(String htmlStr)throws Exception{
|
|
|
- if(StringUtils.isEmpty(htmlStr)){
|
|
|
- return "";
|
|
|
- }
|
|
|
- htmlStr = repairHtmlStr(htmlStr);
|
|
|
- htmlStr = StringEscapeUtils.unescapeHtml4(htmlStr);
|
|
|
- return htmlStr;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 过滤非空格的空字符
|
|
|
- * @param str
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String trimNoBlankSpace(final String str){
|
|
|
- if(str == null || str.length() == 0){
|
|
|
- return "";
|
|
|
- }
|
|
|
- return str.replaceAll("[\\t\\r\\f]*","");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 替换掉字符串的Unicode字符为中文字符
|
|
|
- * 如下所示
|
|
|
- * \u0026ldquo;=“
|
|
|
- * \u0026mdash;=—
|
|
|
- * \u0026lt;=<
|
|
|
- * \u0026hellip;=…
|
|
|
- * \u0026rdquo;=”
|
|
|
- * \u0026gt;=>
|
|
|
- */
|
|
|
- public static String replaceUnicodeStr(String content){
|
|
|
- try {
|
|
|
- Pattern pattern = Pattern.compile("\\\\u\\w+;");
|
|
|
- Matcher matcher = pattern.matcher(content);
|
|
|
- Map<String,String> unicodeMap = new HashMap<String, String>();
|
|
|
- while(matcher.find()){
|
|
|
- String unicodeStr = matcher.group();
|
|
|
- byte[] unicodeStrByte = unicodeStr.getBytes();
|
|
|
- String resultByte = new String(unicodeStrByte,"utf-8");
|
|
|
- String escapeStr = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.unescapeEcmaScript(resultByte));
|
|
|
- unicodeMap.put(unicodeStr, escapeStr);
|
|
|
- }
|
|
|
- System.out.println(unicodeMap);
|
|
|
- if(unicodeMap.size()>0){
|
|
|
- for(String key:unicodeMap.keySet()){
|
|
|
- //replace函数替换时忽略正则表达式符号,replaceAll和replaceFirst函数替换时是使用正则表达式匹配的。
|
|
|
- content = content.replace(key, unicodeMap.get(key));
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return content;
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) throws Exception{
|
|
|
- // QuesStructType quesStructType = getEnum(QuesStructType.class,"单选");
|
|
|
- // System.out.println(quesStructType.getName());
|
|
|
- // System.out.println(characterToNumber("A"));
|
|
|
- System.out.println(formatDoubleFloor(3.61));
|
|
|
- System.out.println(formatDoubleFloor(3.0));
|
|
|
- }
|
|
|
-}
|
|
|
+package com.qmth.cqb.utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.dom4j.Attribute;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.dom4j.DocumentHelper;
|
|
|
+import org.dom4j.Element;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.common.dto.question.enums.QuesStructType;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by songyue on 16/12/27.
|
|
|
+ */
|
|
|
+public final class CommonUtils {
|
|
|
+
|
|
|
+ private static final String COMMON_PROPERTIES = "common.properties";
|
|
|
+
|
|
|
+ private static final String[] CN_SMALL_NUM = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
|
|
|
+
|
|
|
+ private static final String[] CN_BIG_NUM = { "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿" };
|
|
|
+
|
|
|
+ public static final String PAPER_TITLE="中国石油大学";
|
|
|
+ public static final String PAPER_SUB_TITLE="网络教育";
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载properties配置文件
|
|
|
+ *
|
|
|
+ * @param propertiesPath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Properties loadProperties(String propertiesPath) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ Properties properties = new Properties();
|
|
|
+ try {
|
|
|
+ inputStream = ClassLoader.getSystemResourceAsStream(propertiesPath);
|
|
|
+ BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
+ properties.load(bf);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("加载配置异常:",e.getMessage());
|
|
|
+ }
|
|
|
+ return properties;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 记载通用配置文件
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Properties loadCommonProperties() {
|
|
|
+ return loadProperties(COMMON_PROPERTIES);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取临时文件夹根路径
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTmpRootPath() {
|
|
|
+ return (String) loadCommonProperties().get("tmp_root_path");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期字符串
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurDate() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期时间字符串
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurDateTime() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据当前日期获取数字字符串
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurNum() {
|
|
|
+ return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据枚举名称获取枚举
|
|
|
+ *
|
|
|
+ * @param clazz
|
|
|
+ * @param name
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> T getEnum(Class<T> clazz, String name) {
|
|
|
+ T[] enumConstants = clazz.getEnumConstants();
|
|
|
+ for (T _enum : enumConstants) {
|
|
|
+ if (_enum.toString().equals(name)) {
|
|
|
+ return _enum;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验题型
|
|
|
+ *
|
|
|
+ * @param quesType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean checkQuesType(String quesType) {
|
|
|
+ return Stream.of(QuesStructType.values()).anyMatch(type -> type.getName().equals(quesType));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取中文数字
|
|
|
+ *
|
|
|
+ * @param inputNum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String toCHNum(int inputNum) {
|
|
|
+ String resultNum = "";
|
|
|
+ String numStr = String.valueOf(inputNum);
|
|
|
+ int len = numStr.length();
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ int tmpNum = numStr.charAt(i) - '0';
|
|
|
+ if (i != len - 1 && tmpNum != 0) {
|
|
|
+ resultNum += CN_SMALL_NUM[tmpNum] + CN_BIG_NUM[len - 2 - i];
|
|
|
+ } else {
|
|
|
+ resultNum += CN_SMALL_NUM[tmpNum];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(resultNum.contains("零")){
|
|
|
+ resultNum = resultNum.replaceAll("零{1,}","零");
|
|
|
+ }
|
|
|
+ if(resultNum.endsWith("零")){
|
|
|
+ resultNum = resultNum.substring(0,resultNum.length()-1);
|
|
|
+ }
|
|
|
+ if(resultNum.startsWith("一十")){
|
|
|
+ resultNum = resultNum.replaceFirst("一十","十");
|
|
|
+ }
|
|
|
+ while(StringUtils.countMatches(resultNum,"万") > 1){
|
|
|
+ resultNum = resultNum.replaceFirst("万","");
|
|
|
+ }
|
|
|
+ while(StringUtils.countMatches(resultNum,"亿") > 1){
|
|
|
+ resultNum = resultNum.replaceFirst("亿","");
|
|
|
+ }
|
|
|
+ return resultNum;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * iterator转List
|
|
|
+ *
|
|
|
+ * @param <T>
|
|
|
+ * @param iterator
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> List<T> toList(Iterable<T> iterator) {
|
|
|
+ if (iterator == null) {
|
|
|
+ throw new NullPointerException("Iterator must not be null");
|
|
|
+ }
|
|
|
+ final List<T> list = new ArrayList<T>();
|
|
|
+ final Iterator<T> iter = iterator.iterator();
|
|
|
+ while (iter.hasNext())
|
|
|
+ list.add(iter.next());
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将整数转化成字母
|
|
|
+ * @param number
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getOptionNum(int number){
|
|
|
+ char optionNum = (char)(65 + number);
|
|
|
+ return String.valueOf(optionNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字母转换成数字
|
|
|
+ * @param input
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer characterToNumber(String input){
|
|
|
+ String reg = "[a-zA-Z]";
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
+ input = input.toLowerCase();
|
|
|
+ if (null != input && !"".equals(input)) {
|
|
|
+ for (char c : input.toCharArray()) {
|
|
|
+ if (String.valueOf(c).matches(reg)) {
|
|
|
+ strBuf.append(c - 96);
|
|
|
+ } else {
|
|
|
+ strBuf.append(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Integer.valueOf(strBuf.toString());
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从A标签中获取属性值
|
|
|
+ * 例如从:
|
|
|
+ * <a id=\"123456\" name=\"1_1_1.mp3\">
|
|
|
+ * 中获取id的属性值为:123456
|
|
|
+ * 获取name的属性值为:1_1_1.mp3
|
|
|
+ * @param questionStr
|
|
|
+ * @param attrName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAttrValue(String questionStr,String attrName){
|
|
|
+ Pattern pattern = Pattern.compile("a.*");
|
|
|
+ Matcher matcher = pattern.matcher(questionStr);
|
|
|
+ while(matcher.find()){
|
|
|
+ String result = matcher.group();
|
|
|
+ String idstr = attrName+"=\".*?\"";
|
|
|
+ Pattern pattern02 = Pattern.compile(idstr);
|
|
|
+ Matcher matcher02 = pattern02.matcher(result);
|
|
|
+ while(matcher02.find()){
|
|
|
+ return matcher02.group().replaceAll(attrName+"=\"", "").replaceAll("\"", "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从一段HTML字符串中取出标签中的属性值
|
|
|
+ * @param htmlString
|
|
|
+ * @return
|
|
|
+ * @throws DocumentException
|
|
|
+ */
|
|
|
+ public static List<String> getAttrValueFromString(String htmlString,String flagName,String attrName){
|
|
|
+ List<String> idValues = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ Document document = DocumentHelper.parseText(htmlString);
|
|
|
+ Element rootElement = document.getRootElement();
|
|
|
+ List<Element> nodes = new ArrayList<Element>();
|
|
|
+ if(flagName.equals(rootElement.getName())){
|
|
|
+ nodes.add(rootElement);
|
|
|
+ }else{
|
|
|
+ nodes = rootElement.elements(flagName);
|
|
|
+ }
|
|
|
+ for(Iterator<Element> itr = nodes.iterator();itr.hasNext();){
|
|
|
+ Element element = (Element) itr.next();
|
|
|
+ Attribute attr = element.attribute(attrName);
|
|
|
+ idValues.add(attr.getValue());
|
|
|
+ }
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return idValues;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保留两位小数
|
|
|
+ * @param number
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static double formatDouble(double number){
|
|
|
+ BigDecimal formatNumber = new BigDecimal(number);
|
|
|
+ return formatNumber.setScale(2, RoundingMode.HALF_UP).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向下取整,可以取0.5
|
|
|
+ * @param number
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static double formatDoubleFloor(double number){
|
|
|
+ BigDecimal formatNumber = new BigDecimal(number);
|
|
|
+ double floorNumber = formatNumber.setScale(0, RoundingMode.FLOOR).doubleValue();
|
|
|
+ if(number >= floorNumber && number < floorNumber + 0.5){
|
|
|
+ return floorNumber;
|
|
|
+ }else{
|
|
|
+ return floorNumber + 0.5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 补全html标签
|
|
|
+ * @param htmlStr
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String repairHtmlStr(String htmlStr)throws Exception{
|
|
|
+ htmlStr = htmlStr.trim();
|
|
|
+ if(htmlStr.toLowerCase().contains("<!doctype html ")){
|
|
|
+ int index1 = htmlStr.toLowerCase().indexOf("<!doctype html ");
|
|
|
+ int index2 = htmlStr.indexOf('>',index1 + 1);
|
|
|
+ htmlStr = htmlStr.substring(0, index1) + htmlStr.substring(index2 + 1);
|
|
|
+ }
|
|
|
+ while(htmlStr.toLowerCase().contains("<br ")){
|
|
|
+ int index1 = htmlStr.toLowerCase().indexOf("<br ");
|
|
|
+ int index2 = htmlStr.toLowerCase().indexOf(">",index1 + 1);
|
|
|
+ htmlStr = htmlStr.substring(0, index1) + "<br/>" + htmlStr.substring(index2 + 1);
|
|
|
+ }
|
|
|
+ while(htmlStr.toLowerCase().endsWith("<br>") || htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
+ if(htmlStr.toLowerCase().endsWith("<br>")){
|
|
|
+ htmlStr = htmlStr.substring(0, htmlStr.length()-"<br>".length());
|
|
|
+ }else if(htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
+ htmlStr = htmlStr.substring(0, htmlStr.length()-"<br/>".length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ htmlStr = htmlStr.replace("<br>", "<br/>").replace("<BR>", "<br/>");
|
|
|
+
|
|
|
+ {//补全META标签
|
|
|
+ int imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ");
|
|
|
+ while(imgIndex > 0){
|
|
|
+ int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
+ if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
+ htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
+ }
|
|
|
+ imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ",flag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {//补全img标签
|
|
|
+ int imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ");
|
|
|
+ while(imgIndex > 0){
|
|
|
+ int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
+ if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
+ htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
+ }
|
|
|
+ imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ",flag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //添加body标签
|
|
|
+ if((htmlStr.toLowerCase().contains("<p") || htmlStr.toLowerCase().contains("<span"))
|
|
|
+ && !htmlStr.toLowerCase().contains("<body")){
|
|
|
+ htmlStr = "<body>"+htmlStr+"</body>";
|
|
|
+ }
|
|
|
+ return new String(htmlStr.getBytes("UTF-8"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从指定的位置开始查找第一个匹配正则表达式的字符串的位置
|
|
|
+ * @param str
|
|
|
+ * @param regex 正则表达式
|
|
|
+ * @param fromIndex 指定的起始位置
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int indexOfRegex(String str,String regex,int fromIndex){
|
|
|
+ int index = indexOfRegex(str.substring(fromIndex),regex);
|
|
|
+ if(index < 0){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return fromIndex + index;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查找第一个匹配正则表达式的字符串的位置
|
|
|
+ * @param str
|
|
|
+ * @param regex 正则表达式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int indexOfRegex(String str,String regex){
|
|
|
+ Pattern p = Pattern.compile(regex);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ if(m.find()){
|
|
|
+ return m.start();
|
|
|
+ }else{
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化html
|
|
|
+ * @param htmlStr
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String formatHtml(String htmlStr)throws Exception{
|
|
|
+ if(StringUtils.isEmpty(htmlStr)){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ htmlStr = repairHtmlStr(htmlStr);
|
|
|
+ htmlStr = StringEscapeUtils.unescapeHtml4(htmlStr);
|
|
|
+ return htmlStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 过滤非空格的空字符
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String trimNoBlankSpace(final String str){
|
|
|
+ if(str == null || str.length() == 0){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return str.replaceAll("[\\t\\r\\f]*","");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换掉字符串的Unicode字符为中文字符
|
|
|
+ * 如下所示
|
|
|
+ * \u0026ldquo;=“
|
|
|
+ * \u0026mdash;=—
|
|
|
+ * \u0026lt;=<
|
|
|
+ * \u0026hellip;=…
|
|
|
+ * \u0026rdquo;=”
|
|
|
+ * \u0026gt;=>
|
|
|
+ */
|
|
|
+ public static String replaceUnicodeStr(String content){
|
|
|
+ try {
|
|
|
+ Pattern pattern = Pattern.compile("\\\\u[a-f0-9A-F]{1,4}");
|
|
|
+ Matcher matcher = pattern.matcher(content);
|
|
|
+ Map<String,String> unicodeMap = new HashMap<String, String>();
|
|
|
+ while(matcher.find()){
|
|
|
+ String unicodeStr = matcher.group();
|
|
|
+ byte[] unicodeStrByte = unicodeStr.getBytes();
|
|
|
+ String resultByte = new String(unicodeStrByte,"utf-8");
|
|
|
+ String escapeStr = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.unescapeEcmaScript(resultByte));
|
|
|
+ unicodeMap.put(unicodeStr, escapeStr);
|
|
|
+ }
|
|
|
+ System.out.println(unicodeMap);
|
|
|
+ if(unicodeMap.size()>0){
|
|
|
+ for(String key:unicodeMap.keySet()){
|
|
|
+ //replace函数替换时忽略正则表达式符号,replaceAll和replaceFirst函数替换时是使用正则表达式匹配的。
|
|
|
+ content = content.replace(key, unicodeMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+ // QuesStructType quesStructType = getEnum(QuesStructType.class,"单选");
|
|
|
+ // System.out.println(quesStructType.getName());
|
|
|
+ // System.out.println(characterToNumber("A"));
|
|
|
+ System.out.println(formatDoubleFloor(3.61));
|
|
|
+ System.out.println(formatDoubleFloor(3.0));
|
|
|
+ }
|
|
|
+}
|