Search in sources :

Example 11 with XMLObject

use of org.mozilla.javascript.xml.XMLObject in project hackpad by dropbox.

the class ScriptRuntime method getElemFunctionAndThis.

/**
     * Prepare for calling obj[id](...): return function corresponding to
     * obj[id] and make obj properly converted to Scriptable available
     * as ScriptRuntime.lastStoredScriptable() for consumption as thisObj.
     * The caller must call ScriptRuntime.lastStoredScriptable() immediately
     * after calling this method.
     */
public static Callable getElemFunctionAndThis(Object obj, Object elem, Context cx) {
    String s = toStringIdOrIndex(cx, elem);
    if (s != null) {
        return getPropFunctionAndThis(obj, s, cx);
    }
    int index = lastIndexResult(cx);
    Scriptable thisObj = toObjectOrNull(cx, obj);
    if (thisObj == null) {
        throw undefCallError(obj, String.valueOf(index));
    }
    Object value;
    for (; ; ) {
        // Ignore XML lookup as required by ECMA 357, 11.2.2.1
        value = ScriptableObject.getProperty(thisObj, index);
        if (value != Scriptable.NOT_FOUND) {
            break;
        }
        if (!(thisObj instanceof XMLObject)) {
            break;
        }
        XMLObject xmlObject = (XMLObject) thisObj;
        Scriptable extra = xmlObject.getExtraMethodSource(cx);
        if (extra == null) {
            break;
        }
        thisObj = extra;
    }
    if (!(value instanceof Callable)) {
        throw notFunctionError(value, elem);
    }
    storeScriptable(cx, thisObj);
    return (Callable) value;
}
Also used : XMLObject(org.mozilla.javascript.xml.XMLObject) XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 12 with XMLObject

use of org.mozilla.javascript.xml.XMLObject in project hackpad by dropbox.

the class ScriptRuntime method getPropFunctionAndThisHelper.

private static Callable getPropFunctionAndThisHelper(Object obj, String property, Context cx, Scriptable thisObj) {
    if (thisObj == null) {
        throw undefCallError(obj, property);
    }
    Object value;
    for (; ; ) {
        // Ignore XML lookup as required by ECMA 357, 11.2.2.1
        value = ScriptableObject.getProperty(thisObj, property);
        if (value != Scriptable.NOT_FOUND) {
            break;
        }
        if (!(thisObj instanceof XMLObject)) {
            break;
        }
        XMLObject xmlObject = (XMLObject) thisObj;
        Scriptable extra = xmlObject.getExtraMethodSource(cx);
        if (extra == null) {
            break;
        }
        thisObj = extra;
    }
    if (!(value instanceof Callable)) {
        Object noSuchMethod = ScriptableObject.getProperty(thisObj, "__noSuchMethod__");
        if (noSuchMethod instanceof Callable)
            value = new NoSuchMethodShim((Callable) noSuchMethod, property);
        else
            throw notFunctionError(thisObj, value, property);
    }
    storeScriptable(cx, thisObj);
    return (Callable) value;
}
Also used : XMLObject(org.mozilla.javascript.xml.XMLObject) XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 13 with XMLObject

use of org.mozilla.javascript.xml.XMLObject in project hackpad by dropbox.

the class ScriptRuntime method deleteObjectElem.

public static boolean deleteObjectElem(Scriptable target, Object elem, Context cx) {
    boolean result;
    if (target instanceof XMLObject) {
        XMLObject xmlObject = (XMLObject) target;
        result = xmlObject.ecmaDelete(cx, elem);
    } else {
        String s = toStringIdOrIndex(cx, elem);
        if (s == null) {
            int index = lastIndexResult(cx);
            target.delete(index);
            return !target.has(index, target);
        } else {
            target.delete(s);
            return !target.has(s, target);
        }
    }
    return result;
}
Also used : XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 14 with XMLObject

use of org.mozilla.javascript.xml.XMLObject in project hackpad by dropbox.

the class ScriptRuntime method nameOrFunction.

