use of org.mozilla.javascript.NativeJavaObject in project pentaho-platform by pentaho.
the class JavaScriptResultSet method jsFunction_addRow.
public static Object jsFunction_addRow(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
if (args == null) {
return null;
}
if (args.length == 0) {
return null;
}
// TODO support dates
JavaScriptResultSet resultSet = (JavaScriptResultSet) thisObj;
if ((args.length == 1) && (args[0] instanceof NativeArray)) {
NativeArray array = (NativeArray) args[0];
int length = (int) array.getLength();
Object[] row = new Object[length];
String[] columnTypes = ((MemoryMetaData) resultSet.getMetaData()).getColumnTypes();
for (int i = 0; i < length; i++) {
Object data = array.get(i, thisObj);
if (data == null) {
row[i] = null;
} else if (columnTypes != null) {
if (data instanceof NativeJavaObject) {
// see if we can force a conversion
Object outputClass = null;
if ("string".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.String.class;
} else if ("date".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.util.Date.class;
} else if ("int".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Integer.class;
} else if ("float".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Float.class;
} else if ("double".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Double.class;
}
if ((NativeJavaObject.canConvert(data, outputClass.getClass()))) {
row[i] = Context.jsToJava(data, java.lang.String.class);
} else {
row[i] = null;
}
}
if ("string".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
row[i] = data.toString();
} else if ("date".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
// $NON-NLS-1$
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
row[i] = format.parse((String) data);
} catch (Throwable t) {
row[i] = null;
}
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
// $NON-NLS-1$
row[i] = data;
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = new Integer(((Double) data).intValue());
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
row[i] = new Integer((String) data);
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = data;
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
// $NON-NLS-1$
row[i] = new Double(((Integer) data).floatValue());
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
row[i] = new Integer((String) data);
} else if ("double".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = data;
}
} else if (data instanceof NativeJavaObject) {
Object obj = ((NativeJavaObject) data).unwrap();
row[i] = obj;
} else {
row[i] = data;
}
}
resultSet.addRow(row);
} else {
int length = args.length;
String[] row = new String[length];
for (int i = 0; i < length; i++) {
row[i] = args[i].toString();
}
resultSet.addRow(row);
}
return null;
}
use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.
the class AbstractCouchDbTransaction method toJson.
protected Object toJson(Object object) throws JSONException {
if (object == null)
return null;
Object jsonElement = null;
if (object instanceof NativeObject) {
JSONObject jsonChildren = new JSONObject();
NativeObject nativeObject = (NativeObject) object;
for (Iterator<Entry<Object, Object>> it = GenericUtils.cast(nativeObject.entrySet().iterator()); it.hasNext(); ) {
Entry<Object, Object> entry = it.next();
jsonChildren.put(entry.getKey().toString(), toJson(entry.getValue()));
}
return jsonChildren;
}
if (object instanceof NativeJavaObject) {
NativeJavaObject nativeJavaObject = (NativeJavaObject) object;
return toJson(nativeJavaObject.unwrap());
} else if (object instanceof NativeJavaArray) {
Object ob = ((NativeJavaArray) object).unwrap();
return toJson(Arrays.asList((Object[]) ob));
} else if (object instanceof NativeArray) {
NativeArray array = (NativeArray) object;
JSONArray jsonArray = new JSONArray();
for (int j = 0; j < array.getLength(); j++) {
jsonArray.put(toJson(array.get(j, array)));
}
jsonElement = jsonArray;
} else if ((object instanceof org.mozilla.javascript.Scriptable)) {
org.mozilla.javascript.Scriptable jsObj = (org.mozilla.javascript.Scriptable) object;
return toJson(String.valueOf(jsObj.toString()));
} else if (object.getClass().isArray()) {
return toJson(Arrays.asList((Object[]) object));
} else if (object instanceof Collection<?>) {
JSONArray jsonArray = new JSONArray();
for (Object o : (Collection<?>) object) {
jsonArray.put(toJson(o));
}
jsonElement = jsonArray;
} else if (object instanceof Element) {
jsonElement = toJson((Element) object);
} else {
jsonElement = object;
}
return jsonElement;
}
use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.
the class PacManager method evaluate.
public String evaluate(String url, String host) {
Object result = "";
try {
Context ctx = Context.enter();
result = RhinoUtils.evalInterpretedJavascript(ctx, scope, "FindProxyForURL (\"" + url + "\",\"" + host + "\")", "check", 0, null);
} catch (Exception e) {
Engine.logProxyManager.error("(PacManager) Failed to evaluate .pac for " + url + " from " + host, e);
} finally {
Context.exit();
}
if (result instanceof NativeJavaObject) {
result = ((NativeJavaObject) result).unwrap();
}
Engine.logProxyManager.debug("(PacManager) evaluate " + url + " from " + host + " : " + result);
return result.toString();
}
use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.
the class ClientInstructionSetValue method applyOnResponse.
@Override
public boolean applyOnResponse(Shuttle shuttle) {
String targetPath = getEvaluatedTargetPath(shuttle);
String value = "";
Object ob = shuttle.evalJavascript(targetValue);
if (ob != null) {
if (ob instanceof NativeJavaArray) {
Object object = ((NativeJavaArray) ob).unwrap();
List<Object> list = Arrays.asList((Object[]) object);
for (int j = 0; j < list.size(); j++) {
Object item = list.get(j);
value += item.toString() + ",";
}
} else if (ob instanceof NativeJavaObject) {
NativeJavaObject nativeJavaObject = (NativeJavaObject) ob;
Object javaObject = nativeJavaObject.unwrap();
if (javaObject instanceof List) {
Vector<String> v = GenericUtils.cast(javaObject);
for (int j = 0; j < v.size(); j++) {
value += v.get(j) + ",";
}
} else {
value = (String) nativeJavaObject.getDefaultValue(String.class);
}
} else if (ob instanceof NativeArray) {
NativeArray array = (NativeArray) ob;
for (int j = 0; j < array.getLength(); j++) {
Object item = array.get(j, array);
value += item.toString() + ",";
}
} else if (ob instanceof List) {
Vector<String> v = GenericUtils.cast(ob);
for (int j = 0; j < v.size(); j++) {
value += v.get(j) + ",";
}
} else {
value = ob.toString();
}
}
Engine.logSiteClipper.trace("(ClientInstructionSetValue) JQuery selector '" + targetPath + "' with value '" + value + "'");
shuttle.addPostInstruction(new SetValue(targetPath, value));
return true;
}
use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.
the class InputHtmlSetValueStatement method getEvent.
@Override
public AbstractEvent getEvent(Context javascriptContext, Scriptable scope) throws EngineException {
try {
evaluate(javascriptContext, scope, expression, "expression", true);
} catch (EngineException e) {
// TODO:
}
String value = "";
if (evaluated != null) {
if (evaluated instanceof NativeJavaArray) {
Object object = ((NativeJavaArray) evaluated).unwrap();
List<Object> list = Arrays.asList((Object[]) object);
for (int j = 0; j < list.size(); j++) {
Object item = list.get(j);
value += item.toString() + ",";
}
} else if (evaluated instanceof NativeJavaObject) {
NativeJavaObject nativeJavaObject = (NativeJavaObject) evaluated;
Object javaObject = nativeJavaObject.unwrap();
if (javaObject instanceof List) {
Vector<String> v = GenericUtils.cast(javaObject);
for (int j = 0; j < v.size(); j++) {
value += v.get(j) + ",";
}
} else {
value = (String) nativeJavaObject.getDefaultValue(String.class);
}
} else if (evaluated instanceof NativeArray) {
NativeArray array = (NativeArray) evaluated;
for (int j = 0; j < array.getLength(); j++) {
Object item = array.get(j, array);
value += item.toString() + ",";
}
} else if (evaluated instanceof List) {
Vector<String> v = GenericUtils.cast(evaluated);
for (int j = 0; j < v.size(); j++) {
value += v.get(j) + ",";
}
} else {
value = evaluated.toString();
}
}
return new InputValueEvent(getXpath(), getUiEvent(), value);
}
Aggregations