Commit 9cddcd4b by s_guodong

替换文件中File.separator 为 //

parent b14012a7
package com.brilliance.mda.runtime.mda.util;
import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.impl.ModuleList;
import com.brilliance.mda.runtime.mda.driver.I18NImpl;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.impl.AttributeValue;
import com.brilliance.mda.runtime.mda.impl.ModuleList;
import com.fasterxml.jackson.databind.ObjectMapper;
import oracle.sql.TIMESTAMP;
import org.apache.commons.beanutils.MethodUtils;
......@@ -46,17 +46,19 @@ import static com.brilliance.mda.runtime.mda.Constants.NO_ERROR;
public class MdaUtils {
private static final Logger log = LoggerFactory.getLogger(MdaUtils.class);
public static final int MAX = 200; // 防止StackOver Overflow
private static final String FORMATDATE = "yyyyMMddHHmmssSSS";
private static final String FORMATINT = "0.00";
private static final char CR = '\r';
private static final char STRING = 'S';
private static final char INTEGER = 'I';
private static final char BIGDECIMAL = 'B';
private static final char DATE = 'D';
private static final char MODULELIST = 'M';
private static final char EQUALS = '=';
private static final char DATA = '#';
public static final int MAX = 200; // 防止StackOver Overflow
private static final String FORMATDATE = "yyyyMMddHHmmssSSS";
private static final String FORMATINT = "0.00";
private static final char CR = '\r';
private static final char STRING = 'S';
private static final char INTEGER = 'I';
private static final char BIGDECIMAL = 'B';
private static final char DATE = 'D';
private static final char MODULELIST = 'M';
private static final char EQUALS = '=';
private static final char DATA = '#';
private static final String SPLIT = "//";
static final Format format = new DecimalFormat("#.######");
static final Format dateFormat = new SimpleDateFormat("yyyy/MM/dd");
......@@ -968,6 +970,7 @@ public class MdaUtils {
log.error("模型序列化异常", e);
}
}
/**
* 模板数据保存
**/
......@@ -976,55 +979,54 @@ public class MdaUtils {
// os.insertLine(0,module.getName());
try {
os.getOutputStream().write((module.getName()+CR).getBytes(StandardCharsets.UTF_8));
}catch (IOException e){
os.getOutputStream().write((module.getName() + CR).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error("流数据写入失败");
}
writeData("",module, os , 0);
writeData("", module, os, 0);
}
/**
* 递归保存模型中的叶子节点
**/
private static int writeData(String parentDir ,IModule module, IStream os ,int deep) {
private static int writeData(String parentDir, IModule module, IStream os, int deep) {
String thisDir = "";
if(parentDir.length() !=0){
thisDir = parentDir +File.separator;
if (parentDir.length() != 0) {
thisDir = parentDir + SPLIT;
}
String moduleName = module.getClass().getName();
Class moduleClass = module.getClass();
Field[] fields =moduleClass.getDeclaredFields();
Field[] fields = moduleClass.getDeclaredFields();
String fieldName = "";
String getMethodName = "";
Object fieldVal = null;
Method method = null;
for(Field field: fields){
for (Field field : fields) {
fieldName = field.getName();
if(fieldName.equalsIgnoreCase("log")){
if (fieldName.equalsIgnoreCase("log")) {
continue;
}
getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
method= moduleClass.getDeclaredMethod(getMethodName);
method = moduleClass.getDeclaredMethod(getMethodName);
fieldVal = method.invoke(module);
if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) {
if (!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) {
//如果是IMODULE 属性 ,递归获取
if (fieldVal instanceof IModule && !(fieldVal instanceof IModuleList)) {
String subDir = thisDir + fieldName;
deep = writeData(subDir, (IModule) fieldVal, os , deep);
} else{
deep = writeData(subDir, (IModule) fieldVal, os, deep);
} else {
//如果是ModuleList
if(field.getType().getName().equals("com.brilliance.mda.runtime.mda.impl.ModuleList")){
if (field.getType().getName().equals("com.brilliance.mda.runtime.mda.impl.ModuleList")) {
IModuleList<IModule> list = (IModuleList) fieldVal;
if (!list.isEmpty() && list.size() > 0 )
{
if (!list.isEmpty() && list.size() > 0) {
String subDir = thisDir + fieldName;
String line = thisDir+ fieldName + EQUALS + MODULELIST + list.size();
String line = thisDir + fieldName + EQUALS + MODULELIST + list.size();
try {
os.getOutputStream().write((line+CR).getBytes(StandardCharsets.UTF_8));
os.getOutputStream().write((line + CR).getBytes(StandardCharsets.UTF_8));
deep++;
}catch (IOException e){
} catch (IOException e) {
log.error("流数据写入失败");
}
deep = 0; // 设置ModuleList可将读取栈清空
......@@ -1034,46 +1036,45 @@ public class MdaUtils {
}
}
//叶子节点
}else if (fieldVal instanceof String || fieldVal instanceof Number || fieldVal instanceof Date){
} else if (fieldVal instanceof String || fieldVal instanceof Number || fieldVal instanceof Date) {
String newFieldVal = "";
//数值类型 ,值为0的 不保存
if (fieldVal instanceof Number && ((Number) fieldVal).doubleValue() == 0) {
return deep;
}
if(field.getType().getName().equals("java.util.Date")){
if (field.getType().getName().equals("java.util.Date")) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMATDATE);
newFieldVal = DATE + simpleDateFormat.format((Date)fieldVal) ;
}else if(field.getType().getName().equals("java.lang.Integer")){
newFieldVal = INTEGER+fieldVal.toString();
}else if(field.getType().getName().equals("java.math.BigDecimal")){
newFieldVal = BIGDECIMAL+new DecimalFormat(FORMATINT).format(new BigDecimal(fieldVal.toString()));
}else if(field.getType().getName().equals("java.lang.String")){
newFieldVal = STRING+fieldVal.toString().replaceAll("(\r\n)|\r|\n", "\r" + DATA);
newFieldVal = DATE + simpleDateFormat.format((Date) fieldVal);
} else if (field.getType().getName().equals("java.lang.Integer")) {
newFieldVal = INTEGER + fieldVal.toString();
} else if (field.getType().getName().equals("java.math.BigDecimal")) {
newFieldVal = BIGDECIMAL + new DecimalFormat(FORMATINT).format(new BigDecimal(fieldVal.toString()));
} else if (field.getType().getName().equals("java.lang.String")) {
newFieldVal = STRING + fieldVal.toString().replaceAll("(\r\n)|\r|\n", "\r" + DATA);
}
String line = thisDir+ fieldName + EQUALS + newFieldVal;
String line = thisDir + fieldName + EQUALS + newFieldVal;
try {
os.getOutputStream().write((line+CR).getBytes(StandardCharsets.UTF_8));
os.getOutputStream().write((line + CR).getBytes(StandardCharsets.UTF_8));
deep++;
if (deep == MAX)
{
if (deep == MAX) {
os.getOutputStream().write(CR); // 多写一个回车便于LoadModule为退出loadline的循环调用
deep = 0;
}
}catch (IOException e){
} catch (IOException e) {
log.error("流数据写入失败");
}
}else{
} else {
deep = 0;
continue;
}
}
}
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, getMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, getMethodName);
} catch (NoSuchMethodException e) {
log.error("方法{}未找到",getMethodName);
log.error("方法{}未找到", getMethodName);
}
}
return deep;
......@@ -1085,40 +1086,40 @@ public class MdaUtils {
return Codetables.getCodetableLabel(key, codetable, locale.getLanguage());
}
public static void loadData(IModule module ,IStream stream) {
public static void loadData(IModule module, IStream stream) {
module.clear();
String[] lines = null;
try {
lines = stream.getOutputStream().toString().split("\r");
}catch (Exception e){
} catch (Exception e) {
log.error("获取流数据失败");
}
boolean isLoading = false;
if(lines.length > 0){
if (lines.length > 0) {
Class moduleClass = module.getClass();
for (String line : lines ) {
for (String line : lines) {
//去除空格
line.trim();
//空行和#开头的注释行 跳过
if(line.length() == 0 || line.indexOf(DATA) == 0){
if (line.length() == 0 || line.indexOf(DATA) == 0) {
continue;
}
//读取到本模型模型名相同数据的行,开始获取模型数据
if(moduleClass.getName().toUpperCase().indexOf(line.toUpperCase()) != -1){
if (moduleClass.getName().toUpperCase().indexOf(line.toUpperCase()) != -1) {
//开始获取模型数据标识
isLoading = true;
continue;
}
if(isLoading){
if (isLoading) {
//读取到下一个模型(不包含"="好的列为模型名),跳出循环
if(line.indexOf("=") == -1) {
if (line.indexOf("=") == -1) {
break;
}
//获取模型数据
String val = line.split("=")[1].trim();
String key = line.split("=")[0].trim();
readData(module,key,val);
readData(module, key, val);
}
}
}
......@@ -1128,10 +1129,10 @@ public class MdaUtils {
String fieldName = "";
Class moduleClass = module.getClass();
String moduleName = moduleClass.getName();
Method getMethod = null ;
Method setMethod = null ;
Method getMethod = null;
Method setMethod = null;
//叶子节点赋值
if(key.indexOf(File.separator) == -1){
if (key.indexOf(SPLIT) == -1) {
//获取setXXX方法名
fieldName = key;
String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
......@@ -1139,79 +1140,79 @@ public class MdaUtils {
Field field = moduleClass.getDeclaredField(fieldName);
Class fieldTyp = field.getType();
//获取setXXX方法
setMethod= moduleClass.getDeclaredMethod(setMethodName,field.getType());
setMethod = moduleClass.getDeclaredMethod(setMethodName, field.getType());
//根据类型 对数据进行处理
Object val = null;
if(oldval.indexOf(MODULELIST)==0){
if (oldval.indexOf(MODULELIST) == 0) {
return;
}
if(oldval.indexOf(BIGDECIMAL) == 0){
if (oldval.indexOf(BIGDECIMAL) == 0) {
val = new BigDecimal(oldval.substring(1));
}else if(oldval.indexOf(DATE) == 0){
} else if (oldval.indexOf(DATE) == 0) {
//String转Date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMATDATE);
try {
val= simpleDateFormat.parse(oldval.substring(1));
val = simpleDateFormat.parse(oldval.substring(1));
} catch (ParseException e) {
log.error("日期{}格式不正确",oldval);
log.error("日期{}格式不正确", oldval);
}
}else if(oldval.indexOf(INTEGER) == 0){
val= new Integer(oldval.substring(1));
}else if(oldval.indexOf(STRING) == 0){
} else if (oldval.indexOf(INTEGER) == 0) {
val = new Integer(oldval.substring(1));
} else if (oldval.indexOf(STRING) == 0) {
val = oldval.substring(1).toString();
}
setMethod.invoke(module,val);
setMethod.invoke(module, val);
} catch (NoSuchMethodException e) {
log.error("方法{}未找到",setMethodName);
log.error("方法{}未找到", setMethodName);
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,setMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, setMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,setMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, setMethodName);
} catch (NoSuchFieldException e) {
log.error("模型{}中属性{}未找到",moduleName,fieldName);
log.error("模型{}中属性{}未找到", moduleName, fieldName);
}
}else{//非叶子节点继续迭代
} else {//非叶子节点继续迭代
//获取setXXX方法名
fieldName = key.substring(0,key.indexOf(File.separator));
fieldName = key.substring(0, key.indexOf(SPLIT));
//moduleList 的数据包含 [?]
int index = -1;
if(fieldName.indexOf("[") >0){
if (fieldName.indexOf("[") > 0) {
String[] fieldNameArr = fieldName.split("\\[");
fieldName = fieldNameArr[0];
index = Integer.parseInt(fieldNameArr[1].replace("]",""));
index = Integer.parseInt(fieldNameArr[1].replace("]", ""));
}
String newKey = key.substring(key.indexOf(File.separator)+1);
String newKey = key.substring(key.indexOf(SPLIT) + 1);
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
//查询getXXX方法
getMethod= moduleClass.getDeclaredMethod(getMethodName);
getMethod = moduleClass.getDeclaredMethod(getMethodName);
//获取下次递归的模型
IModule subModule = (IModule)getMethod.invoke(module);
if(subModule instanceof ModuleList){
if(subModule == null ){
IModule subModule = (IModule) getMethod.invoke(module);
if (subModule instanceof ModuleList) {
if (subModule == null) {
subModule = new ModuleList();
}
IModuleList<IModule> list = (IModuleList)subModule;
if(list.size() -1 < index){
IModuleList<IModule> list = (IModuleList) subModule;
if (list.size() - 1 < index) {
try {
list.add(((ModuleList<?>) subModule).getDataClass().newInstance());
}catch (InstantiationException e) {
} catch (InstantiationException e) {
e.printStackTrace();
}
}
readData(list.get(index), newKey, oldval);
}else {
} else {
readData(subModule, newKey, oldval);
}
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, getMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
log.error("模型{}中执行方法{}时出错", moduleName, getMethodName);
} catch (NoSuchMethodException e) {
log.error("模型{}方法{}未找到",moduleName,getMethodName);
log.error("模型{}方法{}未找到", moduleName, getMethodName);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment