2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.util;
|
2010-02-01 21:57:52 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
2010-03-04 20:15:46 +01:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2010-03-08 03:34:25 +01:00
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2010-02-01 21:57:52 +01:00
|
|
|
|
2010-02-05 20:19:38 +01:00
|
|
|
import org.apache.commons.codec.DecoderException;
|
2010-02-01 21:57:52 +01:00
|
|
|
import org.apache.commons.codec.net.URLCodec;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
|
|
|
|
|
|
|
public class ParsingUtilities {
|
2010-03-04 20:38:23 +01:00
|
|
|
static public SimpleDateFormat s_sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
2010-03-04 20:15:46 +01:00
|
|
|
|
2010-03-08 03:34:25 +01:00
|
|
|
static public Properties parseUrlParameters(HttpServletRequest request) {
|
|
|
|
Properties options = new Properties();
|
|
|
|
|
|
|
|
String query = request.getQueryString();
|
|
|
|
if (query != null) {
|
|
|
|
if (query.startsWith("?")) {
|
|
|
|
query = query.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] pairs = query.split("&");
|
|
|
|
for (String pairString : pairs) {
|
|
|
|
int equal = pairString.indexOf('=');
|
|
|
|
String name = equal >= 0 ? pairString.substring(0, equal) : "";
|
|
|
|
String value = equal >= 0 ? ParsingUtilities.decode(pairString.substring(equal + 1)) : "";
|
|
|
|
|
|
|
|
options.put(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
static public String inputStreamToString(InputStream is) throws IOException {
|
|
|
|
Reader reader = new InputStreamReader(is, "UTF-8");
|
|
|
|
try {
|
|
|
|
return readerToString(reader);
|
|
|
|
} finally {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 21:57:52 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
static public String readerToString(Reader reader) throws IOException {
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
char[] chars = new char[8192];
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = reader.read(chars)) > 0) {
|
|
|
|
sb.insert(sb.length(), chars, 0, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2010-02-01 21:57:52 +01:00
|
|
|
|
|
|
|
static public JSONObject evaluateJsonStringToObject(String s) throws JSONException {
|
2010-03-03 05:19:58 +01:00
|
|
|
JSONTokener t = new JSONTokener(s);
|
|
|
|
JSONObject o = (JSONObject) t.nextValue();
|
|
|
|
return o;
|
2010-02-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static public JSONArray evaluateJsonStringToArray(String s) throws JSONException {
|
2010-03-03 05:19:58 +01:00
|
|
|
JSONTokener t = new JSONTokener(s);
|
|
|
|
JSONArray a = (JSONArray) t.nextValue();
|
|
|
|
return a;
|
2010-02-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final URLCodec codec = new URLCodec();
|
|
|
|
static public String encode(String s) {
|
|
|
|
try {
|
2010-03-03 05:19:58 +01:00
|
|
|
return codec.encode(s, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
return s; // should not happen
|
|
|
|
}
|
2010-02-01 21:57:52 +01:00
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
static public String decode(String s) {
|
|
|
|
try {
|
2010-03-03 05:19:58 +01:00
|
|
|
return codec.decode(s, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
return s; // should not happen
|
|
|
|
} catch (DecoderException e) {
|
|
|
|
return s; // should not happen
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
}
|
2010-03-04 20:15:46 +01:00
|
|
|
|
|
|
|
static public String dateToString(Date d) {
|
|
|
|
return s_sdf.format(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Date stringToDate(String s) {
|
|
|
|
try {
|
|
|
|
return s_sdf.parse(s);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 21:57:52 +01:00
|
|
|
}
|