JSValue* JSXPathResultPrototypeFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args) { if (!thisObj->inherits(&JSXPathResult::info)) return throwError(exec, TypeError); XPathResult* imp = static_cast<XPathResult*>(static_cast<JSXPathResult*>(thisObj)->impl()); switch (id) { case JSXPathResult::IterateNextFuncNum: { ExceptionCode ec = 0; KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->iterateNext(ec))); setDOMException(exec, ec); return result; } case JSXPathResult::SnapshotItemFuncNum: { ExceptionCode ec = 0; bool indexOk; unsigned index = args[0]->toInt32(exec, indexOk); if (!indexOk) { setDOMException(exec, TYPE_MISMATCH_ERR); return jsUndefined(); } KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->snapshotItem(index, ec))); setDOMException(exec, ec); return result; } } return 0; }
JSValue* jsXPathResultPrototypeFunctionIterateNext(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args) { if (!thisValue->isObject(&JSXPathResult::s_info)) return throwError(exec, TypeError); JSXPathResult* castedThisObj = static_cast<JSXPathResult*>(thisValue); XPathResult* imp = static_cast<XPathResult*>(castedThisObj->impl()); ExceptionCode ec = 0; KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->iterateNext(ec))); setDOMException(exec, ec); return result; }
EncodedJSValue JSC_HOST_CALL jsXPathResultPrototypeFunctionIterateNext(ExecState* exec) { JSValue thisValue = exec->hostThisValue(); if (!thisValue.inherits(&JSXPathResult::s_info)) return throwVMTypeError(exec); JSXPathResult* castedThis = static_cast<JSXPathResult*>(asObject(thisValue)); XPathResult* imp = static_cast<XPathResult*>(castedThis->impl()); ExceptionCode ec = 0; JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(imp->iterateNext(ec))); setDOMException(exec, ec); return JSValue::encode(result); }
static v8::Handle<v8::Value> iterateNextCallback(const v8::Arguments& args) { INC_STATS("DOM.XPathResult.iterateNext"); XPathResult* imp = V8XPathResult::toNative(args.Holder()); ExceptionCode ec = 0; { RefPtr<Node> result = imp->iterateNext(ec); if (UNLIKELY(ec)) goto fail; return toV8(result.release()); } fail: V8Proxy::setDOMException(ec); return v8::Handle<v8::Value>(); }
void MainThreadDebugger::xpathSelectorCallback( const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 1) return; String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); if (selector.isEmpty()) return; Node* node = secondArgumentAsNode(info); if (!node || !node->isContainerNode()) return; ExceptionState exceptionState(ExceptionState::ExecutionContext, "$x", "CommandLineAPI", info.Holder(), info.GetIsolate()); XPathResult* result = XPathEvaluator::create()->evaluate( selector, node, nullptr, XPathResult::kAnyType, ScriptValue(), exceptionState); if (exceptionState.hadException() || !result) return; if (result->resultType() == XPathResult::kNumberType) { info.GetReturnValue().Set(toV8(result->numberValue(exceptionState), info.Holder(), info.GetIsolate())); } else if (result->resultType() == XPathResult::kStringType) { info.GetReturnValue().Set(toV8(result->stringValue(exceptionState), info.Holder(), info.GetIsolate())); } else if (result->resultType() == XPathResult::kBooleanType) { info.GetReturnValue().Set(toV8(result->booleanValue(exceptionState), info.Holder(), info.GetIsolate())); } else { v8::Isolate* isolate = info.GetIsolate(); v8::Local<v8::Context> context = isolate->GetCurrentContext(); v8::Local<v8::Array> nodes = v8::Array::New(isolate); size_t index = 0; while (Node* node = result->iterateNext(exceptionState)) { if (exceptionState.hadException()) return; if (!createDataPropertyInArray( context, nodes, index++, toV8(node, info.Holder(), info.GetIsolate())) .FromMaybe(false)) return; } info.GetReturnValue().Set(nodes); } }
int main( ) { try { // Initialize Xerces and retrieve a DOMImplementation. XercesInitializer init; DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation( fromNative("LS").c_str( ) ); if (impl == 0) { cout << "couldn't create DOM implementation\n"; return EXIT_FAILURE; } // Construct a DOMBuilder to parse animals.xml. DOMPtr<DOMBuilder> parser = static_cast<DOMImplementationLS*>(impl)-> createDOMBuilder( DOMImplementationLS::MODE_SYNCHRONOUS, 0 ); CircusErrorHandler err; parser->setErrorHandler(&err); // Parse animals.xml. DOMDocument* doc = parser->parseURI("animals.xml"); DOMElement* animalList = doc->getDocumentElement( ); // Create XPath expression. auto_ptr<XPathEvaluator> evaluator(XPathEvaluator::createEvaluator( )); auto_ptr<XPathNSResolver> resolver(evaluator->createNSResolver(animalList)); auto_ptr<XPathExpression> xpath( evaluator->createExpression( fromNative( "animalList/animal[child::name='Herby']" ).c_str( ), resolver.get( ) ) ); auto_ptr<XPathEvaluator> evaluator(XPathEvaluator::createEvaluator( )); auto_ptr<XPathNSResolver> resolver(evaluator->createNSResolver(animalList)); auto_ptr<XPathExpression> xpath( evaluator->createExpression( fromNative("animalList/animal[child::name='Herby']").c_str( ), resolver.get( ) )); // Evaluate the expression. XPathResult* result = xpath->evaluate( doc, XPathResult::ORDERED_NODE_ITERATOR_TYPE, 0 ); DOMNode* herby; if (herby = result->iterateNext( )) { animalList->removeChild(herby); herby->release( ); // optional. } // Construct a DOMWriter to save animals.xml. DOMPtr<DOMWriter> writer = static_cast<DOMImplementationLS*>(impl)->createDOMWriter( ); writer->setErrorHandler(&err); // Save animals.xml. LocalFileFormatTarget file("circus.xml"); writer->writeNode(&file, *animalList); } catch (const DOMException& e) { cout << toNative(e.getMessage( )) << "\n"; return EXIT_FAILURE; } catch (const XPathException &e) { cout << e.getString( ) << "\n"; return EXIT_FAILURE; } catch (const exception& e) { cout << e.what( ) << "\n"; return EXIT_FAILURE; } }