如果使用groovy缺少条目,则查找并添加内部级子节点

时间:2012-11-12 06:57:04

标签: groovy xmlnode

使用groovy,我试图读取tomcat server.xml并在节点<Resource name="jdbc/testDS" auth="Container" ..... >丢失的情况下插入新的节点资源标记。

Server.xml的层次结构为 服务器 - &gt; GlobalNamingResources - &gt;资源

以两种方式尝试: - 1)读取server.xml

  groovy.util.XmlParser parser = new groovy.util.XmlParser();
  println "tomcat.directory.path for server.xml >> " + "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server.xml"

  def root = parser.parse( "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server.xml" )
  datasourceToAdd = parser.parseText("<Resource name=\"jdbc/testDS\" auth=\"Container\" type=\"javax.sql.DataSource\" driverClassName=\"oracle.jdbc.OracleDriver\" url=\"jdbc:oracle:thin:@localhost:1521:ORA\" username=\"ORA_TEST\" password=\"*****\" factory=\"org.apache.commons.dbcp.BasicDataSourceFactory\" defaultAutoCommit=\"false\" maxActive=\"10\" maxIdle=\"5\" maxWait=\"30000\" whenExhaustedAction=\"1\" driver=\"oracle.jdbc.OracleDriver\"/>");

  def nodeName = root.'**'.findAll { it.name()== 'jdbc/testDS' };
  println "nodeName >>>>>>> " + nodeName;
  if(!root.'**'.findAll { it.name()== 'jdbc/testDS' })  
  {
    root.find { it.name() == 'GlobalNamingResources' }.children().add( 0, fragmentToAdd )

    String outxml = groovy.xml.XmlUtil.serialize( root )
    println outxml
  }

这里的问题是将数据写回server.xml是不可能的,条件也是不正确的。如果条件正确,那么需要将xml写回文件,但没有找到如何执行此操作。

2)逐行读取server.xml并开始写入文件,然后在找不到条目时插入标记。在这里,条件似乎也存在问题。

  String cr = System.getProperty( "line.separator" );
  File webXML = new File(  "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server2.xml"  )

  try
  {
    FileInputStream fstream = new FileInputStream( "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server.xml" );
    DataInputStream dataInputStream = new DataInputStream( fstream );
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( dataInputStream ) );
    String strLine;

    //Read File Line By Line
    while ( ( strLine = bufferedReader.readLine() ) != null )
    {
    println ">>>>>" + strLine;
    if(!strLine.contains("jdbc/testDS"))
    {
      webXML.append( strLine + cr );
    }
  }
  dataInputStream.close();
  AntBuilder ant = new AntBuilder()
  ant.move(file:  "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server2.xml" , tofile:  "C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server.xml" );
  }
  catch ( Exception e )
  {//Catch exception if any
    println( "Error: " + e.getMessage() );
  }

我试过的两种方法都不会给出理想的结果。知道如何实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

findAll不应该是:

root.'**'.findAll { it.name() == 'Resource' && it.@name == 'jdbc/testDS' }

所以为了让你的第一个片段更加'groovy',你最终得到:

new XmlParser().with { parser ->
  def root = parser.parse( 'C:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/server.xml' )
  String frag = '''<Resource name="jdbc/testDS"
                  |          auth="Container"
                  |          type="javax.sql.DataSource"
                  |          driverClassName="oracle.jdbc.OracleDriver"
                  |          url="jdbc:oracle:thin:@localhost:1521:ORA"
                  |          username="ORA_TEST"
                  |          password="*****"
                  |          factory="org.apache.commons.dbcp.BasicDataSourceFactory"
                  |          defaultAutoCommit="false"
                  |          maxActive="10"
                  |          maxIdle="5"
                  |          maxWait="30000"
                  |          whenExhaustedAction="1"
                  |          driver="oracle.jdbc.OracleDriver"/>'''.stripMargin()
  def fragment = parser.parseText( frag );

  if( !root.'**'.Resource.@name.find { it == 'jdbc/testDS' } ) {
    root.find { it.name() == 'GlobalNamingResources' }.children() << fragment
    String outxml = groovy.xml.XmlUtil.serialize( root )
    println outxml
  }
}