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)
	{
		// split based on &
		String pairs[] = new StringParse(query,"&").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; }
}

@@ -84,16 +84,17 @@
 ashMap;%0A
+%0A
 /**%0A * U
@@ -869,16 +869,98 @@
 ery)%0A%09%7B%0A
+%09%09this(query, false);%0A%09%7D%0A%09public QueryString(String query, boolean keepQuotes)%0A%09%7B%0A
 %09%09// spl
@@ -1017,16 +1017,28 @@
 uery,%22&%22
+, keepQuotes
 ).getTok
@@ -2152,10 +2152,707 @@
 uery; %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

Allow to keep quotes within the string when parsing