/**
|
* 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;
|
}
|
}
|