1

In the below ant script snippet, I am processing all the conn.xml files in a directory to find out if there is a DB entry other than MyDB. This code only sets the DB name of the last match which is still okay as there is only one such file normally. But I want to name the exact file (out of those all included files) which has this invalid entry.

<xmltask>
   <fileset dir="${srcdir}/../apps" includes="*/conn.xml"/>
   <copy path="//Reference[@name!='MyDB']/@name" attrvalue="true" property="bad_connection_name"/>
</xmltask>

What parameter I can use in "path" field of copy command which will print the current file name as well?

dganesh2002
  • 1,917
  • 1
  • 26
  • 29

2 Answers2

2

The path attribute of xmltask copy holds only the XPath reference of the element(s) to copy.
If you want to catch all matches you need to set attribute append for xmltask copy,
see xmltask manual :

when set to true, appends to the given buffer or property. You can only append when creating a new property since Ant properties are immutable (i.e. when an XPath resolves to multiple text nodes)

<copy path="//Reference[@name!='MyDB']/@name" attrvalue="true" property="bad_connection_name" append="true"/>

you may also set a propertySeparator, default=','

But a much simpler way to get all files with bad connection string is using a fileset with selector like that :

<fileset dir="${srcdir}/../apps" includes="*/conn.xml" id="foo">
 <contains text=" your bad Connection string goes here "/>
</fileset>

<!-- simple echo -->
<echo>${toString:foo}</echo>

<!-- convert to one file one line -->
<pathconvert refid="foo" pathsep="${line.separator}" property="foobar"/>
<!-- echo to ant logger/stdout -->
<echo>${foobar}</echo>
<!-- write to file -->
<echo file="path/to/badconnection.txt">${foobar}</echo>

if bad connection string is not static, use containsregexp selector instead of contains.

Rebse
  • 10,307
  • 2
  • 38
  • 66
  • Let me try this.. actually there are two text checks I am doing in the real issue but to keep issue simple I added just one check for posting it here. Its like - > so not sure if it will work with fileset, not option. – dganesh2002 Aug 28 '15 at 19:12
  • Anyways, I was able to nail down the fix last night using foreach.. See my answer – dganesh2002 Aug 28 '15 at 19:15
  • @dganesh2002 the negated - means using ...- selector was wrong as you are looking for the files with bad connection string - has been corrected in my edit. Also you may combine multiple selectors with ... and ... or combinations of both. – Rebse Aug 28 '15 at 21:44
  • I need that and condition to look on the same line because there could be other DB name with different className which is valid. I believe thats only possible using xmltask. – dganesh2002 Aug 29 '15 at 01:50
1
<target name="check_connection_violations">
    <xmltask source="${file}">
        <copy path="//Reference[@className='oracle.jdeveloper.db.adapter.DatabaseProvider' and @name!='MyDB']/@name" attrvalue="true" property="bad_connection_name"/>
    </xmltask>
    <if>
        <isset property="bad_connection_name"/>
    <then>
        <echo message="${file} has connection violation due to ${bad_connection_name} entry. ${line.separator}" file="${basedir}/conn_name_violation.txt" append="true"/>
    </then>
    </if>
</target>

The above one is newly added target. Here is the original snippet which calls it in a loop :

<foreach target="check_connection_violations" param="file">
    <fileset dir="${srcdir}/../apps" includes="*/conn.xml"/>
</foreach>
dganesh2002
  • 1,917
  • 1
  • 26
  • 29
  • When I did this, I doubted it will work because bad_connection_name property is already set and will not work in a loop. But it seems xmltask unsets the property every time which just works perfectly for me. – dganesh2002 Aug 28 '15 at 19:23