summaryrefslogtreecommitdiff
path: root/doc/ToolUtil.java
blob: edc3987e0846b5a163b7c05735d1dc5a93265253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * Copyright (c) 2015-2016, Chill Zhuang 庄骞 ([email protected]).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.nis.ntc.utils;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.nis.ntc.utils.bean.LocationPage;
import cn.nis.ntc.utils.exception.NtcException;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csvreader.CsvReader;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ValidationException;
import org.apache.commons.beanutils.ConvertUtils;
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static cn.nis.ntc.utils.DPs.log;


/**
 * 高频方法集合类
 */
public class ToolUtil {

    private static final Log logger = Log.get();

    private static final Pattern POINT_PATTERN = Pattern.compile("(\\s)(\\.)(\\s)|(\\.)(\\s)|(\\s)(\\.)");

    private static final Pattern POINT_PATTERN_DEEP = Pattern.compile("(\\.)");

    private static final Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 入库字段\处理查询入库
     *
     * @param cfgKeywords
     * @return
     */
    public static String strGetEscape(String cfgKeywords) {
        if (StringUtils.isNotEmpty(cfgKeywords)) {
            cfgKeywords = cfgKeywords.trim();// 首先去掉空白符号
            // 不能输入不可见字符
            if (containsInvisibleChar(cfgKeywords)) {
                throw new NtcException(Code.InvisibleChar.getCode(), Code.InvisibleChar.getDesc() + ": " + cfgKeywords);
            }
            // \\ 需要第一个替换
//            String[] fbsArr = { "\\","$","(",")","*","+",".","?","^","|","'","%","&"};
            cfgKeywords = cfgKeywords.replace("\\", "\\\\")
                    .replace("&", "\\&")
                    .replace(" ", "\\b");
        }
        return cfgKeywords;
    }

    /**
     * 反转义特殊字符 出库
     *
     * @param cfgKeywords
     * @return
     */
    public static String strUnEscape(String cfgKeywords, Boolean... flag) {
        if (StringUtils.isNotEmpty(cfgKeywords)) {
            // 不转译特殊字符
            cfgKeywords = cfgKeywords.trim();// 首先去掉首尾空格
            if (flag.length > 0 && flag[0]) {
                /**因为入库前空格要转换成了\b;但如果输入的是\b而不是空格,入库后就是\\b,这时在反转时不需要处理,
                 * 在查询的时候要将\b转反换回空格,需要对\\b提前处理,否则会误转
                 * * split本身对\需要转义,最终\\b写成了8个\
                 */
                String[] cfgKeywordAry = cfgKeywords.split("\\\\\\\\b", -1);
//                String tempKeywords = "";
                StringBuilder tempKeywords = new StringBuilder();
                for (String str : cfgKeywordAry) {
                    str = str.replace("\\b", " ");
//                    tempKeywords+=str+"\\\\b";
                    tempKeywords.append(str);
                    tempKeywords.append("\\\\b");
                }
                if (StringUtils.endsWith(tempKeywords.toString(), "\\\\b")) {
                    cfgKeywords = tempKeywords.substring(0, tempKeywords.lastIndexOf("\\\\b"));
                }
                cfgKeywords = cfgKeywords.indexOf("\\\\b") == -1 ? cfgKeywords.replace("\\b", " ") : cfgKeywords;
            } else {
                String[] tempStr = cfgKeywords.split("\\\\\\\\b", -1);
//                String tempKwd  = "";
                StringBuilder tempKwd = new StringBuilder();
                for (String str : tempStr) {
                    str = str.replace("\\b", "\b");
//                    tempKwd += str+"\\\\b";
                    tempKwd.append(str);
                    tempKwd.append("\\\\b");
                }
                if (StringUtils.endsWith(tempKwd.toString(), "\\\\b")) {
                    cfgKeywords = tempKwd.substring(0, tempKwd.lastIndexOf("\\\\b"));
                }
                char[] chars = cfgKeywords.toCharArray();
                StringBuilder sb = new StringBuilder();
                for (char aChar : chars) {
                    if (aChar == '\b') {
                        sb.append(" ");
                    } else {
                        sb.append(aChar);
                    }
                }
                cfgKeywords = sb.toString();

            }
            cfgKeywords = cfgKeywords.replace("\\\\", "\\");
            cfgKeywords = cfgKeywords.replace("\\&", "&");
        }
        return cfgKeywords;
    }

    /**
     * 字符串是否包含不可见字符
     * 包含:true
     * 不包含:false
     *
     * @param content
     * @return Boolean
     */
    public static Boolean containsInvisibleChar(String content) {
        if (content != null && content.length() > 0) {
            char[] contentCharArr = content.toCharArray();
            for (int i = 0; i < contentCharArr.length; i++) {
                if ((contentCharArr[i] <= 0x1F) || contentCharArr[i] == 0x7F) {
                    return true;
                }
            }
            return false;
        }
        return false;
    }
}