2013-10-14 44 views
0

我被以下問題阻止:如何測試xml參數的值

我有一個文件web.xml,其中包含多個信息,標籤和過濾器。

我想在參數名的過濾器名的「後衛」的情況下,停止處理「AdministratorGuardFilter」有

web.xml文件中格式化這樣的帕拉姆值=「真」:

<filter> 
<filter-name>Character encoding filter</filter-name> 
<filter-class>ergonomics.filter.CharacterEncodingFilter</filter-class> 
</filter> 

<filter> 
<filter-name>AdministratorGuardFilter</filter-name> 
<filter-class>security.common.AdministratorGuard</filter-class> 
<init-param> 
    <param-name>guard</param-name> 
    <param-value>true</param-value> 
</init-param> 
</filter>  

<filter-mapping>  
<filter-name>AdministratorGuardFilter</filter-name>  
<url-pattern>/administrator/*</url-pattern>  

任何想法?

非常感謝您的幫助或提示

迪迪埃。

回答

1

以下Ant腳本使用第三方<xmltask><xmltask>可以將由XPath表達式選擇的XML節點映射到Ant屬性。

<project name="ant-xmltask-web-xml-extract" default="run" basedir="."> 
    <!-- The elements of a web.xml file are in an XML namespace. To have --> 
    <!-- <xmltask> ignore namespaces, we put a ':' in front of each element --> 
    <!-- name. ':' tells the XPath mechanism to use the local name of the --> 
    <!-- element. --> 
    <property 
     name="xpath.expression" 
     value="/:web-app/:filter[:filter-name/text() = 'AdministratorGuardFilter']/:init-param[:param-name/text() = 'guard']/:param-value/text()" 
    /> 

    <target name="run"> 
     <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" /> 
     <xmltask> 
      <fileset file="web.xml"/> 
      <copy path="${xpath.expression}" property="guard.value"/> 
     </xmltask> 

     <fail message="guard param for AdministratorGuardFilter is true."> 
      <condition> 
       <equals arg1="${guard.value}" arg2="true"/> 
      </condition> 
     </fail> 
    </target> 
</project>