File indexing completed on 2025-02-16 13:49:57

0001 /*
0002 ** A program to convert the XML rendered by Calligra into LATEX.
0003 **
0004 ** Copyright (C) 2000-2002 Robert JACOLIN
0005 **
0006 ** This library is free software; you can redistribute it and/or
0007 ** modify it under the terms of the GNU Library General Public
0008 ** License as published by the Free Software Foundation; either
0009 ** version 2 of the License, or (at your option) any later version.
0010 **
0011 ** This library is distributed in the hope that it will be useful,
0012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014 ** Library General Public License for more details.
0015 **
0016 ** To receive a copy of the GNU Library General Public License, write to the
0017 ** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 **
0020 */
0021 
0022 #include "xmlparser.h"
0023 
0024 #include "LatexDebug.h"
0025 
0026 #include <KoStore.h>
0027 
0028 #include <QFile>
0029 
0030 /* Init static data */
0031 FileHeader* XmlParser::_fileHeader = 0;
0032 Document* XmlParser::_root = 0;
0033 KoStore* XmlParser::_in = nullptr;
0034 
0035 XmlParser::XmlParser(const QString &filename): _filename(filename)
0036 {
0037     QFile f(filename);
0038     if (!f.open(QIODevice::ReadOnly))
0039         return;
0040     if (!_document.setContent(&f)) {
0041         f.close();
0042         return;
0043     }
0044     f.close();
0045     //_eltCurrent = _document.documentElement();
0046 }
0047 
0048 XmlParser::XmlParser(QByteArray in)
0049 {
0050     _document.setContent(in);
0051 }
0052 
0053 XmlParser::XmlParser(const KoStore* in)
0054 {
0055     _in = const_cast<KoStore*>(in);
0056     if (!_in->open("root")) {
0057         errorLatex << "Unable to open input file!" << endl;
0058         return;
0059     }
0060     /* input file Reading */
0061     QByteArray array = _in->read(_in->size());
0062     _document.setContent(array);
0063 }
0064 
0065 XmlParser::XmlParser()
0066 {
0067 }
0068 
0069 XmlParser::~XmlParser()
0070 {
0071     if (_in != nullptr)
0072         _in->close();
0073 }
0074 
0075 QDomNode XmlParser::getChild(const QDomNode &node, QString name)
0076 {
0077     QDomNode childNode = getChild(node, name, 0);
0078     debugLatex << childNode.nodeName();
0079     return childNode;
0080 }
0081 
0082 bool XmlParser::isChild(const QDomNode &node, QString name)
0083 {
0084     if (node.isElement())
0085         return node.toElement().elementsByTagName(name).count();
0086     return false;
0087 }
0088 
0089 QDomNode XmlParser::getChild(const QDomNode &node, QString name, int index)
0090 {
0091     if (node.isElement()) {
0092         QDomNodeList children = node.toElement().elementsByTagName(name);
0093         if (children.count())
0094             return children.item(index);
0095     }
0096     return QDomNode();
0097 }
0098 
0099 QDomNode XmlParser::getChild(const QDomNode &node, int index)
0100 {
0101     QDomNodeList children = node.childNodes();
0102     if (children.count())
0103         return children.item(index);
0104     return QDomNode();
0105 }
0106 
0107 QString XmlParser::getData(const QDomNode &node, int index)
0108 {
0109     return getChild(getChild(node, index), 0).nodeValue();
0110 }
0111 
0112 QString XmlParser::getData(const QDomNode &node, QString name)
0113 {
0114     return getChild(getChild(node, name), 0).nodeValue();
0115 }
0116 
0117 int XmlParser::getNbChild(const QDomNode &node)
0118 {
0119     return node.childNodes().count();
0120 }
0121 
0122 int XmlParser::getNbChild(const QDomNode &node, QString name)
0123 {
0124     if (node.isElement())
0125         return node.toElement().elementsByTagName(name).count();
0126     return -1;
0127 }
0128 
0129 QString  XmlParser::getChildName(const QDomNode &node, int index)
0130 {
0131     return node.childNodes().item(index).nodeName();
0132 }
0133 
0134 QString  XmlParser::getAttr(const QDomNode &node, QString name) const
0135 {
0136     if (node.isElement())
0137         return node.toElement().attributeNode(name).value();
0138     return QString();
0139 }