美文网首页
xml 配置文件注释和取消注释

xml 配置文件注释和取消注释

作者: Canon_2020 | 来源:发表于2020-04-20 09:45 被阅读0次

使用 comment_xml.py 对 spring-task.xml 进行注释和取消注释,及更改 value 节点的触发时间值

spring-task.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--################################################# Start JOB  #################################################-->

    <!-- 测试 job1 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoUpdateMerCheckIPBean">
        <property name="jobClass" value="com.lot.autocheck.AutoUpdateMerCheckIPDataJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job2 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoMerContractDateBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckMerContractDateTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job3 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoAE3DCheckBean">
        <property name="jobClass" value="com.lot.autocheck.AutoAE3DCheckTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job4 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoCheckBankCodeBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckBankCodeTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean> 

    <!-- 测试 job5 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoCheckFdmsBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckFdmsTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job6 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoECPBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckECPTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>
    
    <!-- 测试 job7 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoCheckWxBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckTenpayCnTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job8 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoCheckAlipayBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckAlipayTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>

    <!-- 测试 job9 -->
    <bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" name="autoCheckMonthlyTradeBean">
        <property name="jobClass" value="com.lot.autocheck.AutoCheckMonthlyTradeTaskJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="autoCheckService" value-ref="autoCheckService"/>
            </map>
        </property>
    </bean>
    
    <!--################################################# End JOB #################################################-->

    <!--################################################# Start Trigger #################################################-->

    <!-- 测试1 -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoUpdateMERCheckIpTriggerBean">
        <property name="jobDetail">
            <ref bean="autoUpdateMerCheckIPBean"/>
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ? *</value>
        </property>
    </bean>

    <!-- 测试2  -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoMerContractDateTriggerBean">
        <property name="jobDetail">
            <ref bean="autoMerContractDateBean"/>
        </property>
        <property name="cronExpression">
            <value>0 0 9 * * ? *</value>
        </property>
    </bean>

    <!-- 测试3  -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoAE3DTriggerBean">
        <property name="jobDetail">
            <ref bean="autoAE3DCheckBean"/>
        </property>
        <!-- <property name="cronExpression">
            <value>0 0/60 * * * ? *</value>
        </property> -->
        <property name="cronExpression">
            <value>0 0/1 * * * ? *</value>
        </property>
    </bean>

    <!-- 测试4 -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckBankCodeTriggerBean">
        <property name="jobDetail">
            <ref bean="autoCheckBankCodeBean"/>
        </property>
        <property name="cronExpression">
            <value>0 0/10 * * * ? *</value>
        </property>
    </bean>

    <!-- 测试5 -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckFdmsTriggerBean">
        <property name="jobDetail">
            <ref bean="autoCheckFdmsBean"/>
        </property>
        <property name="cronExpression">
            <value>52 0/4 * * * ? *</value>
        </property>
    </bean>

    <!-- 测试6-->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckECPTriggerBean">
        <property name="jobDetail">
            <ref bean="autoECPBean"/>
        </property>
        <property name="cronExpression">
            <value>16 0/10 * * * ? *</value>
        </property>
    </bean>

    <!-- 测试7
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckWxTriggerBean">
        <property name="jobDetail">
            <ref bean="autoCheckWxBean"/>
        </property>
        <property name="cronExpression">
            <value>53 0/11 * * * ? *</value>
        </property>
    </bean>-->

    <!-- 测试8 -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckAlipayTriggerBean">
        <property name="jobDetail">
            <ref bean="autoCheckAlipayBean"/>
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ? *</value>
        </property>
    </bean>
    
    <!-- 测试9 -->
    <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" id="autoCheckTradeTriggerBean">
        <property name="jobDetail">
            <ref bean="autoCheckMonthlyTradeBean"/>
        </property>
        <property name="cronExpression">
            <value>0 0/3 * * * ? *</value>
        </property>
    </bean>
    
    <!--################################################# End Trigger #################################################-->

    <!--################################################# Start Spring ScheduleFactory工厂#################################################-->
        
    <!-- <bean id="schedulerFactoryBean1"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoUpdateMERCheckIpTriggerBean" />
            </list>
        </property>
        <property name="startupDelay" value="2" />
    </bean> -->
    
    <!-- <bean id="schedulerFactoryBean2"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoMerContractDateTriggerBean" />
            </list>
        </property>
    </bean>
    <bean id="schedulerFactoryBean3"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoAE3DTriggerBean" />
            </list>
        </property>
    </bean>
    
    <bean id="schedulerFactoryBean4"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoCheckBankCodeTriggerBean" />
            </list>
        </property>
    </bean>
    
    
        <bean id="schedulerFactoryBean5"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoCheckFdmsTriggerBean" />
            </list>
        </property>
        <property name="startupDelay" value="2" />
    </bean> 
    
        <bean id="schedulerFactoryBean6"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="autoCheckECPTriggerBean" />
            </list>
        </property>
        <property name="startupDelay" value="2" />
    </bean>
    -->

      <!--<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="schedulerFactoryBean7">
        <property name="triggers">
            <list>
                <ref bean="autoCheckWxTriggerBean"/>
            </list>
        </property>
        <property name="startupDelay" value="1"/>
    </bean>-->
    
        <!--<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="schedulerFactoryBean8">
        <property name="triggers">
            <list>
                <ref bean="autoCheckAlipayTriggerBean"/>
            </list>
        </property>
        <property name="startupDelay" value="2"/>
    </bean>--> 
    
        <!--<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="schedulerFactoryBean9">
        <property name="triggers">
            <list>
                <ref bean="autoCheckTradeTriggerBean"/>
            </list>
        </property>
    </bean>-->
    
    <!--################################################# End Spring ScheduleFactory #################################################-->

    <!-- quartz中 Cron 表达式中常用的示例写法 -->
    <!--0 0 12 * * ?           每天12:00执行触发  -->
    <!--0 15 10 ? * *          每天10:15执行触发 -->
    <!--0 15 10 * * ?          每天10:15执行触发 -->
    <!--0 15 10 * * ? *        每天10:15执行触发-->
    <!--0 15 10 * * ? 2005     2005年的每天10:15执行触发 -->
    <!--0 * 14 * * ?           每天从14:00到14:59每隔1分钟执行一次触发 -->
    <!--0 0/5 14 * * ?         每天从14:00到14:59每个5分钟执行一次触发 -->
    <!--0 0/5 14,18 * * ?      每天从14:00到14:59和18:00到18:59每隔5分钟执行一次触发 -->
    <!--0 0-5 14 * * ?         每天从14:00到14:05每隔1分钟执行一次触发 -->
    <!--0 10,44 14 ? 3 WED     3月的每个星期3的14:10和14:44分别执行一次触发 -->
    <!--0 15 10 15 * ?         每月的第15日10:15执行一次触发-->
    <!--0 15 10 L * ?          每月最后一天的10:15执行一次触发 -->
    <!--0 15 10 ? * 6L         每月的最后一个星期5的10:15执行一次触发-->
    <!--
