Skip to content

Instantly share code, notes, and snippets.

@winse
Last active July 31, 2018 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save winse/9540124 to your computer and use it in GitHub Desktop.
Save winse/9540124 to your computer and use it in GitHub Desktop.
调整XML的的结构。增加属性/节点,调整节点层次,转多配置文件等。
package com.importconfig
import groovy.util.XmlNodePrinter;
import groovy.util.XmlNodePrinter.NamespaceContext;
import java.io.PrintWriter;
import java.util.Map;
import org.codehaus.groovy.runtime.InvokerHelper;
/**
* 把v1.0的配置文件转换为v2.0
* @version v2.0
*/
// XSLT Extensible Stylesheet Language Transformations
public class XSLTtransform {
def toNumber(obj){
if(obj==null || obj.isEmpty()){
return Integer.MAX_VALUE;
}
return Integer.valueOf(obj);
}
def toStringUtil(obj){
if(obj==null || obj.isEmpty()){
return "";
}
return obj.toString();
}
def execute(){
/* 测试时获取当前路径下的文件 */
def importConfig
/* 获取当前路径下的原始配置文件 */
def file
if( false && inEclpse){
def parentFile = new File(getClass().getResource(".").toURI())
importConfig = "excelimportconifg.xml"
file = new File(parentFile, importConfig);
}else{
importConfig = "E:/cndata-add/workspaces/zj_bpaf/tools/xmlgen/test/com/importconfig/excelimportconifg.xml"
file = new File(importConfig)
}
def root = new XmlParser().parse(file);
// 获取全部的第二节点Excel
def excels = root.Excel
excels.each { excel ->
// 添加/修改属性
excel.@multiple = 'N'
// 拷贝Excel的孩子节点,并进行排序
def excelChildren = excel.children().clone().sort{a,b->
// 先按name,再excelRowNum,最后考虑tableName进行排序
a.name()<=>b.name()?:toNumber(a.@excelRowNUM) <=> toNumber(b.@excelRowNUM)?: a.@tableName<=>b.@tableName
}
// 删除excel节点的所有孩子节点
excel.children().clear()
// 增加sheet节点,同时设置sheet的属性index和caption
excel.appendNode('sheet', [index: "1", caption:""], )
// 遍历excel原始的clumn孩子节点(上面已经进行了clone)
excelChildren.findAll({it.name()=='clumn'}).each{clumn->
// 找到
def clumnNode = excel.sheet[0].clumn.find{it.@excelRowNUM==toStringUtil(clumn.@excelRowNUM)}
if(!clumnNode){
if(clumn.@excelRowNUM == null || clumn.@excelRowNUM.isEmpty() ){
excel.sheet[0].appendNode('clumn', [
excelRowNUM:""
], )
}else{
excel.sheet[0].appendNode('clumn', [
excelRowNUM:toStringUtil(clumn.@excelRowNUM),
efficacy:toStringUtil(clumn.@efficacy),
efficacySql:toStringUtil(clumn.@efficacySql),
efficacyerrMeg:toStringUtil(clumn.@efficacyerrMeg)
], )
}
}
clumnNode = excel.sheet[0].clumn.find{it.@excelRowNUM==toStringUtil(clumn.@excelRowNUM)}
clumnNode.appendNode('column', [
caption:toStringUtil(clumn.@meg) ,
name:toStringUtil(clumn.@clumName) ,
tableName:toStringUtil(clumn.@tableName) ,
type:toStringUtil(clumn.@clumType) ,
transValueSql:toStringUtil(clumn.@transValueSql),
// XXX 这里的对应关系要注意哦
confirmValue:toStringUtil(clumn.@confimValue),
isID:toStringUtil(clumn.@isID),
childTable:toStringUtil(clumn.@childTable).replaceAll("\\.", ","),
childClum:toStringUtil(clumn.@childClum).replaceAll("\\.", ","),
isupdateKey:toStringUtil(clumn.@isupdateKey)
],)
}
// 遍历excel原始的extsql孩子节点(上面已经进行了clone)
excelChildren.findAll({it.name()=='extsql'}).each{extsql->
if(!excel.sheet[0].extsql){
excel.sheet[0].appendNode('extsql')
}
excel.sheet[0].extsql[0].appendNode('SQL', [
sql:toStringUtil(extsql.@sql)
],)
}
}
def outFolder = new File(".", "gen-out");
outFolder.deleteDir();
outFolder.list().each { println it }
outFolder.mkdirs();
// 输出, 每个excel配置写一个文件
excels.each{
def newRootNode = new XmlParser().parseText('<excelimport />')
newRootNode.append(it);
def excelPartFile = new File(outFolder, it.@name + "_" + it.@apptype + "_ExcelImportConifg.xml")
def writer = new PrintWriter(excelPartFile, "UTF8")
writer.write('<?xml version="1.0" encoding="UTF-8"?>\n\n');
new XmlPrinter(writer).print(newRootNode)
}
// new XmlNodePrinter(new PrintWriter(System.out)).print(root)
}
static main(args){
new XSLTtransform().execute();
}
}
/**
* 属性值为null或者空字符串不打印
*
* @author LFQ
*
*/
class XmlPrinter extends XmlNodePrinter{
public XmlPrinter(PrintWriter out) {
super(out);
}
protected void printNameAttributes(Map attributes, NamespaceContext ctx) {
if (attributes == null || attributes.isEmpty()) {
return;
}
for (Object p : attributes.entrySet()) {
Map.Entry entry = (Map.Entry) p;
Object value = entry.getValue();
if(value == null || value.isEmpty()){
continue;
}
out.print(" ");
out.print(getName(entry.getKey()));
out.print("=");
out.print(quote);
if (value instanceof String) {
printEscaped((String) value, true);
} else {
printEscaped(InvokerHelper.toString(value), true);
}
out.print(quote);
printNamespace(entry.getKey(), ctx);
}
}
private String getName(Object object) {
return super.getName(object);
}
private void printEscaped(String s, boolean isAttributeValue) {
super.printEscaped(s, isAttributeValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment