Showing posts with label isInList. Show all posts
Showing posts with label isInList. Show all posts

Custom Ant Task IsInList

I  had created this Custom Ant Task sometime ago while working on a project where I needed to check whether an item exists in the list. As I did not find any other efficient way to do it using any of the standard Ant tasks, I created one on my own. I'm publishing (see below GitHub project location) this Custom Ant Task source code as an Open Source. Feel free to use/modify/distribute it as per your need or suggest if you have any other better ways to do it.

What IsInList contains?
1) It contains one Java source file: com.sysgenius.tools.ant.customtask.IsInList.java

/* Copyright 2017 ppoudel@sysgenius.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sysgenius.tools.ant.customtask;
/**
* Custom Ant Task: IsInList
* Returns true if given string is in the list (delimited) of strings.
* Comparison can be regular or Java RegEx based.
* @author Purna Poudel
* Date: 2011
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
public class IsInList extends ProjectComponent implements Condition {
private String value;
private String valueList;
private String delimiter;
private boolean casesensitive = false;
private boolean isRegEx=false;
/**
* Sets the value/item.
* @param value value/item to be looked in the given list.
*/
public void setValue(String value)
{
this.value = value.trim();
}
/**
* Sets the value/item list.
* @param valueList value/item list.
*/
public void setValueList(String valueList)
{
this.valueList = valueList.trim();
}
/**
* Sets the delimiter that separates the value/items in the list.
* @param delimiter delimiter that separates the value/items in the list.
*/
public void setDelimiter(String delimiter)
{
this.delimiter = delimiter.trim();
}
/**
* Sets the case sensitivity that is used to search the item/value in the list.
* @param casesensitive true or false
*/
public void setCasesensitive(boolean casesensitive)
{
this.casesensitive = casesensitive;
}
/**
* Sets whether or not the value/item search in the list is based on regular expression
* @param isRegEx true or false
*/
public void setIsRegEx(boolean isRegEx){
this.isRegEx = isRegEx;
}
/**
* Ant eval method.
*/
public boolean eval() throws BuildException
{
boolean _isInList=false;
if (this.valueList == null) {
throw new BuildException("No list of values specified for IsInList condition");
}
if (this.value == null) {
throw new BuildException("No value specified to test IsInList condition");
}
if (!this.casesensitive)
{
this.value = this.value.toLowerCase();
this.valueList = this.valueList.toLowerCase();
}
String[] sArray = this.valueList.split(this.delimiter);
ArrayList<String> _list = new ArrayList<String>(Arrays.asList(sArray));
if(!this.isRegEx){
_isInList = _list.contains(this.value);
} else {
for(String s:_list) {
Pattern pt = Pattern.compile(s);
//System.out.println("Performing RegEx Matching using pattern: "+s+ " with value from list: "+this.value);
_isInList = pt.matcher(this.value).matches();
if(_isInList){
//System.out.println("Matched. Skipping the rest.");
break;
}
}
}
return _isInList;
}
// The following main method is just for testing purpose. It is not required in order to run as Ant task.
/*
public static void main(String[] args)
{
IsInList myList = new IsInList();
myList.setValueList("ci;Inting.*;release;SystemOut_16.01.23.log;native_stdout.*;native_stderr.*");
myList.setValue("native_stdout.log");
myList.setDelimiter(";");
myList.setCasesensitive(false);
myList.setIsRegEx(true);
System.out.println("Resullt is " + myList.eval());
}*/
}
view raw IsInList.java hosted with ❤ by GitHub

2) The GitHub project also has Ant build file build.xml to build the project from source code, sample-usage.xml - Ant build file that shows few usage scenarios of 'IsInList' task and README.txt that basically explains how to use it.

How to Use It?
Follow the steps below:

1) Make sure isinlist-<version>.jar file is in your build classpath. You can do it either by adding it into your $ANT_HOME/lib directory or by defining a custom library path like below and making a reference to it.

<path id="ant.opt.lib.path">
   <fileset dir="${basedir}/../target">
      <include name="isinlist-1.0.0.0.jar"/>
   </fileset>
</path>

2) Next, define the "isinlist" task, below is one of the few ways:

<typedef classname="com.sysgenius.tools.ant.customtask.IsInList" name="isinlist" classpathref="ant.opt.lib.path"/>

3) Use it, see the examples below:

Example 1:
You have a list of items like "ci;Inting.*;release;SystemOut_16.01.23.log;native_stdout.*;native_stderr.*" separated by ";". Here you need to find out whether or not any item starting with "native_stdout.log" exists. In this case you can do lookup using regular expression (isRegEx="true"). In your build file, you'll need to have:

<property name="item.list" value="ci;Inting.*;release;SystemOut_16.01.23.log;native_stdout.*;native_stderr.*"/>
<property name="regex.item.name" value="native_stdout.log"/>
<isinlist casesensitive="false" delimiter=";" value="${regex.item.name}" valueList="${item.list}" isRegEx="true"/>

Example 2:
You have a list of items like "ci;Inting.*;release;SystemOut_16.01.23.log;native_stdout.*;native_stderr.*" separated by ";".
Here you need to find out whether an item called "release" exists in the given list. In this case you can use regular lookup, meaning isRegEx="false".

<property name="item.list" value="ci;Inting.*;release;SystemOut_16.01.23.log;native_stdout.*;native_stderr.*"/>
<property name="regular.item.name" value="release"/>
<isinlist casesensitive="false" delimiter=";" value="${regular.item.name}" valueList="${item.list}" isRegEx="false"/>

See the sample-usage.xml for complete example and more detail usage scenarios.

You can  get/dowload files from GitHub location: https://github.com/pppoudel/customanttasks.