10 ? * 2002-2005 2002、2003、2004、2005年的每个月的最后一个星期5的10:15执行一次触发
    -->
    <!--
10 ? * 6#3 每月的第3个星期5的10:15执行一次触发 这里定义每天12点30分的时候程序执行一次
    -->
</beans>

comment_xml.py

#!/usr/local/python3
# -*- coding: utf-8 -*-
# @Date: 2018-04-15 09:00:00
# @Author: Canon
# @Link: https://www.python.org
# @Version: 3.6.1

"""
注释对账的 spring-task.xml 文件

# 注释最后一个标签
# comment_node(doc.getElementsByTagName('bean')[-1])

"""

from xml.dom import minidom
import re
import os

# Spring 定时任务文件
spring_file = os.path.dirname(os.path.abspath(__file__)) + '/spring-task.xml'

# 执行的定时任务
spring_task = "schedulerFactoryBean48"

# 定时任务触发时间
spring_time = "0 0/1 * * * ? *".strip()

# 属性值
spring_attri = "schedulerFactoryBean"


def uncomment_node(doc, xml_file):
    """
    取消注释 xml 代码
    """
    def clear_val(node_val):
        label_list = re.findall(">.*?<", node_val, flags=re.DOTALL)
        for item in label_list:
            if '*' not in item:
                node_val = re.subn(re.sub(">|<|\n|\t| ", "", item), '', node_val)[0]
        return node_val
    new_doc = ""
    for child in doc.childNodes:
        # 判断节点是否注释
        if child.nodeType == child.COMMENT_NODE:
            # 判断节点是否包含标签的注释内容
            re_list = [x for x in re.findall('<.*|>|.*=".*"', child.data, flags=re.DOTALL) if x != '']
            if re_list:
                # 标签的第一个 "<" 之前是否存在内容
                if "<" in child.data:
                    comment_val = child.data.split("<")[0].replace(" ", "")
                else:
                    comment_val = False
                # 标签的最后一个 ">" 之后是否存在内容
                if ">" in child.data:
                    comment_end = re.findall("(?<=>)(?![\w\W]*?>)[\w\W]*", child.data, flags=re.DOTALL)[0].replace(" ", "")
                else:
                    comment_end = False
                # 判断注释内容是否包含除了标签外的其他内容
                if comment_val:
                    # 标签之外的内容
                    start_val = re.sub("<|\n|\t| ", "", comment_val.strip())
                    if comment_end:
                        if start_val:
                            new_doc += "<!--" + start_val + "-->\n\t"
                        new_doc += clear_val(re.findall("<.*>", child.data, flags=re.DOTALL)[0])
                        end_val = re.sub(">|\n|\t| ", "", comment_end.strip())
                        if end_val:
                            new_doc += "\n\t<!--" + end_val + "-->"
                    else:
                        if start_val:
                            new_doc += "<!--" + start_val + "-->\n\t"
                        new_doc += clear_val(re.findall("<.*", child.data, flags=re.DOTALL)[0])
                else:
                    # 标签的最后一个 ">" 之后是否存在内容
                    if comment_end:
                        end_val = re.sub(">|\n|\t| ", "", comment_end.strip())
                        new_doc += clear_val(re.findall(".*>", child.data, flags=re.DOTALL)[0])
                        if end_val:
                            new_doc += "\n\t<!--" + end_val + "-->"
                    else:
                        new_doc += clear_val(child.nodeValue)
            else:
                new_doc += "<!--" + child.nodeValue + "-->"
        else:
            new_doc += child.toxml()
    tag_top = ('<beans xmlns="http://www.springframework.org/schema/beans" '
                + 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
                + 'xsi:schemaLocation="http://www.springframework.org/schema/beans        '
                + 'http://www.springframework.org/schema/beans/spring-beans.xsd">')
    new_xml = tag_top + new_doc + "</beans>"
    with open(xml_file, 'w', encoding="utf-8") as w_file:
        w_file.write(new_xml)