private static Object nameOrFunction(Context cx, Scriptable scope, Scriptable parentScope, String name, boolean asFunctionCall) {
    Object result;
    // It is used only if asFunctionCall==true.
    Scriptable thisObj = scope;
    XMLObject firstXMLObject = null;
    for (; ; ) {
        if (scope instanceof NativeWith) {
            Scriptable withObj = scope.getPrototype();
            if (withObj instanceof XMLObject) {
                XMLObject xmlObj = (XMLObject) withObj;
                if (xmlObj.ecmaHas(cx, name)) {
                    // function this should be the target object of with
                    thisObj = xmlObj;
                    result = xmlObj.ecmaGet(cx, name);
                    break;
                }
                if (firstXMLObject == null) {
                    firstXMLObject = xmlObj;
                }
            } else {
                result = ScriptableObject.getProperty(withObj, name);
                if (result != Scriptable.NOT_FOUND) {
                    // function this should be the target object of with
                    thisObj = withObj;
                    break;
                }
            }
        } else if (scope instanceof NativeCall) {
            // NativeCall does not prototype chain and Scriptable.get
            // can be called directly.
            result = scope.get(name, scope);
            if (result != Scriptable.NOT_FOUND) {
                if (asFunctionCall) {
                    // ECMA 262 requires that this for nested funtions
                    // should be top scope
                    thisObj = ScriptableObject.getTopLevelScope(parentScope);
                }
                break;
            }
        } else {
            // Can happen if Rhino embedding decided that nested
            // scopes are useful for what ever reasons.
            result = ScriptableObject.getProperty(scope, name);
            if (result != Scriptable.NOT_FOUND) {
                thisObj = scope;
                break;
            }
        }
        scope = parentScope;
        parentScope = parentScope.getParentScope();
        if (parentScope == null) {
            result = topScopeName(cx, scope, name);
            if (result == Scriptable.NOT_FOUND) {
                if (firstXMLObject == null || asFunctionCall) {
                    throw notFoundError(scope, name);
                }
                // The name was not found, but we did find an XML
                // object in the scope chain and we are looking for name,
                // not function. The result should be an empty XMLList
                // in name context.
                result = firstXMLObject.ecmaGet(cx, name);
            }
            // For top scope thisObj for functions is always scope itself.
            thisObj = scope;
            break;
        }
    }
    if (asFunctionCall) {
        if (!(result instanceof Callable)) {
            throw notFunctionError(result, name);
        }
        storeScriptable(cx, thisObj);
    }
    return result;
}
Also used : XMLObject(org.mozilla.javascript.xml.XMLObject) XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 15 with XMLObject

use of org.mozilla.javascript.xml.XMLObject in project jaggery by wso2.

the class XSLTHostObject method jsConstructor.

/**
     * XSLT(xslt)
     * XSLT(xslt, paramMap)
     * XSLT(xslt, uriResolver)
     * XSLT(xslt, paramMap, uriResolver)
     */
public static Scriptable jsConstructor(final Context cx, Object[] args, final Function ctorObj, boolean inNewExpr) throws ScriptException {
    int argsCount = args.length;
    if (argsCount < 1 || argsCount > 3) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
    }
    Function uriResolver = null;
    NativeObject paramMap = null;
    XSLTHostObject xho = new XSLTHostObject();
    if (args[0] instanceof String) {
        xho.xslt = new StringReader((String) args[0]);
    } else if (args[0] instanceof XMLObject) {
        xho.xslt = new StringReader(args[0].toString());
    } else {
        HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string | xml", args[0], true);
    }
    if (argsCount == 2) {
        if (args[1] instanceof NativeObject) {
            paramMap = (NativeObject) args[1];
        } else if (args[1] instanceof Function) {
            uriResolver = (Function) args[1];
        } else {
            HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "2", "string", args[1], true);
        }
    } else if (argsCount == 3) {
        if (args[1] instanceof NativeObject) {
            paramMap = (NativeObject) args[1];
        } else {
            HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "2", "string", args[1], true);
        }
        if (args[2] instanceof Function) {
            uriResolver = (Function) args[2];
        } else {
            HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "3", "string", args[1], true);
        }
    }
    xho.factory = TransformerFactory.newInstance();
    if (uriResolver != null) {
        xho.factory.setURIResolver(getUriResolver(cx, ctorObj, uriResolver));
    }
    return xho;
}
Also used : StringReader(java.io.StringReader) XMLObject(org.mozilla.javascript.xml.XMLObject)

Aggregations

XMLObject (org.mozilla.javascript.xml.XMLObject)17 StringReader (java.io.StringReader)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Abdera (org.apache.abdera.Abdera)1 Factory (org.apache.abdera.factory.Factory)1 IRISyntaxException (org.apache.abdera.i18n.iri.IRISyntaxException)1 LogFactory (org.apache.commons.logging.LogFactory)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 Context (org.mozilla.javascript.Context)1 Scriptable (org.mozilla.javascript.Scriptable)1 Wrapper (org.mozilla.javascript.Wrapper)1 Node (org.w3c.dom.Node)1