Value XMLSerializerProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
{
  if (!thisObj.inherits(&XMLSerializer::info)) {
    Object err = Error::create(exec,TypeError);
    exec->setException(err);
    return err;
  }

  switch (id) {
  case XMLSerializer::SerializeToString:
    {
      if (args.size() != 1) {
	return Undefined();
      }

      if (!args[0].toObject(exec).inherits(&DOMDocument::info)) {
	return Undefined();
      }

      DOM::Node docNode = static_cast<KJS::DOMDocument *>(args[0].toObject(exec).imp())->toNode();
      DOM::DocumentImpl *doc = static_cast<DOM::DocumentImpl *>(docNode.handle());

      if (!doc) {
	return Undefined();
      }
	  
      QString body;

      try {
	  body = doc->toString().string();
      } catch(DOM::DOMException& e) {
	  Object err = Error::create(exec, GeneralError, "Exception serializing document");
	  exec->setException(err);
	  return err;
      }
    
      return getStringOrNull(body);
    }
  }

  return Undefined();
}