File indexing completed on 2024-04-28 15:23:20

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2003 Apple Computer, Inc.
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Lesser General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Lesser General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Lesser General Public
0016  *  License along with this library; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "xmlserializer.h"
0021 #include "xmlserializer.lut.h"
0022 
0023 #include "dom/dom_exception.h"
0024 #include "dom/dom_doc.h"
0025 #include "xml/dom_docimpl.h"
0026 
0027 using namespace KJS;
0028 
0029 ////////////////////// XMLSerializer Object ////////////////////////
0030 
0031 /* Source for XMLSerializerProtoTable.
0032 @begin XMLSerializerProtoTable 1
0033   serializeToString XMLSerializer::SerializeToString DontDelete|Function 1
0034 @end
0035 */
0036 namespace KJS
0037 {
0038 KJS_DEFINE_PROTOTYPE(XMLSerializerProto)
0039 KJS_IMPLEMENT_PROTOFUNC(XMLSerializerProtoFunc)
0040 KJS_IMPLEMENT_PROTOTYPE("XMLSerializer", XMLSerializerProto, XMLSerializerProtoFunc, ObjectPrototype)
0041 
0042 XMLSerializerConstructorImp::XMLSerializerConstructorImp(ExecState *exec)
0043     : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype())
0044 {
0045 }
0046 
0047 bool XMLSerializerConstructorImp::implementsConstruct() const
0048 {
0049     return true;
0050 }
0051 
0052 JSObject *XMLSerializerConstructorImp::construct(ExecState *exec, const List &)
0053 {
0054     return new XMLSerializer(exec);
0055 }
0056 
0057 const ClassInfo XMLSerializer::info = { "XMLSerializer", nullptr, nullptr, nullptr };
0058 
0059 XMLSerializer::XMLSerializer(ExecState *exec)
0060 {
0061     setPrototype(XMLSerializerProto::self(exec));
0062 }
0063 
0064 JSValue *XMLSerializerProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
0065 {
0066     KJS_CHECK_THIS(XMLSerializer, thisObj);
0067 
0068     switch (id) {
0069     case XMLSerializer::SerializeToString: {
0070         if (args.size() != 1) {
0071             return jsUndefined();
0072         }
0073 
0074         if (!args[0]->toObject(exec)->inherits(&DOMNode::info)) {
0075             return jsUndefined();
0076         }
0077 
0078         DOM::NodeImpl *node = static_cast<KJS::DOMNode *>(args[0]->toObject(exec))->impl();
0079 
0080         if (!node) {
0081             return jsUndefined();
0082         }
0083 
0084         DOMString body;
0085 
0086         try {
0087             body = node->toString();
0088         } catch (DOM::DOMException &) {
0089             JSObject *err = Error::create(exec, GeneralError, "Exception serializing document");
0090             exec->setException(err);
0091             return err;
0092         }
0093 
0094         return ::getStringOrNull(body);
0095     }
0096     }
0097 
0098     return jsUndefined();
0099 }
0100 
0101 } // end namespace