domparser.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2005 Anders Carlsson (andersca@mac.com)
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 "domparser.h"
00022 #include "domparser.lut.h"
00023 
00024 #include "kjs_dom.h"
00025 #include "kjs_window.h"
00026 #include "xml/dom_nodeimpl.h"
00027 #include "xml/dom_docimpl.h"
00028 
00029 #include "html/html_documentimpl.h"
00030 
00031 using DOM::DocumentImpl;
00032 
00034 
00035 /* Source for DOMParserProtoTable.
00036 @begin DOMParserProtoTable 1
00037   parseFromString DOMParser::ParseFromString DontDelete|Function 2
00038 @end
00039 */
00040 
00041 using namespace KJS;
00042 
00043 DEFINE_PROTOTYPE("DOMParser",DOMParserProto)
00044 IMPLEMENT_PROTOFUNC_DOM(DOMParserProtoFunc)
00045 IMPLEMENT_PROTOTYPE(DOMParserProto,DOMParserProtoFunc)
00046 
00047 namespace KJS {
00048 
00049 DOMParserConstructorImp::DOMParserConstructorImp(ExecState *, DOM::DocumentImpl *d)
00050     : doc(d)
00051 {
00052 }
00053 
00054 bool DOMParserConstructorImp::implementsConstruct() const
00055 {
00056   return true;
00057 }
00058 
00059 Object DOMParserConstructorImp::construct(ExecState *exec, const List &)
00060 {
00061   return Object(new DOMParser(exec, doc.get()));
00062 }
00063 
00064 const ClassInfo DOMParser::info = { "DOMParser", 0, 0 /* &DOMParserTable*/, 0 };
00065 
00066 
00067 DOMParser::DOMParser(ExecState *exec, DOM::DocumentImpl *d)
00068   : DOMObject(DOMParserProto::self(exec)), doc(d)
00069 {
00070 //   setPrototype(DOMParserProto::self(exec));
00071 }
00072 
00073 
00074 Value DOMParserProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00075 {
00076   if (!thisObj.inherits(&DOMParser::info)) {
00077     Object err = Error::create(exec,TypeError);
00078     exec->setException(err);
00079     return err;
00080   }
00081 
00082   DOMParser *parser = static_cast<DOMParser *>(thisObj.imp());
00083 
00084   switch (id) {
00085   case DOMParser::ParseFromString:
00086     {
00087       if (args.size() != 2) {
00088                 return Undefined();
00089       }
00090 
00091       QString str = args[0].toString(exec).qstring();
00092       QString contentType = args[1].toString(exec).qstring().stripWhiteSpace();
00093 
00094       if (contentType == "text/xml" || contentType == "application/xml" || contentType == "application/xhtml+xml") {
00095         DocumentImpl *docImpl = parser->doc->implementation()->createDocument();
00096 
00097         docImpl->open();
00098         docImpl->write(str);
00099         docImpl->finishParsing();
00100         docImpl->close();
00101 
00102         return getDOMNode(exec, docImpl);
00103       }
00104     }
00105   }
00106 
00107   return Undefined();
00108 }
00109 
00110 } // end namespace
00111 
00112 
KDE Home | KDE Accessibility Home | Description of Access Keys