config.xml为比较规则,misInitConfig.xml,misInitConfig1.xml为比较文件
比较方法:
public class XMLConfigImpl implements ConfigInterface{
/**config.xml文件路径*/
private String configFilePath="config.xml";
/**源文件中没有的配置项个数*/
private static int srcNumber=0;
/**目标文件中没有的配置项个数*/
private static int newNumber=0;
/**值不同的配置项个数*/
private static int differentNumber=0;
/**
* 入口方法,打印结果
* @param srcFilePath 源配置文件路径
* @param newFilePath 目标配置文件路径
* @param outFilePath 输出文件路径
*/
public void compareConfig(String srcFilePath,String newFilePath,
String outFilePath) {
Element srcFileRoot=XMLUtil.getXMLRoot(srcFilePath);
Element newFileRoot=XMLUtil.getXMLRoot(newFilePath);
System.out.println("源配置文件:"+srcFilePath);
System.out.println("目标配置文件:"+newFilePath);
System.out.println("**********详细情况*************");
readConfig(srcFilePath,srcFileRoot,newFileRoot);
System.out.println("**********统计结果*************");
System.out.println("源文件中没有的配置项共:"+srcNumber+"个");
System.out.println("目标文件中没有的配置项共:"+newNumber+"个");
System.out.println("值不同的配置项共:"+differentNumber+"个");
srcNumber=0;
newNumber=0;
differentNumber=0;
}
/**
* 根据配置文件名称查找config.xml文件并解析
* @param srcFileName 源配置文件名称
* @param srcFileRoot 源文件根节点
* @param newFileRoot 目标文件根节点
*/
public void readConfig(String srcFileName,Element srcFileRoot,Element newFileRoot ){
Element configRoot=XMLUtil.getXMLRoot(configFilePath);
List fileList=configRoot.getChildren("file");
Element childrenRoot = null;
List ruleList=new ArrayList();
for(int i=0;i<fileList.size();i++){
Element fileElement=(Element)fileList.get(i);
String name=fileElement.getAttributeValue("name");
if(name.equals(srcFileName)){
childrenRoot=(Element)fileList.get(i);
ruleList=childrenRoot.getChildren("rule");
for(int j=0;j<ruleList.size();j++){
Element ruleElement=(Element)ruleList.get(j);
String type=ruleElement.getAttributeValue("type");
String path=ruleElement.getText();
if("1".equals(type)){
String standard=new String();
String[] pathAndStandard=path.split("\\{");
standard=pathAndStandard[1].substring(0,pathAndStandard[1].length()-1);
parseTypeOne(pathAndStandard[0],standard,newFileRoot);
}else if("2".equals(type)){
String[] pathAndStandard=path.split("\\[");
//System.out.print(pathAndName[1]);
String standard=new String();
standard=pathAndStandard[1].substring(0,pathAndStandard[1].length()-1);
parseTypeTwo(pathAndStandard[0],newFileRoot);
// System.out.println(standard);
}else if("3".equals(type)){
parseTypeThree(path,newFileRoot);
}
}
}
}
}
/**
* type为“1”的rule节点解析
* @param path rule节点Text中的路径
* @param standard rule节点Text中的比较标准
* @param srcFileRoot 源文件根节点
* @param newFileRoot 目标文件根节点
*/
public void parseTypeOne(String path,String standard,Element newFileRoot){
try{
List srcList = XPath.selectNodes(srcFileRoot,"/"+path);
List newList = XPath.selectNodes(newFileRoot,"/"+path);
List srcStringList=new ArrayList();
List newStringList=new ArrayList();
for(Iterator m=srcList.iterator();m.hasNext();){
Element srcElement=(Element)m.next();
Element childSrcElement=srcElement.getChild(standard);
String childSrcText=childSrcElement.getText();
srcStringList.add(childSrcText);
}
for(Iterator n= newList.iterator();n.hasNext();){
Element newElement=(Element)n.next();
Element childNewElement=newElement.getChild(standard);
String childNewText=childNewElement.getText();
newStringList.add(childNewText);
if(!srcStringList.contains(childNewText)){
System.out.println("节点"+newElement.getName()+",子节点Text是"+childNewText+"不存在源文件中");
srcNumber++;
}
}
for(Iterator i = srcList.iterator();i.hasNext();){
Element srcElement= (Element)i.next();
Element childSrcElement=srcElement.getChild(standard);
String childSrcText=childSrcElement.getText();
if(!newStringList.contains(childSrcText)){
System.out.println("节点"+srcElement.getName()+",子节点Text是"+childSrcText+"不存在目标文件中");
newNumber++;
}
for(Iterator j = newList.iterator();j.hasNext();){
Element newElement= (Element)j.next();
Element childNewElement=newElement.getChild(standard);
String childNewText=childNewElement.getText();
if(childSrcText.equals(childNewText)){
compareElement(srcElement,newElement);
}
}
}
}catch(JDOMException e){
e.printStackTrace();
}
}
/**
* type为“2”的rule节点解析
* @param path rule节点中的Text中的路径
* @param standard rule节点Text中的比较标准
* @param srcFileRoot 源文件根节点
* @param newFileRoot 目标文件根节点
*/
public void parseTypeTwo(String path,"/"+path);
List srcStringList=new ArrayList();
List newStringList=new ArrayList();
for(Iterator m=srcList.iterator();m.hasNext();){
Element srcElement=(Element)m.next();
srcStringList.add(srcElement.getAttributeValue(standard).toString());
}
for(Iterator n= newList.iterator();n.hasNext();){
Element newElement=(Element)n.next();
newStringList.add(newElement.getAttributeValue(standard).toString());
if(!srcStringList.contains(newElement.getAttributeValue(standard).toString())){
System.out.println("节点"+newElement.getName()+",属性是"+newElement.getAttributeValue(standard)+"不存在源文件中");
srcNumber++;
}
}
for(Iterator i = srcList.iterator();i.hasNext();){
Element srcElement= (Element)i.next();
String srcAttribute=srcElement.getAttributeValue(standard);
if(!newStringList.contains(srcAttribute.toString())){
System.out.println("节点"+srcElement.getName()+",属性是"+srcAttribute+"不存在目标文件中");
newNumber++;
}
for(Iterator j = newList.iterator();j.hasNext();){
Element newElement= (Element)j.next();
String newAttribute=newElement.getAttributeValue(standard);
if(srcAttribute.equals(newAttribute)){
compareElement(srcElement,newElement);
}
}
}
}catch(JDOMException e){
e.printStackTrace();
}
}
/**
* type为“3”的rule节点解析
* @param path rule节点Text中的路径
* @param srcFileRoot 源文件根节点
* @param newFileRoot 目标文件根节点
*/
public void parseTypeThree(String path,Element newFileRoot){
try{
Element srcParentElement = (Element)XPath.selectSingleNode(srcFileRoot,"/"+path);
Element newParentElement = (Element)XPath.selectSingleNode(newFileRoot,"/"+path);
List srcList = srcParentElement.getChildren();
List newList = newParentElement.getChildren();
List srcStringList=new ArrayList();
List newStringList=new ArrayList();
for(Iterator m=srcList.iterator();m.hasNext();){
Element srcElement=(Element)m.next();
srcStringList.add(srcElement.toString());
}
for(Iterator n= newList.iterator();n.hasNext();){
Element newElement=(Element)n.next();
newStringList.add(newElement.toString());
if(!srcStringList.contains(newElement.toString())){
System.out.println("节点"+newElement.getName()+"不存在源文件中");
srcNumber++;
}
}
for(Iterator i = srcList.iterator();i.hasNext();){
Element srcElement= (Element)i.next();
if(!newStringList.contains(srcElement.toString())){
System.out.println("节点"+srcElement.getName()+"不存在目标文件中");
newNumber++;
}
for(Iterator j = newList.iterator();j.hasNext();){
Element newElement= (Element)j.next();
if(srcElement.toString().equals(newElement.toString())){
compareElement(srcElement,newElement);
}
}
}
}catch(JDOMException e){
e.printStackTrace();
}
}
/**
* 把节点存入ElementBean并调用ElementBean的比较方法
* @param srcElement 源文件节点根节点
* @param newElement 目标文件根节点
*/
public void compareElement(Element srcElement,Element newElement){
List srcList = new ArrayList();
getElementList(srcElement,srcList,1);
List newList = new ArrayList();
getElementList(newElement,newList,1);
for (int m = 0; m < srcList.size(); m++) {
ElementBean srcBean = (ElementBean)srcList.get(m);
int level = srcBean.getLevel();
String name = srcBean.getName();
for (int n = 0; n < newList.size(); n++) {
ElementBean newBean = (ElementBean) newList.get(n);
int lev = newBean.getLevel();
if(level == lev) {
String targetName = newBean.getName();
if(name.equals(targetName)) {
compareElementBean(srcBean,newBean);
}
}
}
}
}
/**
* 得到一个节点List,用ElementBean来封装节点的信息,然后将所有节点存入List
* @param element 节点对象
* @param elementList 用来存ElementBean的list
* @param level 节点层级,从1级开始。
*/
public void getElementList(Element element,List elementList,int level) {
ElementBean elementBean = new ElementBean();
elementBean.setName(element.getName());
String text = element.getText();
if(!"".equals(text)) {
elementBean.setText(text);
}
List attributes = element.getAttributes();
Map attributeMap = new HashMap();
for (int i = 0; i < attributes.size(); i++) {
Attribute attribute = (Attribute) attributes.get(i);
String attrName = attribute.getName();
String attrValue = attribute.getValue();
attributeMap.put(attrName,attrValue);
}
if(!attributeMap.isEmpty()) {
elementBean.setAttribute(attributeMap);
}
elementBean.setLevel(level);
elementList.add(elementBean);
List elements = element.getChildren();
for (int j = 0; j < elements.size(); j++) {
Element ele = (Element) elements.get(j);
getElementList(ele,elementList,level+1);
}
}
/**
* 比较两个ElementBean
* @param srcBean 源ElementBean
* @param newBean 目标ElementBean
*/
public void compareElementBean(ElementBean srcBean,ElementBean newBean) {
String name = srcBean.getName();
String srcText = srcBean.getText();
if(!srcText.equals(newBean.getText())) {
System.out.println("节点" + name + ",层级为:" + srcBean.getLevel()+" 的text属性不一样" );
differentNumber++;
}
Map srcAttrMap = srcBean.getAttribute();
Map targetAttrMap = newBean.getAttribute();
Set srcMapKeySey = srcAttrMap.keySet();
Set targetMapKeySey = targetAttrMap.keySet();
for(Iterator it=targetMapKeySey.iterator();it.hasNext();){
String targetMapKey=(String)it.next();
String targetValue=(String)targetAttrMap.get(targetMapKey);
if(srcAttrMap.containsKey(targetMapKey)){
if(null==targetValue&&null!=srcAttrMap.get(targetMapKey)){
System.out.println("节点"+name+"的属性:" + targetMapKey + "值不同");
differentNumber++;
}
}else{
System.out.println("节点"+name+"的属性:"+targetMapKey+"不存在源文件中");
differentNumber++;
}
}
for (Iterator iterator = srcMapKeySey.iterator(); iterator.hasNext();) {
String srcMapKey = (String ) iterator.next();
String srcMapValue=(String)srcAttrMap.get(srcMapKey);
if(targetAttrMap.containsKey(srcMapKey)) {
if(null!=srcMapValue){
if(null!=targetAttrMap.get(srcMapKey)){
if(!srcMapValue.equals(targetAttrMap.get(srcMapKey))) {
System.out.println("节点"+name+"的属性:" + srcMapKey +"值不同");
differentNumber++;
}
}
}
if(null!=targetAttrMap.get(srcMapKey)&&null==srcMapValue){
System.out.println("节点"+name+"的属性:" + srcMapKey + "值不同");
differentNumber++;
}
} else {
System.out.println("节点"+name+"的属性:"+srcMapKey+"不存在目标文件中");
differentNumber++;
}
}
}
public static Element getXMLRoot(String xmlPath) {
SAXBuilder builder = new SAXBuilder();
Document doc = null;
Element root = null;
try {
doc = builder.build(xmlPath);
root = doc.getRootElement();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return root;
}
public static void main(String[] args) {
XMLConfigImpl config = new XMLConfigImpl();
config.compareConfig("misInitConfig.xml","misInitConfig1.xml","");
}
}
config.xml规则文件:
<?xml version="1.0" encoding="UTF-8"?>
<configs>
<!--
1、用节点的text作为标准进行比较
2、用节点的属性作为标准进行比较
3、用节点名称作为标准进行比较
-->
<file name="reportConfig.xml">
<rule type="1">/reportConfig/config{name}</rule>
</file>
<file name="DemoCenterConfig.xml">
<rule type="2">/items/demoItems/item[title]</rule>
<rule type="2">/items/docItems/item[id]</rule>
</file>
<file name="misInitConfig.xml">
<rule type="3">/misInitConfig/basicInfo</rule>
<rule type="3">/misInitConfig/logManage</rule>
</file>
</configs>
<?xml version="1.0" encoding="UTF-8" ?>
<misInitConfig>
<basicInfo>
<conf_frameMenuLayout dataType="3" default="null" inputType="radio" value="1" />
<conf_title_login dataType="11" default="null" inputType="text" value="登陆" />
<conf_dataFormat dataType="11" default="YYYY/MM/dd hh24:mi:ss" inputType="text" value="YYYY/MM/dd hh24:mi:ss" />
<conf_graphIntoRaq dataType="11" default="0" inputType="text" value="0" />
<conf_ftpIP dataType="11" default="null" inputType="text" value="192.168.0.145" />
<conf_attachmentServlet dataType="11" default="null" inputType="text" value="AttachmentServlet,attachment_path,attachment_code,attachment_order" />
<conf_qtptest dataType="11" default="null" inputType="text" value="false" />
<conf_dicInherit_define dataType="11" default="null" inputType="text" value="1" />
<conf_savedDirectly dataType="11" default="1" inputType="text" value="1" />
<conf_openWebTest dataType="11" default="null" inputType="text" value="close" />
<conf_maxCellsWarnNum dataType="11" default="null" inputType="text" value="10000" />
<conf_showToplogo dataType="11" default="true" inputType="text" value="true" />
<conf_refreshMinute dataType="11" default="5" inputType="text" value="5" />
<conf_email dataType="11" default="null" inputType="text" value="@runqian.com.cn" />
<conf_gis_path dataType="11" default="null" inputType="text" value="/webroot/mis2/gisChart/data.xml" />
<conf_rootorg dataType="1" default="9999" inputType="text" value="9999" />
<conf_resUseTab dataType="11" default="null" inputType="text" value="1" />
<conf_orgInherit dataType="11" default="null" inputType="text" value="1" />
<conf_startHsql dataType="11" default="null" inputType="text" value="no" />
<conf_showDataSource dataType="11" default="null" inputType="text" value="true" />
<conf_showGuiding dataType="11" default="true" inputType="text" value="true" />
<conf_systemBytes dataType="11" default="null" inputType="text" value="GBK" />
<conf_role dataType="11" default="角色" inputType="text" value="角色" />
<conf_default_datasource dataType="11" default="demo" inputType="text" value="demo" />
<conf_mda_limit dataType="11" default="1000" inputType="text" value="1000" />
<conf_showResource dataType="11" default="0" inputType="text" value="0" />
<conf_dicInherit_Status dataType="11" default="null" inputType="text" value="1" />
<conf_dbd_disppriv dataType="11" default="null" inputType="text" value="0" />
<conf_title_main dataType="11" default="null" inputType="text" value="集智数据平台" />
<conf_imageServlet dataType="11" default="null" inputType="text" value="ImageServlet,image_path,image_code,image_order" />
<conf_databaseBytes dataType="11" default="null" inputType="text" value="GBK" />
<conf_showPrintLine dataType="11" default="true" inputType="text" value="true" />
<conf_mda_beginRow dataType="11" default="0" inputType="text" value="0" />
<conf_mis2ResServlet dataType="11" default="null" inputType="text" value="Mis2ResServlet,mis2res_path,mis2res_code,mis2res_order" />
<conf_sessionExpired dataType="null" default="null" inputType="null" value="/index.jsp" />
<conf_roleInherit dataType="11" default="null" inputType="text" value="0" />
<conf_resInherit_Status dataType="11" default="null" inputType="text" value="1" />
<showDic_showDirectory dataType="11" default="null" inputType="text" value="1" />
<conf_editor dataType="11" default="null" inputType="text" value="ewebeditor" />
<conf_customRoot dataType="11" default="null" inputType="text" value="r;" />
<conf_enableDSPrivCtr dataType="11" default="1" inputType="text" value="2" />
<conf_dashBoardClass dataType="11" default="com.runqianapp.eds.dashboard.impl.DashboardAloneImpl" inputType="text" value="com.runqianapp.eds.dashboard.impl.DashboardPlatImpl" />
<conf_pathRoot dataType="11" default="null" inputType="text" value="/mis2/" />
<conf_showVerifyCode dataType="11" default="0" inputType="text" value="0" />
<conf_isgov dataType="3" default="null" inputType="radio" value="0" />
<conf_autoSaveDelay dataType="11" default="null" inputType="text" value="-1" />
<conf_template dataType="11" default="null" inputType="text" value="vertical" />
<conf_helpButtonPath dataType="11" default="null" inputType="text" value="http://www.geezn.com/documents/gez/help/148442-index.html" /> <conf_org dataType="11" default="机构" inputType="text" value="机构" /> <conf_showCommonQueryBtn dataType="11" default="true" inputType="text" value="true" /> <conf_showSource dataType="11" default="null" inputType="text" value="true" /> <conf_version dataType="1" default="null" inputType="text" value="4" /> <conf_appType dataType="11" default="1" inputType="text" value="1" /> <conf_showReportClass dataType="11" default="com.runqianapp.showReport.ShowReportGEZ" inputType="text" value="com.runqianapp.showReport.ShowReportGEZ" /> <conf_showlogo dataType="11" default="null" inputType="text" value="true" /> <conf_seryNumber dataType="11" default="10" inputType="text" value="10" /> <conf_edsFoces dataType="11" default="null" inputType="text" value="0" /> <conf_ftpCode dataType="11" default="null" inputType="text" value="GBK" /> <conf_inputnode dataType="11" default="null" inputType="text" value="input_node_vertically.jsp" /> <sys_datasourceName dataType="11" default="mis2datasource" inputType="text" value="mis2datasource" /> <conf_mda_endRow dataType="11" default="1000" inputType="text" value="1000" /> <conf_allowDrill dataType="11" default="true" inputType="text" value="true" /> <conf_cateNumber dataType="11" default="10" inputType="text" value="10" /> <conf_DataSourceChangAndDel dataType="11" default="null" inputType="text" value="true" /> <conf_expaMaxDimLength dataType="11" default="0" inputType="text" value="100" /> <conf_resInherit_define dataType="11" default="null" inputType="text" value="1" /> <conf_mouseScrollTimer dataType="11" default="true" inputType="text" value="500" /> <conf_funcBtnImage dataType="11" default="null" inputType="text" value="amend.gif" /> <datasource_Type dataType="11" default="UNKNOW" inputType="text" value="UNKNOW" /> <conf_mda_dataCreator dataType="11" default="com.runqianapp.mda.data.impls.DataSetDataCreator" inputType="text" value="com.runqianapp.mda.data.impls.DataSetDataCreator" /> <conf_logfolder dataType="11" default="null" inputType="text" value="/gez_common_lib_log" /> <conf_scheduleDBClass dataType="11" default="null" inputType="text" value="com.runqianapp.schedule.db.GezDBAccess" /> <conf_datacount dataType="11" default="null" inputType="text" value="100" /> <conf_tab dataType="11" default="null" inputType="text" value="true" /> <conf_user dataType="11" default="用户" inputType="text" value="用户" /> <conf_style dataType="11" default="null" inputType="text" value="s1" /> <conf_mda_max dataType="11" default="1000000" inputType="text" value="1000000" /> <conf_needPageMark dataType="11" default="yes" inputType="text" value="yes" /> <conf_dictionary dataType="11" default="null" inputType="text" value="/mis2/" /> <conf_loglevel dataType="11" default="null" inputType="text" value="error" /> <conf_dbdMatrixUnit dataType="11" default="10" inputType="text" value="10" /><!-- dbd面板设计页面的点阵大小配置,只有三种值10、20、30 --> </basicInfo> <logManage> <storeLogFolderName dataType="11" default="null" inputType="text" value="c:\\log" /> <logType1> <maxNum dataType="1" default="null" inputType="text" value="0" /> <entrance dataType="11" default="null" inputType="text" value="C:\\Program Files\\Tomcat 5.0.28\\bin\\runqianReport.log" /> <exportClassName>导出类名</exportClassName> <analyzeClassName>解析类名</analyzeClassName> <importClassName>导入类名</importClassName> </logType1> </logManage> </misInitConfig>