public Object execIdCall()

in rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XMLObjectImpl.java [857:1084]


    public Object execIdCall(
            IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
        if (!f.hasTag(XMLOBJECT_TAG)) {
            return super.execIdCall(f, cx, scope, thisObj, args);
        }
        int id = f.methodId();
        if (id == Id_constructor) {
            return jsConstructor(cx, thisObj == null, args);
        }

        // All (XML|XMLList).prototype methods require thisObj to be XML
        XMLObjectImpl realThis = ensureType(thisObj, XMLObjectImpl.class, f);

        XML xml = realThis.getXML();
        switch (id) {
            case Id_appendChild:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "appendChild");
                    return xml.appendChild(arg(args, 0));
                }
            case Id_addNamespace:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "addNamespace");
                    Namespace ns = lib.castToNamespace(cx, arg(args, 0));
                    return xml.addNamespace(ns);
                }
            case Id_childIndex:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "childIndex");
                    return ScriptRuntime.wrapInt(xml.childIndex());
                }
            case Id_inScopeNamespaces:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "inScopeNamespaces");
                    return cx.newArray(scope, toObjectArray(xml.inScopeNamespaces()));
                }
            case Id_insertChildAfter:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "insertChildAfter");
                    Object arg0 = arg(args, 0);
                    if (arg0 == null || arg0 instanceof XML) {
                        return xml.insertChildAfter((XML) arg0, arg(args, 1));
                    }
                    return Undefined.instance;
                }
            case Id_insertChildBefore:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "insertChildBefore");
                    Object arg0 = arg(args, 0);
                    if (arg0 == null || arg0 instanceof XML) {
                        return xml.insertChildBefore((XML) arg0, arg(args, 1));
                    }
                    return Undefined.instance;
                }
            case Id_localName:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "localName");
                    return xml.localName();
                }
            case Id_name:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "name");
                    return xml.name();
                }
            case Id_namespace:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "namespace");
                    String prefix = (args.length > 0) ? ScriptRuntime.toString(args[0]) : null;
                    Namespace rv = xml.namespace(prefix);
                    if (rv == null) {
                        return Undefined.instance;
                    } else {
                        return rv;
                    }
                }
            case Id_namespaceDeclarations:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "namespaceDeclarations");
                    Namespace[] array = xml.namespaceDeclarations();
                    return cx.newArray(scope, toObjectArray(array));
                }
            case Id_nodeKind:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "nodeKind");
                    return xml.nodeKind();
                }
            case Id_prependChild:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "prependChild");
                    return xml.prependChild(arg(args, 0));
                }
            case Id_removeNamespace:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "removeNamespace");
                    Namespace ns = lib.castToNamespace(cx, arg(args, 0));
                    return xml.removeNamespace(ns);
                }
            case Id_replace:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "replace");
                    XMLName xmlName = lib.toXMLNameOrIndex(cx, arg(args, 0));
                    Object arg1 = arg(args, 1);
                    if (xmlName == null) {
                        //    I refuse to believe that this number will exceed 2^31
                        int index = (int) ScriptRuntime.lastUint32Result(cx);
                        return xml.replace(index, arg1);
                    } else {
                        return xml.replace(xmlName, arg1);
                    }
                }
            case Id_setChildren:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "setChildren");
                    return xml.setChildren(arg(args, 0));
                }
            case Id_setLocalName:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "setLocalName");
                    String localName;
                    Object arg = arg(args, 0);
                    if (arg instanceof QName) {
                        localName = ((QName) arg).localName();
                    } else {
                        localName = ScriptRuntime.toString(arg);
                    }
                    xml.setLocalName(localName);
                    return Undefined.instance;
                }
            case Id_setName:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "setName");
                    Object arg = (args.length != 0) ? args[0] : Undefined.instance;
                    QName qname = lib.constructQName(cx, arg);
                    xml.setName(qname);
                    return Undefined.instance;
                }
            case Id_setNamespace:
                {
                    if (xml == null) xmlMethodNotFound(realThis, "setNamespace");
                    Namespace ns = lib.castToNamespace(cx, arg(args, 0));
                    xml.setNamespace(ns);
                    return Undefined.instance;
                }

            case Id_attribute:
                {
                    XMLName xmlName =
                            XMLName.create(lib.toNodeQName(cx, arg(args, 0), true), true, false);
                    return realThis.getMatches(xmlName);
                }
            case Id_attributes:
                return realThis.getMatches(
                        XMLName.create(XmlNode.QName.create(null, null), true, false));
            case Id_child:
                {
                    XMLName xmlName = lib.toXMLNameOrIndex(cx, arg(args, 0));
                    if (xmlName == null) {
                        //    Two billion or so is a fine upper limit, so we cast to int
                        int index = (int) ScriptRuntime.lastUint32Result(cx);
                        return realThis.child(index);
                    } else {
                        return realThis.child(xmlName);
                    }
                }
            case Id_children:
                return realThis.children();
            case Id_comments:
                return realThis.comments();
            case Id_contains:
                return ScriptRuntime.wrapBoolean(realThis.contains(arg(args, 0)));
            case Id_copy:
                return realThis.copy();
            case Id_descendants:
                {
                    XmlNode.QName qname =
                            (args.length == 0)
                                    ? XmlNode.QName.create(null, null)
                                    : lib.toNodeQName(cx, args[0], false);
                    return realThis.getMatches(XMLName.create(qname, false, true));
                }
            case Id_elements:
                {
                    XMLName xmlName =
                            (args.length == 0) ? XMLName.formStar() : lib.toXMLName(cx, args[0]);
                    return realThis.elements(xmlName);
                }
            case Id_hasOwnProperty:
                {
                    XMLName xmlName = lib.toXMLName(cx, arg(args, 0));
                    return ScriptRuntime.wrapBoolean(realThis.hasOwnProperty(xmlName));
                }
            case Id_hasComplexContent:
                return ScriptRuntime.wrapBoolean(realThis.hasComplexContent());
            case Id_hasSimpleContent:
                return ScriptRuntime.wrapBoolean(realThis.hasSimpleContent());
            case Id_length:
                return ScriptRuntime.wrapInt(realThis.length());
            case Id_normalize:
                realThis.normalize();
                return Undefined.instance;
            case Id_parent:
                return realThis.parent();
            case Id_processingInstructions:
                {
                    XMLName xmlName =
                            (args.length > 0) ? lib.toXMLName(cx, args[0]) : XMLName.formStar();
                    return realThis.processingInstructions(xmlName);
                }
            case Id_propertyIsEnumerable:
                {
                    return ScriptRuntime.wrapBoolean(realThis.propertyIsEnumerable(arg(args, 0)));
                }
            case Id_text:
                return realThis.text();
            case Id_toString:
                return realThis.toString();
            case Id_toSource:
                int indent = ScriptRuntime.toInt32(args, 0);
                return realThis.toSource(indent);
            case Id_toXMLString:
                {
                    return realThis.toXMLString();
                }
            case Id_valueOf:
                return realThis.valueOf();
        }
        throw new IllegalArgumentException(String.valueOf(id));
    }