1、xml文件结构如下:
<?xml version="1.0" encoding="UTF-8"?> <datasync> <readmodule> <read name='read_one'> <path>/data/rsync/read_one</path> <ip>127.0.0.1</ip> <comment>readOne</comment> </read> <read name='read_two'> <path>/data/rsync/read_two</path> <ip>127.0.0.1</ip> <comment>readTwo</comment> </read> </readmodule> <writemodule> <write name='write_one'> <path>/data/rsync/write_one</path> <ip>127.0.0.1</ip> <comment>writeOne</comment> </write> <write name='write_two'> <path>/data/rsync/write_two</path> <ip>127.0.0.1</ip> <comment>writeTwo</comment> </write> </writemodule> </datasync>
2、model类如下:
(1)dataSync
public class DataSync {
private ReadModule readmodule;
private WriteModule writemodule;
}
get/set方法省略
(2)ReadModule
public class ReadModule {
private List<Read> reads;
}
(3)WriteModule
public class WriteModule {
private List<Write> writes;
}
(4)Read
public class Read {
private String name;
private String path;
private String ip;
private String comment;
}
(5)Write
public class Write {
private String name;
private String path;
private String ip;
private String comment;
}
3、controller如下:
public class DataRsyncController{
private Log logger = LogFactory.getLog(this.getClass());
private static final String readBasePath="/data/rsync/to_3rd_party_data/data_sync";
private static final String writeBasePath="/data/rsync/3rd_party_data/data_sync";
private static final String buildBasePath=System.getProperty("user.dir") + "/dataSync/";
private static String xmlHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
private static String PREFIX_CDATA = "<![CDATA[";
private static String SUFFIX_CDATA = "]]>";
@RequestMapping
public String test(ModelMap model) throws IOException{
buildDataSyncXml();
return "commons/message";
}
private void buildDataSyncXml() throws IOException{
// 解析xml得到dataSyncXml对象
XStream xstream = buildDataSyncXStream();
// 重新组装DataSyncXml对象
DataSyncXml reLoadDataSyncXml = reLoadDataSyncXml();
FileWriter fileWriter = new FileWriter(buildBasePath+"datasync_server_config.xml");
fileWriter.write(xmlHead+"\n");
xstream.toXML(reLoadDataSyncXml,fileWriter);
fileWriter.close();
}
/**
* 获取data_sync表的记录重新组装DataSyncXml对象
* @param dataSyncXml
* @return
*/
private DataSyncXml reLoadDataSyncXml(){
List<Read> reads = new ArrayList<Read>();
List<Write> writes = new ArrayList<Write>();
List<DataSync> dataSyncs = dataSyncService.getDataSyncList();
for(DataSync dataSync:dataSyncs){
String syncType = dataSync.getSyncType();
String moduleCode = dataSync.getModuleCode();
if("read".equals(syncType)){
Read read = new Read();
read.setName(moduleCode);
read.setPath(dataSync.getPath());
read.setIp(dataSync.getIpLimit());
read.setComment(dataSync.getModuleName()+";"+dataSync.getAuthor()+";"+dataSync.getRemark());
reads.add(read);
}else if("write".equals(syncType)){
Write write = new Write();
write.setName(moduleCode);
write.setPath(dataSync.getPath());
write.setIp(dataSync.getIpLimit());
write.setComment(dataSync.getModuleName()+";"+dataSync.getAuthor()+";"+dataSync.getRemark());
writes.add(write);
}
}
ReadModule readModule = new ReadModule();
WriteModule writeModule = new WriteModule();
DataSync dataSync = new DataSync();
readModule.setReads(reads);
writeModule.setWrites(writes);
dataSync.setReadmodule(readModule);
dataSync.setWritemodule(writeModule);
return dataSync;
}
private static XStream buildDataSyncXStream() {
// XStream xstream = new XStream(new DomDriver());
XStream xstream = initXStream(false);
xstream.alias("datasync", DataSync.class);
xstream.alias("readmodule", ReadModule.class);
xstream.alias("read", Read.class);
xstream.alias("writemodule", WriteModule.class);
xstream.alias("write", Write.class);
//忽略下列的名字的节点
xstream.addImplicitCollection(ReadModule.class, "reads");
xstream.addImplicitCollection(WriteModule.class, "writes");
//使用这个属性名作为节点上的元素
xstream.useAttributeFor(Read.class, "name");
xstream.useAttributeFor(Write.class, "name");
return xstream;
}
// 判断是否需要检测“"<![CDATA ]]>”特殊字符
private static XStream initXStream(boolean isAddCDATA){
XStream xstream = null;
if(isAddCDATA){
xstream = new XStream(
new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
protected void writeText(QuickWriter writer, String text) {
if(text.startsWith(PREFIX_CDATA)
&& text.endsWith(SUFFIX_CDATA)) {
writer.write(text);
}else{
super.writeText(writer, text);
}
}
};
};
}
);
}else{
xstream = new XStream(new XppDriver());
}
return xstream;
}
}