Search in sources :

Example 1 with MSXMLActiveXObjectFactory

use of com.gargoylesoftware.htmlunit.activex.javascript.msxml.MSXMLActiveXObjectFactory 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)

Aggregations

WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 MSXMLActiveXObjectFactory (com.gargoylesoftware.htmlunit.activex.javascript.msxml.MSXMLActiveXObjectFactory)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1