KHtml

xmlserializer.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2003 Apple Computer, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "xmlserializer.h"
21 #include "xmlserializer.lut.h"
22 
23 #include "dom/dom_exception.h"
24 #include "dom/dom_doc.h"
25 #include "xml/dom_docimpl.h"
26 
27 using namespace KJS;
28 
29 ////////////////////// XMLSerializer Object ////////////////////////
30 
31 /* Source for XMLSerializerProtoTable.
32 @begin XMLSerializerProtoTable 1
33  serializeToString XMLSerializer::SerializeToString DontDelete|Function 1
34 @end
35 */
36 namespace KJS
37 {
38 KJS_DEFINE_PROTOTYPE(XMLSerializerProto)
39 KJS_IMPLEMENT_PROTOFUNC(XMLSerializerProtoFunc)
40 KJS_IMPLEMENT_PROTOTYPE("XMLSerializer", XMLSerializerProto, XMLSerializerProtoFunc, ObjectPrototype)
41 
42 XMLSerializerConstructorImp::XMLSerializerConstructorImp(ExecState *exec)
43  : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype())
44 {
45 }
46 
47 bool XMLSerializerConstructorImp::implementsConstruct() const
48 {
49  return true;
50 }
51 
52 JSObject *XMLSerializerConstructorImp::construct(ExecState *exec, const List &)
53 {
54  return new XMLSerializer(exec);
55 }
56 
57 const ClassInfo XMLSerializer::info = { "XMLSerializer", nullptr, nullptr, nullptr };
58 
59 XMLSerializer::XMLSerializer(ExecState *exec)
60 {
61  setPrototype(XMLSerializerProto::self(exec));
62 }
63 
64 JSValue *XMLSerializerProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
65 {
66  KJS_CHECK_THIS(XMLSerializer, thisObj);
67 
68  switch (id) {
69  case XMLSerializer::SerializeToString: {
70  if (args.size() != 1) {
71  return jsUndefined();
72  }
73 
74  if (!args[0]->toObject(exec)->inherits(&DOMNode::info)) {
75  return jsUndefined();
76  }
77 
78  DOM::NodeImpl *node = static_cast<KJS::DOMNode *>(args[0]->toObject(exec))->impl();
79 
80  if (!node) {
81  return jsUndefined();
82  }
83 
84  DOMString body;
85 
86  try {
87  body = node->toString();
88  } catch (DOM::DOMException &) {
89  JSObject *err = Error::create(exec, GeneralError, "Exception serializing document");
90  exec->setException(err);
91  return err;
92  }
93 
94  return ::getStringOrNull(body);
95  }
96  }
97 
98  return jsUndefined();
99 }
100 
101 } // end namespace
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impos...
Definition: dom_exception.h:58
void setException(JSValue *e)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
int size() const
static JSObject * create(ExecState *, ErrorType, const UString &message, int lineNumber, int sourceId, const UString &sourceURL)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.