yuxc
2025-01-15 c09f81131e8b7c83937206d7cf76f34d2020be75
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
/**
 * Copyright 2018-2118 the original author or authors.
 * <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 com.vci.dbsync.utils;
 
import java.lang.reflect.Field;
import java.lang.reflect.Type;
 
import org.dom4j.Element;
 
import com.vci.dbsync.Constants;
import com.vci.dbsync.entity.Operate;
 
/**
 * @author liuyazhuang
 * @date 2018/9/11 10:27
 * @description 工具类
 * @version 1.0.0
 */
public class Tool {
 
    /**
     * 产生随机字符串
     * @param length 字符串的长度
     * @return 随机的字符串
     */
    public static String generateString(int length) {
        if (length < 1)
            length = 6;
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String genStr = "";
        for (int index = 0; index < length; index++) {
            genStr = genStr + str.charAt((int) ((Math.random() * 100) % 26));
        }
        return genStr;
    }
    
    /**
     * 解析e中的元素,将数据填充到o中
     * @param e 解析的XML Element对象
     * @param o 存放解析后的XML Element对象
     * @return 存放有解析后数据的Object
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Object elementInObject(Element e, Object o) throws IllegalArgumentException, IllegalAccessException {
        Field[] fields = o.getClass().getDeclaredFields();
        for (int index = 0; index < fields.length; index++) {
            Field item = fields[index];
            //当前字段不是serialVersionUID,同时当前字段不包含serialVersionUID
            if (!Constants.FIELD_SERIALVERSIONUID.equals(item.getName()) && !item.getName().contains(Constants.FIELD_SERIALVERSIONUID)){
                item.setAccessible(true);
                String value = e.element(item.getName()).getTextTrim();
                Type type = item.getType();
                
                if (type == Boolean.TYPE){
                    if (value.equalsIgnoreCase("true"))
                        item.setBoolean(o, true);
                    else
                        item.setBoolean(o, false);
                }
                else if (type == Operate.class){
                    if (value.equalsIgnoreCase("insert"))
                        item.set(o, Operate.insert);
                    else if (value.equalsIgnoreCase("update"))
                        item.set(o, Operate.update);
                    else
                        item.set(o, Operate.both);
                }
                else
                    item.set(o, value);
            }
        }
        return o;
    }
}