Java Code Examples for org.mozilla.javascript.NativeArray#get()

The following examples show how to use org.mozilla.javascript.NativeArray#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RhinoValueConverter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object convert( final Object o ) {
  if ( o instanceof NativeJavaObject ) {
    final NativeJavaObject object = (NativeJavaObject) o;
    return object.unwrap();
  }
  if ( o instanceof NativeArray ) {
    final NativeArray array = (NativeArray) o;
    final Object[] result = new Object[(int) array.getLength()];
    for ( final Object val : array.getIds() ) {
      final int index = (Integer) val;
      result[index] = array.get( index, null );
    }
    return result;
  }
  return null;
}
 
Example 2
Source File: JsonParserTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldParseHeterogeneousJsonArray() throws Exception {
    NativeArray actual = (NativeArray) parser
            .parseValue("[ \"hello\" , 3, null, [false] ]");
    assertEquals("hello", actual.get(0, actual));
    assertEquals(3, actual.get(1, actual));
    assertEquals(null, actual.get(2, actual));

    NativeArray innerArr = (NativeArray) actual.get(3, actual);
    assertEquals(false, innerArr.get(0, innerArr));

    assertEquals(4, actual.getLength());
}
 
Example 3
Source File: JsonParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldParseHeterogeneousJsonArray() throws Exception {
    NativeArray actual = (NativeArray) parser
            .parseValue("[ \"hello\" , 3, null, [false] ]");
    assertEquals("hello", actual.get(0, actual));
    assertEquals(3, actual.get(1, actual));
    assertEquals(null, actual.get(2, actual));

    NativeArray innerArr = (NativeArray) actual.get(3, actual);
    assertEquals(false, innerArr.get(0, innerArr));

    assertEquals(4, actual.getLength());
}
 
Example 4
Source File: PNetwork.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
@PhonkMethod(description = "Simple http post request. It needs an object to be sent. If an element of the object contains the key file then it will try to upload the resource indicated in the value as Uri ", example = "")
@PhonkMethodParam(params = {"url", "params", "function(responseString)"})
public void httpPost(final String url, final NativeArray parts, final ReturnInterface callbackfn) {
    final OkHttpClient client = new OkHttpClient();

    Thread t = new Thread(() -> {

        MultipartBody.Builder formBody = new MultipartBody.Builder();
        formBody.setType(MultipartBody.FORM);

        for (int i = 0; i < parts.size(); i++) {
            NativeObject o = (NativeObject) parts.get(i);

            // go through elements
            String name = o.get("name").toString();
            String content = o.get("content").toString();
            String type = o.get("type").toString();

            if (type.equals("file")) {
                String mediaType = (String) o.get("mediaType");
                File f = new File(getAppRunner().getProject().getFullPathForFile(content));
                MLog.d("nn1", f.getAbsolutePath() + " " + content + " " + name + " " + mediaType);
                formBody.addFormDataPart(name, content, RequestBody.create(MediaType.parse(mediaType), f));
            } else {
                formBody.addFormDataPart(name, content);
            }
        }
        MultipartBody body = formBody.build();

        Request request = new Request.Builder().url(url).post(body).build();
        Response response = null;
        final ReturnObject ret = new ReturnObject();
        try {
            response = client.newCall(request).execute();
            ret.put("response", response.body().string());
            ret.put("status", response.code());
            mHandler.post(() -> callbackfn.event(ret));
        } catch (IOException e) {
            e.printStackTrace();
        }


    });
    t.start();


    /*
    final HttpClient httpClient = new DefaultHttpClient();
    final HttpContext localContext = new BasicHttpContext();
    final HttpPost httpPost = new HttpPost(url);

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for (int i = 0; i < parts.size(); i++) {
        NativeObject o = (NativeObject) parts.get(i);

        // go through elements
        String name = (String) o.get("name");
        String content = (String) o.get("content");
        String type = (String) o.get("type");

        // create the multipart
        if (type.contains("file")) {
            File f = new File(getAppRunner().getProject().getFullPathForFile(content));
            ContentBody cbFile = new FileBody(f);
            entity.addPart(name, cbFile);
        } else if (type.equals("text")){ // Normal string data
            entity.addPart(name, new StringBody(content, ContentType.TEXT_PLAIN));
        } else if (type.equals("json")){ // Normal string data
            entity.addPart(name, new StringBody(content, ContentType.APPLICATION_JSON));
        }
    }

    // send
    httpPost.setEntity(entity);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HttpResponse response = httpClient.execute(httpPost, localContext);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                String responseString = out.toString();

                ReturnObject o = new ReturnObject();
                o.put("status", response.getStatusLine().toString());
                o.put("response", responseString);
                callbackfn.event(o);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    */
}