xmlserializer.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2003 Apple Computer, Inc.
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include "xmlserializer.h"
00022 #include "xmlserializer.lut.h"
00023 
00024 #include "dom/dom_exception.h"
00025 #include "dom/dom_doc.h"
00026 #include "xml/dom_docimpl.h"
00027 
00028 #include <kdebug.h>
00029 
00030 using namespace KJS;
00031 
00033 
00034 /* Source for XMLSerializerProtoTable.
00035 @begin XMLSerializerProtoTable 1
00036   serializeToString XMLSerializer::SerializeToString DontDelete|Function 1
00037 @end
00038 */
00039 DEFINE_PROTOTYPE("XMLSerializer",XMLSerializerProto)
00040 IMPLEMENT_PROTOFUNC_DOM(XMLSerializerProtoFunc)
00041 IMPLEMENT_PROTOTYPE(XMLSerializerProto,XMLSerializerProtoFunc)
00042 
00043 namespace KJS {
00044 
00045 XMLSerializerConstructorImp::XMLSerializerConstructorImp(ExecState *)
00046     : ObjectImp()
00047 {
00048 }
00049 
00050 bool XMLSerializerConstructorImp::implementsConstruct() const
00051 {
00052   return true;
00053 }
00054 
00055 Object XMLSerializerConstructorImp::construct(ExecState *exec, const List &)
00056 {
00057   return Object(new XMLSerializer(exec));
00058 }
00059 
00060 const ClassInfo XMLSerializer::info = { "XMLSerializer", 0, 0, 0 };
00061 
00062 XMLSerializer::XMLSerializer(ExecState *exec)
00063   : DOMObject(XMLSerializerProto::self(exec))
00064 {
00065 }
00066 
00067 Value XMLSerializerProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00068 {
00069   if (!thisObj.inherits(&XMLSerializer::info)) {
00070     Object err = Error::create(exec,TypeError);
00071     exec->setException(err);
00072     return err;
00073   }
00074 
00075   switch (id) {
00076   case XMLSerializer::SerializeToString:
00077     {
00078       if (args.size() != 1) {
00079     return Undefined();
00080       }
00081 
00082       if (!args[0].toObject(exec).inherits(&DOMNode::info)) {
00083     return Undefined();
00084       }
00085 
00086       DOM::NodeImpl *node = static_cast<DOM::NodeImpl *>(static_cast<KJS::DOMNode *>(args[0].toObject(exec).imp())->toNode().handle());
00087 
00088       if (!node) {
00089     return Undefined();
00090       }
00091 
00092       QString body;
00093 
00094       try {
00095       body = node->toString().string();
00096       } catch(DOM::DOMException& e) {
00097       Object err = Error::create(exec, GeneralError, "Exception serializing document");
00098       exec->setException(err);
00099       return err;
00100       }
00101 
00102       return getString(body);
00103     }
00104   }
00105 
00106   return Undefined();
00107 }
00108 
00109 } // end namespace
KDE Home | KDE Accessibility Home | Description of Access Keys