Search in sources :

Example 1 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class ActiveXObject method jsConstructor.

/**
 * This method
 * <ol>
 *   <li>instantiates the MSXML (ActiveX) object if requested (<code>XMLDOMDocument</code>,
 *      <code>XMLHTTPRequest</code>, <code>XSLTemplate</code>)
 *   <li>searches the map specified in the <code>WebClient</code> class for the Java object to instantiate based
 *      on the ActiveXObject constructor String
 *   <li>uses <code>ActiveXObjectImpl</code> to initiate Jacob.
 * </ol>
 *
 * @param cx the current context
 * @param args the arguments to the ActiveXObject constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    if (args.length < 1 || args.length > 2) {
        throw Context.reportRuntimeError("ActiveXObject Error: constructor must have one or two String parameters.");
    }
    if (Undefined.isUndefined(args[0])) {
        throw Context.reportRuntimeError("ActiveXObject Error: constructor parameter is undefined.");
    }
    if (!(args[0] instanceof String)) {
        throw Context.reportRuntimeError("ActiveXObject Error: constructor parameter must be a String.");
    }
    final String activeXName = (String) args[0];
    final WebWindow window = getWindow(ctorObj).getWebWindow();
    final MSXMLActiveXObjectFactory factory = window.getWebClient().getMSXMLActiveXObjectFactory();
    if (factory.supports(activeXName)) {
        final Scriptable scriptable = factory.create(activeXName, window);
        if (scriptable != null) {
            return scriptable;
        }
    }
    final WebClient webClient = getWindow(ctorObj).getWebWindow().getWebClient();
    final Map<String, String> map = webClient.getActiveXObjectMap();
    if (map != null) {
        final String xClassString = map.get(activeXName);
        if (xClassString != null) {
            try {
                final Class<?> xClass = Class.forName(xClassString);
                final Object object = xClass.newInstance();
                return Context.toObject(object, ctorObj);
            } catch (final Exception e) {
                throw Context.reportRuntimeError("ActiveXObject Error: failed instantiating class " + xClassString + " because " + e.getMessage() + ".");
            }
        }
    }
    if (webClient.getOptions().isActiveXNative() && System.getProperty("os.name").contains("Windows")) {
        try {
            return new ActiveXObjectImpl(activeXName);
        } catch (final Exception e) {
            LOG.warn("Error initiating Jacob", e);
        }
    }
    if (LOG.isWarnEnabled()) {
        LOG.warn("Automation server can't create object for '" + activeXName + "'.");
    }
    throw Context.reportRuntimeError("Automation server can't create object for '" + activeXName + "'.");
}
Also used : MSXMLActiveXObjectFactory(com.gargoylesoftware.htmlunit.activex.javascript.msxml.MSXMLActiveXObjectFactory) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 2 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class MessageEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (getBrowserVersion().hasFeature(EVENT_ONMESSAGE_DEFAULT_DATA_NULL)) {
        data_ = null;
    }
    String origin = "";
    String lastEventId = "";
    if (details != null && !Undefined.isUndefined(details)) {
        data_ = details.get("data");
        final String detailOrigin = (String) details.get(HttpHeader.ORIGIN_LC);
        if (detailOrigin != null) {
            origin = detailOrigin;
        }
        final Object detailLastEventId = details.get("lastEventId");
        if (detailLastEventId != null) {
            lastEventId = Context.toString(detailLastEventId);
        }
        source_ = null;
        final Object detailSource = details.get("source");
        if (detailSource instanceof Window) {
            source_ = (Window) detailSource;
        } else if (detailSource instanceof WindowProxy) {
            source_ = ((WindowProxy) detailSource).getDelegee();
        }
        ports_ = details.get("ports");
    }
    origin_ = origin;
    lastEventId_ = lastEventId;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) WindowProxy(com.gargoylesoftware.htmlunit.javascript.host.WindowProxy) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 3 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class MouseEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
@Override
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(ScriptRuntime.toString(type), details);
    if (details != null && !Undefined.isUndefined(details)) {
        final Object screenX = details.get("screenX", details);
        if (NOT_FOUND != screenX) {
            screenX_ = ScriptRuntime.toInt32(screenX);
        }
        final Object screenY = details.get("screenY", details);
        if (NOT_FOUND != screenX) {
            screenY_ = ScriptRuntime.toInt32(screenY);
        }
        final Object clientX = details.get("clientX", details);
        if (NOT_FOUND != clientX) {
            clientX_ = ScriptRuntime.toInt32(clientX);
        }
        final Object clientY = details.get("clientY", details);
        if (NOT_FOUND != clientX) {
            clientY_ = ScriptRuntime.toInt32(clientY);
        }
        final Object button = details.get("button", details);
        if (NOT_FOUND != button) {
            button_ = ScriptRuntime.toInt32(button);
        }
        final Object buttons = details.get("buttons", details);
        if (NOT_FOUND != buttons) {
            buttons_ = ScriptRuntime.toInt32(buttons);
        }
        setAltKey(ScriptRuntime.toBoolean(details.get("altKey")));
        setCtrlKey(ScriptRuntime.toBoolean(details.get("ctrlKey")));
        setMetaKey(ScriptRuntime.toBoolean(details.get("metaKey")));
        setShiftKey(ScriptRuntime.toBoolean(details.get("shiftKey")));
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 4 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class InputEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (details != null && !Undefined.isUndefined(details)) {
        if (getBrowserVersion().hasFeature(JS_EVENT_INPUT_CTOR_INPUTTYPE)) {
            final Object inputType = details.get("inputType", details);
            if (!isMissingOrUndefined(inputType)) {
                inputType_ = ScriptRuntime.toString(inputType);
            }
        }
        final Object dataObj = details.get("data", details);
        if (!isMissingOrUndefined(dataObj)) {
            data_ = ScriptRuntime.toString(dataObj);
        }
        final Object isComposing = details.get("isComposing", details);
        if (!isMissingOrUndefined(isComposing)) {
            setIsComposing(ScriptRuntime.toBoolean(isComposing));
        }
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 5 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class KeyboardEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
@Override
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (details != null && !Undefined.isUndefined(details)) {
        final Object key = details.get("key", details);
        if (!isMissingOrUndefined(key)) {
            setKey(ScriptRuntime.toString(key));
        }
        final Object code = details.get("code", details);
        if (!isMissingOrUndefined(code)) {
            setCode(ScriptRuntime.toString(code));
        }
        final Object location = details.get("location", details);
        if (!isMissingOrUndefined(location)) {
            setLocation(ScriptRuntime.toInt32(location));
        }
        final Object ctrlKey = details.get("ctrlKey", details);
        if (!isMissingOrUndefined(ctrlKey)) {
            setCtrlKey(ScriptRuntime.toBoolean(ctrlKey));
        }
        final Object shiftKey = details.get("shiftKey", details);
        if (!isMissingOrUndefined(shiftKey)) {
            setShiftKey(ScriptRuntime.toBoolean(shiftKey));
        }
        final Object altKey = details.get("altKey", details);
        if (!isMissingOrUndefined(altKey)) {
            setAltKey(ScriptRuntime.toBoolean(altKey));
        }
        final Object metaKey = details.get("metaKey", details);
        if (!isMissingOrUndefined(metaKey)) {
            setMetaKey(ScriptRuntime.toBoolean(metaKey));
        }
        final Object repeat = details.get("repeat", details);
        if (!isMissingOrUndefined(repeat)) {
            setRepeat(ScriptRuntime.toBoolean(repeat));
        }
        final Object isComposing = details.get("isComposing", details);
        if (!isMissingOrUndefined(isComposing)) {
            setIsComposing(ScriptRuntime.toBoolean(isComposing));
        }
        final Object charCode = details.get("charCode", details);
        if (!isMissingOrUndefined(charCode)) {
            setCharCode(ScriptRuntime.toInt32(charCode));
        }
        final Object keyCode = details.get("keyCode", details);
        if (!isMissingOrUndefined(keyCode)) {
            setKeyCode(ScriptRuntime.toInt32(keyCode));
        }
        if (getBrowserVersion().hasFeature(JS_EVENT_KEYBOARD_CTOR_WHICH)) {
            final Object which = details.get("which", details);
            if (!isMissingOrUndefined(which)) {
                setWhich(ScriptRuntime.toInt32(which));
            }
        }
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)12 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)6 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 MSXMLActiveXObjectFactory (com.gargoylesoftware.htmlunit.activex.javascript.msxml.MSXMLActiveXObjectFactory)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomText (com.gargoylesoftware.htmlunit.html.DomText)1 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 WindowProxy (com.gargoylesoftware.htmlunit.javascript.host.WindowProxy)1 NativeArray (net.sourceforge.htmlunit.corejs.javascript.NativeArray)1 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1 NativeUint8ClampedArray (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1