filters

xmlparser.cc

00001 /*
00002 ** A program to convert the XML rendered by KOffice into LATEX.
00003 **
00004 ** Copyright (C) 2000-2003 Robert JACOLIN
00005 **
00006 ** This library is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU Library 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 ** Library General Public License for more details.
00015 **
00016 ** To receive a copy of the GNU Library General Public License, write to the
00017 ** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 **
00020 */
00021 
00022 #include <kdebug.h>
00023 #include <KoStore.h>
00024 
00025 #include "xmlparser.h"
00026 #include "qfile.h"
00027 
00028 /* Init static data */
00029 //FileHeader* XmlParser::_fileHeader = 0;
00030 Document* XmlParser::_root = 0;
00031 KoStore* XmlParser::_in = NULL;
00032 QString XmlParser::_filename = "";
00033 
00034 XmlParser::XmlParser(Config* config, QString filename):_config(config)
00035     
00036 {
00037     //_filename = filename;
00038     QFile f(filename);
00039     if(!f.open(IO_ReadOnly))
00040         return;
00041     if(!_document.setContent(&f))
00042     {
00043         f.close();
00044         return;
00045     }
00046     else
00047         f.close();
00048     //_eltCurrent = _document.documentElement();
00049 }
00050 
00051 XmlParser::XmlParser(Config* config, QByteArray in): _config(config)
00052 {
00053     _document.setContent(in);
00054 }
00055 
00056 XmlParser::XmlParser(Config* config, const KoStore* in): _config(config)
00057 {
00058   _in = const_cast<KoStore*>(in);
00059     if(!_in->open("root"))
00060     {
00061             kdError(30503) << "Unable to open input file!" << endl;
00062             return;
00063     }
00064     /* input file Reading */
00065     QByteArray array = _in->read(_in->size());
00066     _document.setContent(array);
00067     if(!_in->close())
00068     {
00069         kdError(30522) << "unable to close input file" << endl;
00070         return;
00071     }
00072 }
00073 
00074 XmlParser::XmlParser()
00075 {
00076     _config = Config::instance();
00077     /*_in = xmlParser.getStorage();
00078     _fileHeader = xmlParser.getFileHeader();
00079     _root = xmlParser.getRoot();
00080     _document.setContent(_in);
00081     _filename = xmlParser.getFilename();*/
00082 }
00083 
00084 XmlParser::~XmlParser()
00085 {
00086     /*if(_in != NULL)
00087         _in->close();*/
00088 }
00089 
00090 QDomNode XmlParser::getChild(QDomNode balise, QString name)
00091 {
00092     QDomNode node = getChild(balise, name, 0);
00093     kdDebug(30522) << node.nodeName() << endl;
00094     return node;
00095 }
00096 
00097 bool XmlParser::isChild(QDomNode balise, QString name)
00098 {
00099     if(balise.isElement())
00100         return balise.toElement().elementsByTagName(name).count();
00101     return false;
00102 }
00103 
00104 QDomNode XmlParser::getChild(QDomNode balise, QString name, int index)
00105 {
00106     if(balise.isElement()) {
00107         QDomNodeList children = balise.toElement().elementsByTagName(name);
00108         if ( children.count() )
00109             return children.item(index);
00110     }
00111     return QDomNode();
00112 }
00113 
00114 QDomNode XmlParser::getChild(QDomNode balise, int index)
00115 {
00116     QDomNodeList children = balise.childNodes();
00117     if ( children.count() )
00118         return children.item(index);
00119     return QDomNode();
00120 }
00121 
00122 QString XmlParser::getData(QDomNode balise, int index)
00123 {
00124     return getChild(getChild(balise, index), 0).nodeValue();
00125 }
00126 
00127 int XmlParser::getNbChild(QDomNode balise)
00128 {
00129     return balise.childNodes().count();
00130 }
00131 
00132 int XmlParser::getNbChild(QDomNode balise, QString name)
00133 {
00134     if(balise.isElement())
00135         return balise.toElement().elementsByTagName(name).count();
00136     return -1;
00137 }
00138 
00139 QString  XmlParser::getChildName(QDomNode balise, int index)
00140 {
00141     return balise.childNodes().item(index).nodeName();
00142 }
00143 
00144 QString  XmlParser::getAttr(QDomNode balise, QString name) const
00145 {
00146     if(balise.isElement())
00147         return balise.toElement().attributeNode(name).value();
00148     return QString();
00149 }
KDE Home | KDE Accessibility Home | Description of Access Keys