use of org.mozilla.javascript.NativeJavaArray 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.NativeJavaArray 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.NativeJavaArray 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);
}
use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.
the class HTTPStatement method makeQuery.
private String makeQuery(com.twinsoft.convertigo.engine.Context context, String methodToAnalyse) throws EngineException {
String variable, httpVariable, httpVariableValue, method, query = "";
int len = numberOfVariables();
String urlEncodingCharset = getUrlEncodingCharset();
if (urlEncodingCharset == null || urlEncodingCharset.length() == 0) {
urlEncodingCharset = getParentTransaction().getComputedUrlEncodingCharset();
}
try {
for (int i = 0; i < len; i++) {
HttpStatementVariable httpStatementVariable = (HttpStatementVariable) getVariable(i);
if (httpStatementVariable != null) {
variable = httpStatementVariable.getName();
method = httpStatementVariable.getHttpMethod();
httpVariable = httpStatementVariable.getHttpName();
if (method.equals(methodToAnalyse)) {
if (query.length() != 0) {
query += "&";
}
try {
// evaluate method can throw EngineException
// try catch to get the default value in this case
evaluate(javascriptContext, scope, variable, httpVariable, false);
Engine.logBeans.debug("Javascript evaluation of httpVariable named '" + httpVariable + "' executed");
// if no Engine Exception has been thown until here, normal execution
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);
httpVariableValue = item.toString();
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
}
} 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++) {
httpVariableValue = v.get(j);
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
}
} else {
httpVariableValue = (String) nativeJavaObject.getDefaultValue(String.class);
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
}
} else if (evaluated instanceof NativeArray) {
NativeArray array = (NativeArray) evaluated;
for (int j = 0; j < array.getLength(); j++) {
Object item = array.get(j, array);
httpVariableValue = item.toString();
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
}
} else if (evaluated instanceof List) {
Vector<String> v = GenericUtils.cast(evaluated);
for (int j = 0; j < v.size(); j++) {
httpVariableValue = v.get(j);
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
}
} else if (evaluated instanceof Undefined) {
throw new EngineException("Undefined");
} else {
httpVariableValue = evaluated.toString();
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
}
}
} catch (EngineException e) {
// Engine Exception has been thrown ==> get variable default value
Object value = getVariableValue(variable);
if (value != null) {
if (value instanceof Collection) {
List<String> list = GenericUtils.toString((Collection<?>) value);
for (String val : list) {
httpVariableValue = val;
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
}
} else {
httpVariableValue = value.toString();
query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
}
}
}
}
}
}
} catch (UnsupportedEncodingException e) {
throw new EngineException("UTF-8 encoding is not supported.", e);
}
return query;
}
use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.
the class FoundSet method get.
public Object get(String name, Scriptable start) {
if (isToplevelKeyword(name))
return Scriptable.NOT_FOUND;
if (// $NON-NLS-1$
name.equals("multiSelect")) {
return Boolean.valueOf(isMultiSelect());
}
if (// $NON-NLS-1$
"alldataproviders".equals(name)) {
return new NativeJavaArray(this, alldataproviders());
}
Object mobj = jsFunctions.get(name);
if (mobj != null) {
ScriptRuntime.setFunctionProtoAndParent((BaseFunction) mobj, start);
return mobj;
}
if (getSize() == 0) {
try {
if (isValidRelation(name)) {
return getPrototypeState().getValue(name);
}
if (containsDataProvider(name)) {
return getDataProviderValue(name);
}
} catch (Exception ex) {
Debug.error(ex);
}
} else if (containsDataProvider(name)) {
return getDataProviderValue(name);
} else if (has(name, start)) {
int row = getSelectedIndex();
// but if a value is being get it should always try to get a value.
if (row == -1 && getSize() > 0)
row = 0;
if (row != -1) {
IRecordInternal state = getRecord(row);
if (state instanceof Scriptable) {
return ((Scriptable) state).get(name, start);
}
}
}
if (// $NON-NLS-1$
name.equals("_records_")) {
int maxRows = getSize();
if (hadMoreRows()) {
maxRows--;
}
Scriptable records = Context.getCurrentContext().newArray(this, maxRows);
for (int i = 0; i < maxRows; i++) {
IRecordInternal record = pksAndRecords.getCachedRecords().get(i);
records.put(i, records, record);
}
return records;
}
if (// $NON-NLS-1$
name.equals("_selection_")) {
int[] selection = getSelectedIndexes();
if (selection.length == 0) {
return Integer.valueOf(0);
}
if (selection.length == 1) {
return Integer.valueOf(selection[0] + 1);
}
StringBuilder buf = new StringBuilder();
buf.append('[');
for (int i = 0; i < selection.length; i++) {
// $NON-NLS-1$
if (i > 0)
buf.append(", ");
buf.append(selection[i] + 1);
}
buf.append(']');
return buf.toString();
}
return Scriptable.NOT_FOUND;
}
Aggregations