package com.powerdata.openpa.tools;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Utiltiy to parse query strings.
 * 
 * A query string has the form: field=value&field2=value ...
 * 
 * The field names can be repeated.  Sometimes the order of the fields is
 * important, other times only the values matter.  This utility allows
 * different approaches depending on which of these is important.
 * 
 * The one kludge is that in most cases there will be exactly one value
 * per field, however, since more are possible it is not enough to simply
 * return a String value for a field name, as a result the caller is
 * forced to check the return value for null and check the count.
 * 
 * @author marck
 *
 */
public class QueryString
{
	String _query[][];
	HashMap<String,String[]> _fields = new HashMap<String,String[]>();
	public QueryString(String query)
	{
		this(query, false);
	}
	public QueryString(String query, boolean keepQuotes)
	{
		// split based on &
		String pairs[] = new StringParse(query,"&", keepQuotes).getTokens();
		// split based on = and index them
		_query = new String[pairs.length][];
		HashMap<String,ArrayList<String>> fields = new HashMap<String,ArrayList<String>>();
		for(int i=0; i<pairs.length; i++)
		{
			_query[i] = new StringParse(pairs[i],"=").getTokens();
			ArrayList<String> vals = fields.get(_query[i][0]);
			if (vals == null)
			{
				vals = new ArrayList<String>();
				fields.put(_query[i][0], vals);
			}
			vals.add(_query[i][1]);
		}
		// convert the ArrayList to String[]
		for(String k : fields.keySet())
		{
			_fields.put(k, fields.get(k).toArray(new String[0]));
		}
	}
	public int count() { return _query.length; }
	public String[] get(int i) { return _query[i]; }
	public String getField(int i) { return _query[i][0]; }
	public String getVal(int i) { return _query[i][1]; }
	public String[] get(String field) { return _fields.get(field); }
	public boolean containsKey(String field) { return _fields.containsKey(field); }
	public int getFieldCount() { return _fields.size(); }
	public HashMap<String,String[]> getMap() { return _fields; }
	public String[][] getQuery() { return _query; }
	
	public static void main(String args[])
	{
		String str = "db=/home/faby/git/usrmgmt/usermgmt.pddb&cdb=/home/faby/git/usrmgmt/config.pddb&pddef=/home/faby/git/psm/src/com/powerdata/security/pd2/security.pddef&usersview=\"view:group=PDSecurityDB&name=Users\"&credentialsview=\"view:group=PDSecurityDB&name=Credential\"";
		QueryString q = new QueryString(str, true);
		if(!q.containsKey("usersview")) 
		{
			System.out.println("QueryString: doesnt contain usersview");
			System.exit(0);
		}
		String v = q.get("usersview")[0];
		System.out.println("Usersview ["+v+"]");
		
		// test
		for(int i = 0; i < q.count(); i++)
		{
			System.out.println(i+") "+q.getField(i)+" : "+q.getVal(i));
		}
	}
}

@@ -2156,703 +2156,6 @@
 ; %7D%0A
-%09%0A%09public static void main(String args%5B%5D)%0A%09%7B%0A%09%09String str = %22db=/home/faby/git/usrmgmt/usermgmt.pddb&cdb=/home/faby/git/usrmgmt/config.pddb&pddef=/home/faby/git/psm/src/com/powerdata/security/pd2/security.pddef&usersview=%5C%22view:group=PDSecurityDB&name=Users%5C%22&credentialsview=%5C%22view:group=PDSecurityDB&name=Credential%5C%22%22;%0A%09%09QueryString q = new QueryString(str, true);%0A%09%09if(!q.containsKey(%22usersview%22)) %0A%09%09%7B%0A%09%09%09System.out.println(%22QueryString: doesnt contain usersview%22);%0A%09%09%09System.exit(0);%0A%09%09%7D%0A%09%09String v = q.get(%22usersview%22)%5B0%5D;%0A%09%09System.out.println(%22Usersview %5B%22+v+%22%5D%22);%0A%09%09%0A%09%09// test%0A%09%09for(int i = 0; i %3C q.count(); i++)%0A%09%09%7B%0A%09%09%09System.out.println(i+%22) %22+q.getField(i)+%22 : %22+q.getVal(i));%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Remove main function