2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.model;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.io.Serializable;
|
2010-03-06 08:43:45 +01:00
|
|
|
import java.io.Writer;
|
2010-03-05 03:25:27 +01:00
|
|
|
import java.util.Calendar;
|
2010-03-05 09:11:48 +01:00
|
|
|
import java.util.Date;
|
2010-04-09 21:32:50 +02:00
|
|
|
import java.util.Map;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-04-15 02:09:14 +02:00
|
|
|
import org.codehaus.jackson.JsonFactory;
|
2010-04-14 01:57:03 +02:00
|
|
|
import org.codehaus.jackson.JsonParser;
|
|
|
|
import org.codehaus.jackson.JsonToken;
|
2010-01-25 23:51:25 +01:00
|
|
|
import org.json.JSONException;
|
2010-03-06 08:43:45 +01:00
|
|
|
import org.json.JSONObject;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
2010-03-01 01:21:13 +01:00
|
|
|
import com.metaweb.gridworks.expr.EvalError;
|
|
|
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.expr.HasFields;
|
2010-03-05 03:25:27 +01:00
|
|
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
public class Cell implements HasFields, Jsonizable {
|
2010-03-04 20:59:31 +01:00
|
|
|
final public Serializable value;
|
|
|
|
final public Recon recon;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-04 20:59:31 +01:00
|
|
|
public Cell(Serializable value, Recon recon) {
|
2010-03-03 05:19:58 +01:00
|
|
|
this.value = value;
|
|
|
|
this.recon = recon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
if ("value".equals(name)) {
|
|
|
|
return value;
|
|
|
|
} else if ("recon".equals(name)) {
|
|
|
|
return recon;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-06 07:35:48 +02:00
|
|
|
|
|
|
|
public boolean fieldAlsoHasFields(String name) {
|
|
|
|
return "recon".equals(name);
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void write(JSONWriter writer, Properties options) throws JSONException {
|
|
|
|
writer.object();
|
|
|
|
if (ExpressionUtils.isError(value)) {
|
2010-03-01 01:21:13 +01:00
|
|
|
writer.key("e");
|
|
|
|
writer.value(((EvalError) value).message);
|
2010-03-03 05:19:58 +01:00
|
|
|
} else {
|
|
|
|
writer.key("v");
|
2010-03-05 09:11:48 +01:00
|
|
|
if (value != null) {
|
|
|
|
if (value instanceof Calendar) {
|
|
|
|
writer.value(ParsingUtilities.dateToString(((Calendar) value).getTime()));
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.key("t"); writer.value("date");
|
2010-03-05 09:11:48 +01:00
|
|
|
} else if (value instanceof Date) {
|
|
|
|
writer.value(ParsingUtilities.dateToString((Date) value));
|
2010-03-06 08:43:45 +01:00
|
|
|
writer.key("t"); writer.value("date");
|
2010-03-05 09:11:48 +01:00
|
|
|
} else {
|
|
|
|
writer.value(value);
|
|
|
|
}
|
2010-03-05 03:25:27 +01:00
|
|
|
} else {
|
2010-03-05 09:11:48 +01:00
|
|
|
writer.value(null);
|
2010-03-05 03:25:27 +01:00
|
|
|
}
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (recon != null) {
|
|
|
|
writer.key("r");
|
|
|
|
recon.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-03-06 08:43:45 +01:00
|
|
|
|
|
|
|
public void save(Writer writer, Properties options) {
|
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
try {
|
|
|
|
write(jsonWriter, options);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 21:32:50 +02:00
|
|
|
static public Cell load(String s, Map<Long, Recon> reconCache) throws Exception {
|
|
|
|
return s.length() == 0 ? null : load(ParsingUtilities.evaluateJsonStringToObject(s), reconCache);
|
2010-03-06 08:43:45 +01:00
|
|
|
}
|
|
|
|
|
2010-04-09 21:32:50 +02:00
|
|
|
static public Cell load(JSONObject obj, Map<Long, Recon> reconCache) throws Exception {
|
2010-03-06 08:43:45 +01:00
|
|
|
Serializable value = null;
|
|
|
|
Recon recon = null;
|
|
|
|
|
|
|
|
if (obj.has("e")) {
|
|
|
|
value = new EvalError(obj.getString("e"));
|
2010-03-07 01:35:44 +01:00
|
|
|
} else if (obj.has("v") && !obj.isNull("v")) {
|
2010-03-06 08:43:45 +01:00
|
|
|
value = (Serializable) obj.get("v");
|
2010-03-07 01:35:44 +01:00
|
|
|
if (obj.has("t") && !obj.isNull("t")) {
|
2010-03-06 08:43:45 +01:00
|
|
|
String type = obj.getString("t");
|
|
|
|
if ("date".equals(type)) {
|
|
|
|
value = ParsingUtilities.stringToDate((String) value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-07 01:35:44 +01:00
|
|
|
if (obj.has("r") && !obj.isNull("r")) {
|
2010-04-09 21:32:50 +02:00
|
|
|
recon = Recon.load(obj.getJSONObject("r"), reconCache);
|
2010-03-06 08:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Cell(value, recon);
|
|
|
|
}
|
2010-04-14 01:57:03 +02:00
|
|
|
|
2010-04-15 02:09:14 +02:00
|
|
|
static public Cell loadStreaming(String s, Map<Long, Recon> reconCache) throws Exception {
|
|
|
|
JsonFactory jsonFactory = new JsonFactory();
|
|
|
|
JsonParser jp = jsonFactory.createJsonParser(s);
|
|
|
|
|
|
|
|
if (jp.nextToken() != JsonToken.START_OBJECT) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadStreaming(jp, reconCache);
|
|
|
|
}
|
|
|
|
|
2010-04-14 01:57:03 +02:00
|
|
|
static public Cell loadStreaming(JsonParser jp, Map<Long, Recon> reconCache) throws Exception {
|
|
|
|
JsonToken t = jp.getCurrentToken();
|
|
|
|
if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serializable value = null;
|
|
|
|
String type = null;
|
|
|
|
Recon recon = null;
|
|
|
|
|
|
|
|
while (jp.nextToken() != JsonToken.END_OBJECT) {
|
|
|
|
String fieldName = jp.getCurrentName();
|
|
|
|
jp.nextToken();
|
|
|
|
|
|
|
|
if ("r".equals(fieldName)) {
|
|
|
|
recon = Recon.loadStreaming(jp, reconCache);
|
|
|
|
} else if ("e".equals(fieldName)) {
|
|
|
|
value = new EvalError(jp.getText());
|
|
|
|
} else if ("v".equals(fieldName)) {
|
|
|
|
JsonToken token = jp.getCurrentToken();
|
|
|
|
|
|
|
|
if (token == JsonToken.VALUE_STRING) {
|
|
|
|
value = jp.getText();
|
|
|
|
} else if (token == JsonToken.VALUE_NUMBER_INT) {
|
2010-04-14 22:34:29 +02:00
|
|
|
value = jp.getLongValue();
|
2010-04-14 01:57:03 +02:00
|
|
|
} else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
|
2010-04-14 22:34:29 +02:00
|
|
|
value = jp.getDoubleValue();
|
2010-04-14 01:57:03 +02:00
|
|
|
} else if (token == JsonToken.VALUE_TRUE) {
|
|
|
|
value = true;
|
|
|
|
} else if (token == JsonToken.VALUE_FALSE) {
|
|
|
|
value = false;
|
|
|
|
}
|
|
|
|
} else if ("t".equals(fieldName)) {
|
|
|
|
type = jp.getText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != null) {
|
|
|
|
if (type != null) {
|
|
|
|
if ("date".equals(type)) {
|
|
|
|
value = ParsingUtilities.stringToDate((String) value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Cell(value, recon);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|