Reputation: 343
I need to split the strings from the given url and that to be stored in a property.
Eg: Url: projectname/qa/projectid/version Properties need to be store: Name=projectname Mode=qa Id=projectid Version=version
Upvotes: 1
Views: 16760
Reputation: 16235
Just to show an alternative to Rebse's script
approach, here is a more long-winded way with regular expression. You could extract each property with a block like this:
<property name="url" value="projectname/qa/projectid/version"/>
<loadresource property="Name">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="(\w+)/(\w+)/(\w+)/(\w+)" replace="\1"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="Name: ${Name}"/>
To answer your question from the comments, here is an example of how you could use that approach to extract the pieces you require. I didn't say it was pretty...
<target name="test">
<property name="url" value="http://svn.abc.com/builds/abcd/qa/FACC790C-1480-49F7-80F6-B91B07E52DA9/v1.0.1/r5532/"/>
<echo message="url: ${url}"/>
<loadresource property="a">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\1"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="a: ${a}"/>
<loadresource property="b">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\2"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="b: ${b}"/>
<loadresource property="c">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\3"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="c: ${c}"/>
<loadresource property="d">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\4"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="d: ${d}"/>
<loadresource property="e">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\5"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="e: ${e}"/>
<loadresource property="f">
<string value="${url}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="http://([^/]+)/([^/]+)/([^/]+)/(?:[^/]+)/([^/]+)/v([^/]+)/r([^/]+)/" replace="\6"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="f: ${f}"/>
</target>
Output:
test:
[echo] url: http://svn.abc.com/builds/abcd/qa/FACC790C-1480-49F7-80F6-B91B07E52DA9/v1.0.1/r5532/
[echo] a: svn.abc.com
[echo] b: builds
[echo] c: abcd
[echo] d: FACC790C-1480-49F7-80F6-B91B07E52DA9
[echo] e: 1.0.1
[echo] f: 5532
So in summary, reusing the same pattern each time, but selecting a different group (1-4). The pattern uses 6 capturing and 1 non-capturing group (for the /qa/ part). Lots of other ways you could do that.
Upvotes: 4
Reputation: 10377
Use builtin javascript engine (JDK >= 1.6.06) and ant script task :
<project>
<property name="url" value="projectname/qa/projectid/version"/>
<script language="javascript">
arr = project.getProperty('url').split('/');
project.setProperty('Name', arr[0]);
project.setProperty('Mode', arr[1]);
project.setProperty('Id', arr[2]);
project.setProperty('Version', arr[3]);
</script>
<echo>
$${Name} => ${Name}
$${Mode} => ${Mode}
$${Id} => ${Id}
$${Version} => ${Version}
</echo>
</project>
output :
[echo] ${Name} => projectname
[echo] ${Mode} => qa
[echo] ${Id} => projectid
[echo] ${Version} => version
Wrap it up in a macrodef or scriptdef for reuse (equivalent to writing a new ant task).
If you prefer using some ant addon instead of ant script task see Ant Flaka which has several possibilities for string manipulation, see manual and examples.
-- EDIT --
split works with regexp, f.e. :
<project>
<property name="url" value="Chico.Harpo.Groucho.Gummo.Zeppo"/>
<script language="javascript">
<![CDATA[
// won't work because special meaning of '.' as wildcard
// arr = project.getProperty('url').split('.');
// so either use
// masking as character class '[.]' or '\\.'
arr = project.getProperty('url').split('[.]');
for (i=0; i < arr.length; i++)
{
print(arr[i]);
}
]]>
</script>
</project>
Upvotes: 8