openamf/src/java/org/openamf/filter MapListToRecordSet.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.java.openamf.cvs
Message-ID <[email protected]>
Update of /cvsroot/openamf/openamf/src/java/org/openamf/filter
In directory sc8-pr-cvs1:/tmp/cvs-serv28527/src/java/org/openamf/filter

Added Files:
	MapListToRecordSet.java 
Log Message:
added new filter

--- NEW FILE: MapListToRecordSet.java ---
package org.openamf.filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openamf.config.FilterConfig;
import org.openamf.recordset.ASRecordSet;

public class MapListToRecordSet implements ResultFilter {

	private static Log log = LogFactory.getLog(MapListToRecordSet.class);

	public Object filter(Object value, FilterConfig config)
		throws FilterException {
		
		log.info("Translating MapListToRecordSet");
		
		ASRecordSet recordSet = null;
		List list = null;
		if (value instanceof List) {
			list = (List)value;
		} else if (value instanceof Iterator) {
			Iterator iterator = (Iterator)value;
			list = new ArrayList();
			while (iterator.hasNext()) {
				list.add(iterator.next());
			}
		}
		
		if (list != null) {
			recordSet = processList(list);
		} else {
			throw new FilterException("value is not an instance of List or Iterator");
		}
	
		return recordSet;	
	}

	private ASRecordSet processList(List list)
		throws FilterException {
		
		ASRecordSet recordSet = new ASRecordSet();	
		
		String[] columnNames = getColumnNames(list);
		list = convertList(list);
		try {	
			recordSet.populate(columnNames, list);
		} catch (Exception e) {
			throw new FilterException(e);
		}
		return recordSet;
	}
	
	private String[] getColumnNames(List list) {
		
		ArrayList names = new ArrayList();	
		Map map = (Map)list.get(0);
		for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
			Map.Entry entry = (Map.Entry)i.next();
			names.add((String)entry.getKey());
		}

		String[] columnNames =  new String[names.size()];
		int index = 0;
		for (Iterator i = names.iterator(); i.hasNext();) {
			String name = (String) i.next();
			columnNames[index] = name;
			index++;
		}
		
		return columnNames;
	}

	private List convertList(List list) {
		
		ArrayList rows = new ArrayList();
		
		for (Iterator i = list.iterator(); i.hasNext();) {
			Map map = (Map) i.next();
			rows.add(new ArrayList(map.values()));
		}
		
		return rows;
	}


}




-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.