JSCustomXPathNSResolver.cpp 4.48 KB
Newer Older
ap's avatar
ap committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2007 Alexey Proskuryakov (ap@nypop.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "JSCustomXPathNSResolver.h"

mjs's avatar
mjs committed
29
#if ENABLE(XPATH)
ap's avatar
ap committed
30 31

#include "CString.h"
32
#include "Console.h"
weinig's avatar
weinig committed
33
#include "DOMWindow.h"
ap's avatar
ap committed
34 35 36
#include "Document.h"
#include "ExceptionCode.h"
#include "Frame.h"
37
#include "JSDOMWindowCustom.h"
darin@apple.com's avatar
darin@apple.com committed
38 39
#include "JSDOMBinding.h"
#include "ScriptController.h"
ap's avatar
ap committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

namespace WebCore {

using namespace KJS;

PassRefPtr<JSCustomXPathNSResolver> JSCustomXPathNSResolver::create(KJS::ExecState* exec, KJS::JSValue* value)
{
    if (value->isUndefinedOrNull())
        return 0;

    JSObject* resolverObject = value->getObject();
    if (!resolverObject) {
        setDOMException(exec, TYPE_MISMATCH_ERR);
        return 0;
    }
    
56
    return adoptRef(new JSCustomXPathNSResolver(resolverObject, asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame()));
ap's avatar
ap committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

JSCustomXPathNSResolver::JSCustomXPathNSResolver(JSObject* customResolver, Frame* frame)
    : m_customResolver(customResolver)
    , m_frame(frame)
{
}

JSCustomXPathNSResolver::~JSCustomXPathNSResolver()
{
}

String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
{
    ASSERT(m_customResolver);

    if (!m_frame)
        return String();
darin@apple.com's avatar
darin@apple.com committed
75
    if (!m_frame->script()->isEnabled())
ap's avatar
ap committed
76 77 78 79
        return String();

    JSLock lock;

darin@apple.com's avatar
darin@apple.com committed
80
    JSGlobalObject* globalObject = m_frame->script()->globalObject();
ggaren@apple.com's avatar
ggaren@apple.com committed
81 82
    ExecState* exec = globalObject->globalExec();
        
ap's avatar
ap committed
83 84 85 86 87 88 89 90 91 92
    JSValue* lookupNamespaceURIFuncValue = m_customResolver->get(exec, "lookupNamespaceURI");
    JSObject* lookupNamespaceURIFunc = 0;
    if (lookupNamespaceURIFuncValue->isObject()) {      
        lookupNamespaceURIFunc = static_cast<JSObject*>(lookupNamespaceURIFuncValue);
        if (!lookupNamespaceURIFunc->implementsCall())
            lookupNamespaceURIFunc = 0;
    }

    if (!lookupNamespaceURIFunc && !m_customResolver->implementsCall()) {
        // FIXME: pass actual line number and source URL.
93
        m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, "XPathNSResolver does not have a lookupNamespaceURI method.", 0, String());
ap's avatar
ap committed
94 95 96 97 98
        return String();
    }

    RefPtr<JSCustomXPathNSResolver> selfProtector(this);

darin@apple.com's avatar
darin@apple.com committed
99
    ArgList args;
100
    args.append(jsString(prefix));
ap's avatar
ap committed
101 102 103

    String result;
    JSValue* retval;
ggaren@apple.com's avatar
ggaren@apple.com committed
104
    globalObject->startTimeoutCheck();
ap's avatar
ap committed
105
    if (lookupNamespaceURIFunc)
ggaren@apple.com's avatar
ggaren@apple.com committed
106
        retval = lookupNamespaceURIFunc->callAsFunction(exec, m_customResolver, args);
ap's avatar
ap committed
107
    else
ggaren@apple.com's avatar
ggaren@apple.com committed
108
        retval = m_customResolver->callAsFunction(exec, m_customResolver, args);
ggaren@apple.com's avatar
ggaren@apple.com committed
109
    globalObject->stopTimeoutCheck();
ap's avatar
ap committed
110 111 112

    if (exec->hadException()) {
        JSObject* exception = exec->exception()->toObject(exec);
mjs's avatar
mjs committed
113
        String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
ap's avatar
ap committed
114 115
        int lineNumber = exception->get(exec, "line")->toInt32(exec);
        String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
116
        m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
ap's avatar
ap committed
117 118 119 120 121 122 123 124 125 126 127 128 129
        exec->clearException();
    } else {
        if (!retval->isUndefinedOrNull())
            result = retval->toString(exec);
    }

    Document::updateDocumentsRendering();

    return result;
}

} // namespace WebCore

mjs's avatar
mjs committed
130
#endif // ENABLE(XPATH)