def comment_task(doc, xml_file, task_list, attri_flag, task_time):

    def comment_node(node_obj):
        node_val = node_obj.toxml()
        # 注释节点
        if '--' in node_val:
            # 删除节点中的注释
            re_val = re.subn(r'<!--.*?-->', '', node_val, flags=re.DOTALL)[0]
            comment = node_obj.ownerDocument.createComment(re_val)
            node_obj.parentNode.replaceChild(comment, node_obj)
        else:
            comment = node_obj.ownerDocument.createComment(node_val)
            node_obj.parentNode.replaceChild(comment, node_obj)

    str_val = doc.getElementsByTagName('bean')
    time_conf = []
    task_conf = []
    class_conf = []
    id_list = []
    name_list = []
    # 遍历所有 bean 节点
    for i in range(len(str_val)):
        node = str_val[i]
        node_id = node.getAttribute("id")
        if node_id:
            if attri_flag in node_id:
                # 定时任务列表
                task_conf.append(node)
            else:
                # 定时任务的触发时间列表
                time_conf.append(node)
        else:
            # 加载类列表
            class_conf.append(node)

    # 需要执行的定时任务
    for task_node in task_conf:
        if task_node.getAttribute("id") not in task_list:
            # 注释第三部分 Start Spring ScheduleFactory 定时任务
            comment_node(task_node)
        else:
            # 第二部分 Trigger 定时触发器的 id 列表
            time_id = task_node.getElementsByTagName('ref')[0].getAttribute("bean")
            id_list.append(time_id)

    # 改变定时任务的触发时间
    for time_node in time_conf:
        if time_node.getAttribute("id") in id_list:
            # 执行的任务改变触发时间
            re_val = re.subn('<value>.*?(?<=</value>)(?![\w\W]*?</value>)', '<value>{0}</value>'.format(task_time), time_node.toxml(), flags=re.DOTALL)[0]
            if '--' in re_val:
                re_val = re.sub("</value>.*-->", "</value>", re_val)
                re_val = re.sub("<!--.*<value>", "<value>", re_val)
            # 创建节点,写入文件会带有 "&lt;"、"&gt;"、"&quot;" 字符, 需要替换为正常字符
            comment = time_node.ownerDocument.createTextNode(re_val)
            time_node.parentNode.replaceChild(comment, time_node)
        else:
            # 注释第二部分 Start Trigger 定时触发器
            comment_node(time_node)
            # 第一部分 job 类的 name 列表
            class_name = time_node.getElementsByTagName('ref')[0].getAttribute("bean")
            name_list.append(class_name)

    # 注释不需要的类
    for class_node in class_conf:
        if class_node.getAttribute("name") in name_list:
            # 注释第一部分 Start JOB 类
            comment_node(class_node)

    xml_val = doc.toxml()
    xml_val = re.sub("&lt;", '<', xml_val)
    xml_val = re.sub("&gt;", '>', xml_val)
    xml_val = re.sub("&quot;", '"', xml_val)
    # print(xml_val)
    with open(xml_file, 'w', encoding="utf-8") as w_file:
        w_file.write(xml_val)


if __name__ == '__main__':
    # 取消注释
    doc = minidom.parse(spring_file).documentElement
    uncomment_node(doc, spring_file)
    # 开启定时任务,改变触发时间
    doc = minidom.parse(spring_file).documentElement
    comment_task(doc, spring_file, spring_task.split(","), spring_attri, spring_time)

相关文章

网友评论

      本文标题:xml 配置文件注释和取消注释

      本文链接:https://www.haomeiwen.com/subject/vsevkqtx.html