Index of list item from ant property - list

I have a ant parameter with list of values as below.
releases=release1,release2
I use the below for loop to process the values.
<for list="${releases}" param="release">
<sequential>
<echo>Processing #{release}</echo>
</sequential>
<for>
I want to do something like, I want to echo the below statement only when the #{release}=release2
<echo>Processing last release</echo>
I am not quite sure how to get the index of the list items or if it is even possible.
Thanks in advance,
Balaji

That's all? And, you're using Ant-Contrib tasks already. Take a look at the PropertyRegEx task. That's the Ant-Contrib task that will do exactly what you want:
<propertyregex property="last.release"
input="${releases}"
regexp=".*,(.*)"
select="\1"/>
This will set ${last.release} to the last release in your string.
Be careful of strings that are null or don't match your regular expression! You can use the Ant-Contrib if task to test for that:
<propertyregex property="last.release"
input="${releases}"
regexp=".*,(.*)"
select="\1"/>
<if>
<isset property="last.release"/>
<then>
<echo>The last release is ${last.release}</echo>
</then>
<else>
<fail>Oops! Something didn't work...</fail>
</else>
</if>

Related

wso2 dataservice filter null for a dataservice field

in my Dataservice ,in a select statement one of the field has a null value.
It is returned like this
<ROLLNUMBER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
I want to write a filter and do some logic based on if the value is null or it has some value.
How can do that in WSO2 ESB?
I tried a few XSL expression nothing works
You need to use a filter mediator wherein you need to use xpath expression as //*[local-name()='ROLL_NUMBER']/text(), what this will do is for the element ROLL_NUMBER if there is a value only then the filter condition is satisfied and it goes to then condition if not then it will go to else condition
Try the following
<filter source="boolean(get-property('yourProperty'))" regex="false">
<then> <!-- NULL OR NON EXIST --> </then>
<else> <!-- EXIST --> </else>
</filter>

ANT target if, then, else if, else

I am so very green and new to ANT and have been searching online for an example similar to this to compare for validity but cannot find anything. Thanks in advance for you input.
Can you tell me if it is valid to append _old to arg1?
<for list="${file1.list}" param="file1.property">
<sequential>
<if>
<equals arg1="${#{file1.property}_old}" arg2="${#{file1.property}}"/>
<then>
<!--<echo message="Property #{file1.property} has no change "/>-->
</then>

Logback filter by regular expression not working

I am trying to get regex-based filtering to work with logback but fail to do so. Based on the example on the logback website, here's the respective part of my logback config but all log messages seem to be filtered out by it:
<appender name="__CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level|%d{HH:mm:ss}] %logger{15} %msg [%thread] %n</pattern>
</encoder>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<matcher>
<Name>custom</Name>
<regex>.*foobar.*</regex>
</matcher>
<expression>custom.matches(formattedMessage)</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>NEUTRAL</OnMatch>
</filter>
</appender>
When I comment out the filter part, log statements matching the regex are displayed. I must be missing something obvious.
Sorry for the noise. The runtime-dependency to janino was simply missing in my setup (see How do I not log a particular type of Exception in Logback?).

Regex to match everything before a word. To be used in nant script

I was writing some build scripts for my project. I wanted a regex pattern which can match everything before a particular word. For eg: My script looks like this
Create Table ABC(
id int(50)
)
--//#UNDO
Drop table ABC
I want to match everything before --//#UNDO using nant regex task. How do I implement it??
I also want it to match everything in the file if --//#UNDO is not present in the file. I am not getting a way around
This is the pattern:
(?'str'.*?)--//#UNDO
The result will be in str.
If you just want to match the text before the string (and not the string itself) you will need to use a lookahead.
.*?(?=--//#UNDO)
Needing to specify Singleline still applies.
This would be the NAnt target:
<target name="go">
<loadfile
file="C:\foo\bar.sql"
property="content" />
<regex
pattern="(?'content'.*)--//#UNDO"
input="${content}"
options="Singleline"
failonerror="false" />
<echo message="${content}" />
</target>
Notice that property content is preset with the complete file content in case that --//#UNDO is not present in file.
This is what I ended up doing
<loadfile file="${filePathAndName}" property="file.contents" />
<property name="fileHasUndo" value="${string::index-of(file.contents, '--//#UNDO')}" />
<choose>
<when test="${fileHasUndo == '-1' }">
<echo file="${file}" append="true" message="${file.contents}" />
</when>
<otherwise>
<regex pattern="(?'sql'[\s\S]*)--\/\/#UNDO[\s\S]*" input="${file.contents}" options="Multiline"/>
<echo file="${file}" append="true" message="${sql}" />
</otherwise>
</choose>
I found the index of --//#UNDO. And depending on its presence I am doing a choose when.. Solved the problem

Set Ant property based on a regular expression in a file

I have the following in a file
version: [0,1,0]
and I would like to set an Ant property to the string value 0.1.0.
The regular expression is
version:[[:space:]]\[([[:digit:]]),([[:digit:]]),([[:digit:]])\]
and I need to then set the property to
\1.\2.\3
to get
0.1.0
I can't workout how to use the Ant tasks together to do this.
I have Ant-contrib so can use those tasks.
Based on matt's second solution, this worked for me for any (text) file, one line or not. It has no apache-contrib dependencies.
<loadfile property="version" srcfile="version.txt">
<filterchain>
<linecontainsregexp>
<regexp pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"/>
</linecontainsregexp>
<replaceregex pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
</filterchain>
</loadfile>
Solved it with this:
<loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/>
<propertyregex property="burning-boots-js-lib-build.lib-version"
override="true"
input="${burning-boots-js-lib-build.lib-version}"
regexp="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"
select="\1.\2.\3" />
But it seems a little wasteful - it loads the whole file into a property!
If anyone has any better suggestions please post :)
Here's a way that doesn't use ant-contrib, using loadproperties and a filterchain (note that replaceregex is a "string filter" - see the tokenfilter docs - and not the replaceregexp task):
<loadproperties srcFile="version.txt">
<filterchain>
<replaceregex pattern="\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
</filterchain>
</loadproperties>
Note the regex is a bit different, we're treating the file as a property file.
Alternatively you could use loadfile with a filterchain, for instance if the file you wanted to load from wasn't in properties format.
For example, if the file contents were just [0,1,0] and you wanted to set the version property to 0.1.0, you could do something like:
<loadfile srcFile="version.txt" property="version">
<filterchain>
<replaceregex pattern="\s+\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
</filterchain>
</loadfile>