package com.whyc.service;
|
|
import com.whyc.dto.ActionUtil;
|
import com.whyc.dto.FileParamToXml;
|
import com.whyc.dto.Response;
|
import com.whyc.dto.XmlFileOpreate;
|
import com.whyc.pojo.FileInfo;
|
import com.whyc.pojo.FileParam;
|
import org.springframework.stereotype.Service;
|
|
import java.io.File;
|
import java.lang.reflect.Field;
|
import java.text.ParseException;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Service
|
public class FileParamService {
|
|
//解析xml文件
|
public Response getXmlValue(String filePath) {
|
File file=new File(filePath);
|
if(file.exists()){
|
FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
|
return new Response().setII(1,fileInfo!=null,fileInfo,"返回解析数据");
|
}else{
|
return new Response().set(1,false,"文件路径不正确");
|
}
|
}
|
|
|
//通过修改属性窗口值来修改文件值
|
public Response updateXmlByFileParam(FileParam fileParam,String filePath) {
|
Map<String,String> map=new HashMap<>();
|
Class paramClass=fileParam.getClass();
|
// 获取所有的属性数组
|
Field[] fields = paramClass.getDeclaredFields();
|
for (Field field:fields) {
|
field.setAccessible(true);
|
try {
|
String paramName=field.getName();
|
//获取属性值
|
Object obj=field.get(fileParam);
|
if(obj==null){
|
obj="null";
|
}
|
String paramValue=obj.toString();
|
if(field.getType().toString().equals("class java.util.Date")){
|
if(!paramValue.equals("null")){
|
paramValue= ActionUtil.sdfwithALL.format(ActionUtil.df.parse(paramValue));
|
}
|
}
|
String xmlName=FileParamToXml.getNameByType(paramName);
|
if(xmlName!=null&&!xmlName.isEmpty()){
|
map.put(xmlName,paramValue);
|
}
|
} catch (IllegalAccessException | ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
boolean bl=false;
|
if(map.size()>0){
|
bl=XmlFileOpreate.writeXml(map,filePath);
|
}
|
return new Response().setII(1,bl,map,"修改文件");
|
}
|
//通过修改属性窗口值来修改文件值
|
public Response updateXmlByParamMap(Map<String,String> map,String filePath) {
|
boolean bl=false;
|
if(map.size()>0){
|
bl=XmlFileOpreate.writeXml(map,filePath);
|
}
|
return new Response().setII(1,bl,map,"修改文件");
|
}
|
}
|