Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#callMethod()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#callMethod() . 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: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter,
        final boolean filterFunctionOnly) {
    org.w3c.dom.traversal.NodeFilter filterWrapper = null;
    if (filter != null) {
        filterWrapper = new org.w3c.dom.traversal.NodeFilter() {
            @Override
            public short acceptNode(final org.w3c.dom.Node n) {
                final Object[] args = new Object[] {((DomNode) n).getScriptableObject()};
                final Object response;
                if (filter instanceof Callable) {
                    response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args);
                }
                else {
                    if (filterFunctionOnly) {
                        throw Context.reportRuntimeError("only a function is allowed as filter");
                    }
                    response = ScriptableObject.callMethod(filter, "acceptNode", args);
                }
                return (short) Context.toNumber(response);
            }
        };
    }
    return filterWrapper;
}
 
Example 2
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter,
        final boolean filterFunctionOnly) {
    org.w3c.dom.traversal.NodeFilter filterWrapper = null;
    if (filter != null) {
        filterWrapper = new org.w3c.dom.traversal.NodeFilter() {
            @Override
            public short acceptNode(final org.w3c.dom.Node n) {
                final Object[] args = new Object[] {((DomNode) n).getScriptableObject()};
                final Object response;
                if (filter instanceof Callable) {
                    response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args);
                }
                else {
                    if (filterFunctionOnly) {
                        throw Context.reportRuntimeError("only a function is allowed as filter");
                    }
                    response = ScriptableObject.callMethod(filter, "acceptNode", args);
                }
                return (short) Context.toNumber(response);
            }
        };
    }
    return filterWrapper;
}
 
Example 3
Source File: JSObject.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a JavaScript method.
 * Equivalent to "this.methodName(args[0], args[1], ...)" in JavaScript.
 *
 * @param methodName the name of the JavaScript method to be invoked
 * @param args an array of Java object to be passed as arguments to the method
 * @return result result of the method
 * @throws JSException in case or error
 */
public Object call(final String methodName, final Object[] args) throws JSException {
    if (LOG.isInfoEnabled()) {
        LOG.info("JSObject call '" + methodName + "(" + Arrays.toString(args) + ")'");
    }

    final Object jsResult = ScriptableObject.callMethod(scriptableObject_, methodName, args);
    if (jsResult instanceof ScriptableObject) {
        return new JSObject((ScriptableObject) jsResult);
    }
    if (jsResult instanceof ConsString) {
        return ((ConsString) jsResult).toString();
    }
    return jsResult;
}
 
Example 4
Source File: JSObject.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a JavaScript method.
 * Equivalent to "this.methodName(args[0], args[1], ...)" in JavaScript.
 *
 * @param methodName the name of the JavaScript method to be invoked
 * @param args an array of Java object to be passed as arguments to the method
 * @return result result of the method
 * @throws JSException in case or error
 */
public Object call(final String methodName, final Object[] args) throws JSException {
    if (LOG.isInfoEnabled()) {
        LOG.info("JSObject call '" + methodName + "(" + Arrays.toString(args) + ")'");
    }

    final Object jsResult = ScriptableObject.callMethod(scriptableObject_, methodName, args);
    if (jsResult instanceof ScriptableObject) {
        return new JSObject((ScriptableObject) jsResult);
    }
    if (jsResult instanceof ConsString) {
        return ((ConsString) jsResult).toString();
    }
    return jsResult;
}