How to delete elements from List and Group properties in a Java step
Summary
Often you need to delete selected items from a Page List
property based on conditional code for each item. This article describes useful PublicAPI methods you can call within a Java step.
Suggested Approach
The For Each step generates Java using the Iterator object.
You cannot delete items from the Iterator object while it is running. To delete items, your code must iterate through the elements manually in Java, as illustrated below.
After you delete an element from a list, the next call to the PublicAPI method getStringValue(i) or getPageValue(i) (for Value List
and Page List
properties, respectively) will return the next element. Note also that the size() will change as you delete items.
ClipboardProperty list = myStepPage.getProperty(".pxResults");
for (int i = list.size() ; i > 0; i--)
{
// if PageList, then you can get the page and perhaps
// check a value, this can't be used if "list" is a ValueList
ClipboardPage row = list.getPageValue(i);
// test if you want to delete
if (delete)
{
list.remove(i);
}
}
To iterate through the elements of a Page Group
property without using an iterator, you need to reference the elements by name; you cannot access elements by number. To get the keys, you can loop through initially and put them into an array:
ClipboardProperty list = tools.findPage("aPage").getProperty(".aPageGroup");
java.util.Iterator itr = list.iterator();
//arrayList of elements that have to be removed.
java.util.ArrayList removeList = new java.util.ArrayList();
while(itr.hasNext())
{
ClipboardProperty row = (ClipboardProperty)itr.next() ;
if (OkToDelete ) // OkToDelete refers to some condition that checks
if it's ok to delete the current element
{
// add the subscript of the group to the arrayList
// of elements that have to be removed
removeList.add(row.getName());
}
}
int size = removeList.size();
for (int i = 0; i < size; i++)
{
list.remove(removeList.get(i).toString());
}